2015-08-13 88 views
0

我想在c#中的数据插入到sqlite数据库。我正在使用WinForms。我有一个例外FileLoadException如何使用c#将数据插入Sqlite数据库#

SQLiteConnection con = new SQLiteConnection(@"Data Source=c:\\sqlite\\mySqliteDb.sqlite;Version=3;New=True;Compress=True;"); 

private void InsBtn_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     con.Open(); 
     string Query = "insert into sqliteDb(ID,Name) values('"+this.textBox1.Text + "','" + this.textBox2.Text + "')"; 

     SQLiteCommand cmd = new SQLiteCommand(Query,con); 
     cmd.ExecuteNonQuery(); 
     con.close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 
+0

在哪一行抛出该异常? 'sqliteDb'表中的列类型是什么?你应该总是使用[参数化查询](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。这种字符串连接对于[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻击是开放的。 –

回答

0

你应该值插入一个,不能直接进入数据库。

假设有一个表:

Create table TbTexts(val1 text, val2 text) 

然后

string Query = "insert into TbTexts(val1, val2) values('"+this.textBox1.Text + "','" + this.textBox2.Text + "')";