【发布时间】:2014-09-02 08:15:44
【问题描述】:
这是我的代码
namespace SDD_Single_Project___Michael
{
public partial class NewUser : Form
{
private OleDbConnection connection = new OleDbConnection();
public NewUser()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\schoolwork\Year 11\SDD\3 SINGLE TASK\SDD Single Project - Michael \SDD Single Project - Michael \bin\Persondata.accdb;
Persist Security Info=False;";
}
private void btnBack_Click(object sender, EventArgs e)
{
this.Hide(); //hides this page
MainScreen frm = new MainScreen(); //finds the next screen (the main game)
frm.Show(); //shows it
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try {
connection.Open(); // opens the connection
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "insert into Persondata where ( FirstName,LastName,Address,Suburb,Email,Mobile) values ( '" + txtFirst.Text + "' , '" + txtLast.Text + "' , '" + txtAddress.Text + "' , '" + txtSuburb.Text + "' , '" + txtEmail.Text + "' , '" + txtMobile.Text + "' ) ";
// finds where its going to, finds the columns it is going to fill, finds the text boxes that is going to fill them
command.ExecuteNonQuery(); // error occurs here!!!
MessageBox.Show("Data Saved");
connection.Close(); // closes the connection
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
} //if there is a error message box will appear informing it
}
}
}
错误发生在command.ExecuteNonQuery();,我无法修复它,一旦我将所有信息填写到文本框中并按下提交按钮,错误就会发生。
错误说这是INSERT INTO语句中的语法错误
System.Data.Ole.DbCommand.ExecuteNonQuery();
请帮忙!是为了任务!我一直在努力解决它。感谢所有帮助。
【问题讨论】:
-
您应该始终使用parameterized queries。这种字符串连接对SQL Injection 攻击开放。
-
这里出现错误!!!什么错误?发布异常详情。
-
Prewarning about sql injection.. 到目前为止你尝试过什么?您是否确认该表存在?发布完整的错误消息以启动和重现问题所需的最少代码。
-
如果从插入语句中删除
where关键字会怎样?
标签: c# oledbconnection executenonquery