将新数据插入访问

时间:2011-10-04 14:46:26

标签: c# insert oledb

我想在我的oledb中插入一行新数据。我只是按照UPDATE的方式构建我的代码。但是, OleDbCommand ins = new OleDbCommand(insertString,strOleDbConnectionString);

如果不使用tableadapters或参数等,它实际上如何工作?

        string strOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
        OleDbConnection objConnection = new OleDbConnection(strOleDbConnectionString);

        string insertString = "INSERT INTO jiahe ([Tag ID], User, Age, [Phone Number]) VALUES (123, Esther, 19, 92786618)";
        string newTagID = textBox1.Text;
        string newUser = textBox2.Text;
        string newAge = textBox3.Text;
        string newPhoneNumber = textBox4.Text;

        OleDbCommand ins = new OleDbCommand(insertString, strOleDbConnectionString);

        ins.Connection.Open();

        ins.ExecuteNonQuery().ToString();

        ins.Connection.Close();

1 个答案:

答案 0 :(得分:0)

在您的查询中看起来像一个简单的错误..

如果User是一个字符串(不知道其余部分),应该简单引用

"INSERT INTO jiahe ([Tag ID], User, Age, [Phone Number]) VALUES (123, 'Esther', 19, 92786618)"

此外,oledbcommand接收连接对象,而不是连接字符串。试试

OleDbCommand ins = new OleDbCommand(insertString, objConnection );