我一直試圖讓我的查詢工作一段時間,但它並沒有插入任何東西,也沒有返回任何錯誤。SQL查詢不會被插入
數據庫連接已打開且連接成功。
的表稱爲錯誤日誌,並擁有以下數據
- 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);
}
}
任何幫助,將不勝感激,在此先感謝。
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;
}
time = DateTime,我試着用ToString()來設置DataType爲nVarChar。沒有差別。沒有任何錯誤插入。 – Deejdd
刪除@exception中的引號,然後重試。 – vtortola
重構你的初學者代碼..整個方法看起來是錯誤的,特別是你如何處理參數 – MethodMan