【问题标题】:How to INSERT INTO access database with c# [closed]如何使用 c# 插入访问数据库 [关闭]
【发布时间】:2016-10-25 14:29:59
【问题描述】:

我正在尝试使用 c# 从 winform 将数据添加到我的 access 数据库中。

我不断收到关于我的 INSERT INTO 语句的语法错误,并且看不到哪里出错了。

请有人检查我的代码并告诉我哪里出错了。

private void btnLog_Click(object sender, EventArgs e)
{
    txtStatus.Text = "Open";

    conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\mwool\\Desktop\\Uni\\3rd Year\\SEM 1\\AP\\Assignment\\Staff.accdb";

    string sql = "INSERT INTO Fault (faultType, Status, TechId, StaffId, Zone, Description) VALUES ('" + txtFaultType.Text + "', '" + txtStatus.Text + "', " + txtTechId.Text + "' , '" + txtStaffId.Text + "' , '" + txtZone.Text + "' , '" + txtDescription.Text + "')";

    OleDbCommand add = new OleDbCommand();

    add.CommandText = sql;

    add.Connection = conn;

    add.Connection.Open();

    add.ExecuteNonQuery();

    conn.Close();

}

【问题讨论】:

  • 您可以访问sql injection attacks
  • 您应该为该查询使用准备好的语句
  • 感谢我的 sql 语句可能存在缺陷,我是一名学生,这是我的作业。该程序处于早期开发阶段。感谢您的提醒,我会考虑解决这个问题。

标签: c# sql ms-access insert-into


【解决方案1】:

您错过了txtTechId.Text 之前的单引号。但是,您应该始终使用parameterized queries 来避免使用SQL Injection

string sql = "INSERT INTO Fault (faultType, Status, TechId, StaffId, Zone, Description) VALUES (@a,@b,@c,@d,@e,@f)";
add.Parameters.AddWithValue("@a", txtFaultType.Text);
add.Parameters.AddWithValue("@b", txtStatus.Text);
add.Parameters.AddWithValue("@c", txtTechId.Text);
add.Parameters.AddWithValue("@d", txtStaffId.Text);
add.Parameters.AddWithValue("@e", txtZone.Text);
add.Parameters.AddWithValue("@f", txtDescription.Text);

【讨论】:

  • 谢谢,我添加了单引号但没有改变,我会尝试你的其他建议并让你知道。
  • 谢谢,我试过了,但仍然收到以下消息:
  • 抛出异常:System.Data.dll 中的“System.Data.OleDb.OleDbException”附加信息:INSERT INTO 语句中的语法错误。如果有这个异常的处理程序,程序可以安全地继续。
  • 现在还有这条消息
  • System.Data.dll 中出现“System.Data.OleDb.OleDbException”类型的未处理异常附加信息:INSERT INTO 语句中的语法错误。如果有这个异常的处理程序,程序可以安全地继续。
【解决方案2】:
  • 始终使用参数化查询。这可以防止简单的错误,例如忘记带有字符串的',但更重要的是可以防止 sql 注入攻击。

  • 还要始终将您的数据库连接、命令和任何其他 Disposable 对象包装在 using 块中。

使用 using 语句和参数化输入重构您的代码。

using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\mwool\\Desktop\\Uni\\3rd Year\\SEM 1\\AP\\Assignment\\Staff.accdb"))
using (OleDbCommand cmd = new OleDbCommand())
{
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = string sql = "INSERT INTO Fault (faultType, Status, TechId, StaffId, Zone, [Description]) VALUES (?, ?, ?, ?, ?, ?)";

    cmd.Parameters.Add(new OleDbParameter("@faultType", OleDbType.VarChar)).Value = txtFaultType.Text;
    cmd.Parameters.Add(new OleDbParameter("@Status", OleDbType.VarChar)).Value = txtStatus.Text;

    // this parameter is an example of passing an int instead of a string. Alwaysuse the correct types!
    cmd.Parameters.Add(new OleDbParameter("@TechId", OleDbType.Int)).Value = int.Parse(txtTechId.Text);

    cmd.Parameters.Add(new OleDbParameter("@StaffId", OleDbType.VarChar)).Value = txtStaffId.Text;
    cmd.Parameters.Add(new OleDbParameter("@Zone", OleDbType.VarChar)).Value = txtZone.Text;
    cmd.Parameters.Add(new OleDbParameter("@Description", OleDbType.VarChar)).Value = txtDescription.Text;

    con.Open();
    cmd.ExecuteNonQuery();
}

OleDbCommand 不支持命名参数,见OleDbCommand.Parameters

备注

当 CommandType 设置为 Text 时,OLE DB .NET 提供程序不支持将参数传递给 SQL 语句或由 OleDbCommand 调用的存储过程的命名参数。在这种情况下,必须使用问号 (?) 占位符。


还要注意:

  • OleConnection 和 OleDbCommand 包装在 using 块中,因此即使发生异常,它们也会被释放/清理。
  • 现在使用参数而不是对字符串值进行硬编码
  • 参数使用正确的数据类型

可能不允许使用Description,因为它是reserved word(见链接)。在这种情况下,用[] 包围它(参见上面的更新)。

【讨论】:

  • 谢谢你,但它仍然不起作用。我做错了什么,可能是一些非常基本的事情。可能是我数据库中字段的数据类型不对。
  • @MWoolley - 可能是,我在上面有一个例子,并在最后一个注释中提到数据类型很关键。检查表中的数据类型并将它们与OleDbParameter 中选定的OleDbType 相匹配,并确保txtXXXX.text 值转换为正确的数据类型。
  • @MWoolley - 请参阅 TechId 作为示例参数。
  • @MWoolley - 可能是 Description 不被允许,因为它是一个保留字。请参阅我的更新,如果这是您目前所拥有的唯一问题,则使用 [] 将解决它。
  • 谢谢,我会试一试,尝试匹配数据库数据类型并通知您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多