C# 如何用c语言插入access数据库#
我正在尝试使用c#从winform向access数据库添加数据 关于INSERT INTO语句,我一直遇到语法错误,看不出哪里出错了C# 如何用c语言插入access数据库#,c#,sql,ms-access,insert-into,C#,Sql,Ms Access,Insert Into,我正在尝试使用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\\Desk
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();
}
请有人检查我的代码,告诉我哪里出了问题
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();
}
您在
txtTechId.Text
之前错过了一个报价。但是,您应该始终使用以避免
您在
txtTechId.Text
之前错过了一个报价。但是,您应该始终使用以避免
- 始终使用参数化查询。这可以防止简单的错误,例如忘记带有字符串的
,但更重要的是可以防止sql注入攻击”
- 还应始终使用块将数据库连接、命令和任何其他一次性对象包装在
中
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不支持命名参数,请参阅 评论 当CommandType设置为Text时,OLE DB.NET提供程序不支持将参数传递给SQL语句或OleDbCommand调用的存储过程的命名参数。在这种情况下,必须使用问号(?)占位符
还请注意:
- OleConnection和OleDbCommand使用块包装在
中,因此即使发生异常,它们也会被释放/清理
- 现在使用参数,而不是硬编码字符串值
- 参数使用正确的数据类型
Description
,因为它是一个(请参阅链接)。在这种情况下,用[]
(参见上面的更新)将其包围起来。- 始终使用参数化查询。这可以防止简单的错误,例如忘记带有字符串的
,但更重要的是可以防止sql注入攻击”
- 还应始终使用块将数据库连接、命令和任何其他一次性对象包装在
中
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不支持命名参数,请参阅 评论 当CommandType设置为Text时,OLE DB.NET提供程序不支持将参数传递给SQL语句或OleDbCommand调用的存储过程的命名参数。在这种情况下,必须使用问号(?)占位符
还请注意:
- OleConnection和OleDbCommand使用块包装在
中,因此即使发生异常,它们也会被释放/清理
- 现在使用参数,而不是硬编码字符串值
- 参数使用正确的数据类型
可能是不允许使用
Description
,因为它是一个(请参阅链接)。在这种情况下,用[]
(见上面的更新)将其包围起来。您可以使用准备好的语句进行查询。我知道我的sql语句可能有缺陷,我是一名学生,这是我的作业。该项目处于早期开发阶段。谢谢你的提醒,我会研究解决这个问题的。你是不可能的。你应该用准备好的语句来回答这个问题。我明白我的sql语句可能有缺陷,我是一名学生,这是我的作业。该项目处于早期开发阶段。感谢您的提醒,我将研究解决此问题。谢谢,我添加了单引号,但没有任何更改,我将尝试您的其他建议并让您知道。谢谢,我尝试了此操作,但仍然收到以下消息:抛出异常:System.Data.OleDb.OleDbException中的System.Data.dll附加信息:INSERT into语句中的语法错误。如果存在此异常的处理程序,则程序可以安全地继续。现在,此消息以及System.Data.OleDb.OledBeException类型的未处理异常在System.Data.dll中发生。其他信息:INSERT INTO语句中的语法错误。如果有此异常的处理程序,程序可能会安全地继续。谢谢,我添加了单引号,但没有更改,我将尝试您的其他建议并让您知道。谢谢,我尝试了此操作,但仍然得到以下消息:抛出异常:“System.Data.OleDb.OleDbException”在System.Data.dll中。其他信息:INSERT INTO语句中出现语法错误。如果存在此异常的处理程序,则程序可以安全地继续。现在,此消息以及System.Data.OleDb.OledBeException类型的未处理异常在System.Data.dll中发生。其他信息:INSERT INTO语句中的语法错误。如果有此异常的处理程序,程序可以安全地继续。谢谢,但它仍然无法工作。我做错了一些事情,可能是一些非常基本的事情。可能我数据库中字段的数据类型不正确。@MWoolley-可能不正确,我有一个上面的例子,并在最后一个注释中提到数据类型是关键的。检查表中的数据类型,并将其与OleDbParameter
中选定的OleDbType
进行匹配,同时确保txtXXXX.text
值已转换为正确的数据类型。@MWoolley-请参阅TechId作为示例参数。@MWoolley-可能是不允许使用Description
因为它是一个保留字。请参阅我的更新,如果这是您目前唯一的问题,则使用[]将修复它。谢谢,我将尝试使用它,尝试匹配数据库数据类型并让您知道。谢谢,但它仍然不起作用。我做错了一些事情,可能是一些非常基本的事情。可能我数据库中字段的数据类型不正确。@MWoolley-可能不正确,我有一个上面的例子,并在最后一个注释中提到数据类型是关键的。检查表中的数据类型,并将其与OleDbParameter
中选定的OleDbType
以及make匹配