2012-03-04 10 views
5

안녕하세요 코드는 구문 오류를 제공합니다. 문제를 해결하는 방법을 모르겠습니다.Sqlite "업데이트"C# 구문 오류

오류

{myText를 \ "SQLite는 오류 \ 연구 \ nnear \" "구문 오류"}

내 코드

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 주입)를 방지하려면 SQL 명령 문자열을 동적으로 작성하지 말고 조회 매개 변수를 사용해야합니다. – svick

+2

@ user1248067 : * 허용되는 답변을있는 그대로 사용하지 마십시오. 매개 변수화 된 SQL을 사용해야합니다. –

답변

21

다른 사람들은 SQL을 구성하는 다른 방법을 제안했지만 SQL에 값을 포함하지 않아야합니다. 다른 것들 중에서 SQL injection attacks을 피하는 매개 변수가있는 쿼리를 사용해야합니다.

어떤 드라이버를 사용하고 있는지 즉시 알 수는 없지만 Devart.com이라고 가정하면 SQLiteCommand.Parameters에 대한 설명서는이를 수행하는 좋은 예입니다. 귀하의 경우, 코드는 다음과 같이됩니다.

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 : 음 ... 편집합니다. –

5

당신은 ' 2를 놓쳤다 번

시험해보기 :

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