2012-03-04 9 views
5

こんにちは、次のコードでは構文エラーが発生します。問題の解決方法はわかりません。Sqlite "Update" C#構文エラー

エラー

{ "SQLiteのエラー\ rを\ nnear \" MYTEXT \ ":構文エラー"}

マイコード

string dataSource = "Database.s3db"; 
SQLiteConnection connection = new SQLiteConnection(); 
connection.ConnectionString = "Data Source=" + dataSource; 
connection.Open(); 
SQLiteCommand command = new SQLiteCommand(connection); 
command.CommandText = ("update Example set Info ='" + textBox2.Text + ", Text ='"+textBox3.Text + "where ID ='" + textBox1.Text +"'"); 
command.ExecuteNonQuery(); 
+2

、あなたは動的SQLコマンド文字列を作成しないで、クエリパラメータを使用する必要があります。 – svick

+2

@ user1248067:*受け入れられた回答をそのまま使用しないでください。 *実際には、パラメータ化されたSQLを使用するべきです。 –

答えて

21

その他の方法では、SQLを構築する代替方法が示唆されていますが、SQLに値を含めないでください。パラメータ化されたクエリを使用する必要があります。これは、特にSQL injection attacksを回避します。

あなたが使用しているドライバはすぐにはわかりませんが、Devart.comのものとすれば、SQLiteCommand.Parametersのドキュメントがこれを行う良い例です。あなたのケースでは、コードは次のようになる:エラー(およびSQLインジェクション)この種のを避けるために

string dataSource = "Database.s3db"; 
using (SQLiteConnection connection = new SQLiteConnection()) 
{ 
    connection.ConnectionString = "Data Source=" + dataSource; 
    connection.Open(); 
    using (SQLiteCommand command = new SQLiteCommand(connection)) 
    { 
     command.CommandText = 
      "update Example set Info = :info, Text = :text where ID=:id"; 
     command.Parameters.Add("info", DbType.String).Value = textBox2.Text; 
     command.Parameters.Add("text", DbType.String).Value = textBox3.Text; 
     command.Parameters.Add("id", DbType.String).Value = textBox1.Text; 
        command.ExecuteNonQuery(); 
    } 
} 
+0

バージョンの問題かどうかわかりません。 "SqLiteType.Text"の代わりに "DbType.String"を使用しなければなりませんでした。私はSQLiteバージョン1.0.99を使用しています – sapbucket

+0

@sapbucket:Mmm ...編集します。 –

5

あなたは' 2を逃しました回

試用:

command.CommandText = ("update Example set Info ='" + textBox2.Text + "', Text ='"+textBox3.Text + "' where ID ='" + textBox1.Text +"'");