c# - update into 语句中 Access 数据库语法错误

标签 c# database ms-access syntax-error insert-update

首先我知道密码是一种保留类型的Access数据库。但我读过一篇文章,你可以像那样输入 [password] 并且它会起作用。但它不起作用。我已经尝试了很多方法,但仍然希望有人能有所帮助。

OleDbCommand cmd = new OleDbCommand();

try
{
    String query = "update [Employe] set [UserName] ='" + txtNewUser.Text +"', [Password] ='"+ txtNewPass.Text + "', [Authorization] ='" + nudAuthorizationLvl.Value + "', where [Id] = '" + int.Parse(txtExistingId.Text);
    cmd.CommandText = query;
    cmd.Connection = conn;
    conn.Open();

    cmd.ExecuteNonQuery();
    System.Windows.Forms.MessageBox.Show("Info Updated!!!");

    conn.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error" + ex);
}
finally
{
    conn.Close();
}

最佳答案

我相信您在 where 子句之前有一个额外的逗号,在 ID 之前有一个额外的引号。

此外,始终使用参数,以避免 Sql 注入(inject)攻击:

conn.Open();
cmd.CommandText = "update [Employe] set [UserName] =@userName, [Password] =@password, [Authorization] =@authorization where [Id] = @id";
cmd.Connection = conn;
cmd.Parameters.AddRange(new OleDbParameter[]
       {
           new OleDbParameter("@userName", txtNewUser.Text),
           new OleDbParameter("@password", txtNewPass.Text),
           new OleDbParameter("@authorization", nudAuthorizationLvl.Value),
           new OleDbParameter("@id", int.Parse(txtExistingId.Text))
       });
cmd.ExecuteNonQuery();

关于c# - update into 语句中 Access 数据库语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29873436/

相关文章:

c# - 在 Silverlight 中打开文件夹对话框?

javascript - 使用 Node JS 在 mongoose 中存储多个文本的理想方法是什么?

sql - 如何将现有 MySQL 表中的 id 更改为从最后一个最高 id 自动递增的数据?

mysql - SQL LEFT JOIN“不支持JOIN表达式”

c# - 连接字符串最有效的方法?

c# - 是否可以使用反射从通用实体访问属性(按名称)?

c# - 如何在 C# 中有效地共享连接池中的数据库连接对象?

sql - Access : how to detect with VBA whether a query is opened?

c# - 我的应用程序和 Access 查询向导之间的不同 LIKE 行为

c# - 使用基于属性的 LINQ 从列表中选择对象