2009-12-04 69 views
0

我有下面的代碼塊SQLite的不插入

SQLiteConnection cnn = new SQLiteConnection("Data Source=" + getDBPath()); 
cnn.Open(); 
SQLiteCommand mycommand = new SQLiteCommand(cnn); 
string values = "'" + this.section + "','" + this.exception + "','" + this.dateTimeString + "'"; 
string sql = @"INSERT INTO Emails_Pending (Section,Message,Date_Time) values (" + values + ")"; 
mycommand.CommandText = sql; 
mycommand.ExecuteNonQuery(); 
cnn.Close(); 

當我執行它,什麼都不會發生,沒有錯誤的產生,但沒有被插入,我究竟做錯了什麼?

DB的路徑是正確的! Insert語句作品,試圖在一個SQLLite GUI(沒有問題存在)

下面是SQL代碼段:

"INSERT INTO Emails_Pending (Section,Message,Date_Time) values ('Downloading Received Messages','Object reference not set to an instance of an object.','04.12.2009 11:09:49');" 

回答

0

如何將提交之前關閉

mycommand.Transaction.Commit(); 
+1

對象引用未設置爲對象的實例,我猜是因爲我沒有使用事務。 – 2009-12-04 10:04:21

0

你應該總是使用事務和參數化語句時使用sqlite,否則性能會非常慢。

這裏閱讀:Improve INSERT-per-second performance of SQLite?

你的做法是容易受到SQL注入太多。電子郵件中的消息可以在其正文中包含一段sql,並且您的代碼將執行這段sql。當字符串值中的消息包含「或a」時,您也可能遇到問題。