开发者

inserting new data into access

I'd like to insert a new row of data into my oledb. I simply structured my codes the way I did with UPDATE.开发者_如何学C However, there's error with OleDbCommand ins = new OleDbCommand(insertString, strOleDbConnectionString);

How does it actually work without using tableadapters or parameters etc?

        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();


looks like a simple error in your query..

if User is a string (don't know about the rest) it should be simple quoted

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

Also, the oledbcommand receive a connection object, not a connection string. try with

OleDbCommand ins = new OleDbCommand(insertString, objConnection );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜