我目前正在尝试使用 C# 将数据添加到访问数据库(另存为 mdb)中,这是我当前的代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private OleDbConnection bookConn;
private OleDbCommand oleDbCmd = new OleDbCommand();
private String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Alex\Desktop\Project\example.mdb;Persist Security Info=False";
public Form1()
{
bookConn = new OleDbConnection(connParam);
InitializeComponent();
}
public void add()
{
try
{
bookConn.Open();
oleDbCmd.Connection = bookConn;
oleDbCmd.CommandText = "INSERT INTO Student (StudentID, Module) VALUES ('"+ this.textBox1.Text +"','"+ this.textBox2.Text +"');";
oleDbCmd.CommandType = CommandType.Text;
int temp = oleDbCmd.ExecuteNonQuery();
if (temp > 0)
{
MessageBox.Show("Added");
}
else
{
MessageBox.Show("Failed");
}
bookConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
add();
}
}
}
当我运行此代码时,我收到错误消息:INSERT INTO 语句中的语法错误。
当我与其他示例进行比较时,我无法弄清楚我做错了什么,它似乎设置正确。
任何解决此问题的帮助将不胜感激。