c# - Sqlite "Update"C#语法错误

标签 c# sql sqlite syntax

嗨,下面的代码给出了一个语法错误。我不知道如何解决这个问题。

错误

{"SQLite error\r\nnear \"Mytext\": syntax error"}

我的代码

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();

最佳答案

其他人提出了构建 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();
    }
}

关于c# - Sqlite "Update"C#语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556135/

相关文章:

c# - 如何使用 asp.net mvc 5 在一个页面上使用多个模型?

php - 需要 PHP 和 MySQL 方面的帮助

SQL 选择具有较高时间戳值的行

mysql - 优化 group_concat 函数的 MySQL 查询

android - 检查 SQL 查询是否在 Android SQLite 上成功

php - PHP和SQLite-将某些列值相乘

Python SQLite 如何使用 '?' 占位符进行部分搜索?

c# - 捆绑的 Javascript,如何检查 bundle 是否已在浏览器上正确加载并检查内容

c# - 命令行解析器动词帮助不起作用?

c# - 如何在数据库中保存一个特定值