【发布时间】: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