SQL查询未插入

时间:2012-12-15 19:43:25

标签: c# sql-server

我一直试图让我的查询工作一段时间它运行但是没有插入任何东西也没有返回任何错误。

数据库连接已打开,并且已成功连接。

该表称为错误日志并保存以下数据

- id (int autoincremental, Primary key, Unique)
- exception (varchar)
- time (DateTime)

exception = String(error message)
time = DateTime.Now

以下是代码:

 public void insertError(string error, DateTime time)
    {
        SqlCeParameter[] sqlParams = new SqlCeParameter[]  
        {  
           new SqlCeParameter("@exception", error), 
           new SqlCeParameter("@time", time)
        };

        try
        {
            cmd = new SqlCeCommand();
            cmd.Connection = connection;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO errorlog (exception, time) VALUES(@exception, @time)";
            cmd.Parameters.AddRange(sqlParams);
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

任何帮助将不胜感激,提前致谢。

修改 删除了@exception

周围的引号

继承人:

protected DataController()
    {
        try
        {
            string appPath = System.IO.Path.GetDirectoryName(Assembly.GetAssembly(typeof(DataController)).CodeBase).Replace(@"file:\", "") + @"\";
            string strCon = @"Data Source = " + appPath + @"Data\EasyShop.sdf";
            connection = new SqlCeConnection(strCon);
        }
        catch (Exception e)
        {

        }

        connection.Open();
    }

最后调用它的方式:

public bool log(string msg, bool timestamp = true)
    {
        DataController dc = DataController.Instance();
        dc.insertError(msg, DateTime.Today);

        return true;
    }

1 个答案:

答案 0 :(得分:1)

  1. 调试您的应用程序,看看connection是否完全指向 你想要的数据库。还要检查是否查找插入的记录 在同一个数据库中。
  2. 如果您的connection属于该交易,请检查它是否已提交。在提交事务之前,您不会看到这些记录被插入。
  3. 在我看来,你INSERT是错的。删除@exception
  4. 周围的引号
  5. 打开SQL Server Profiler,连接到您的数据库并检查您的INSERT是否显示在那里。