我一直试图让我的查询工作一段时间它运行但是没有插入任何东西也没有返回任何错误。
数据库连接已打开,并且已成功连接。
该表称为错误日志并保存以下数据
- 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;
}
答案 0 :(得分:1)
connection
是否完全指向
你想要的数据库。还要检查是否查找插入的记录
在同一个数据库中。 connection
属于该交易,请检查它是否已提交。在提交事务之前,您不会看到这些记录被插入。 INSERT
是错的。删除@exception
INSERT
是否显示在那里。