使用microsoft访问在数据库中添加数据

时间:2013-02-05 04:46:26

标签: c# winforms oledb

我真的无法在我的数据库中添加任何数据,我可以阅读数据库中的内容但无法写入,我已经尝试了很多代码但是没有工作:(我最终得到了这段代码,

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string user = textBox1.Text;
                string pass = textBox2.Text;

                string myExecutequery = "INSERT INTO Accountstbl (Username,Password) VALUES (@user,@pass)";
                OleDbCommand cmd = new OleDbCommand(myExecutequery, con);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

错误说:

System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   at VirginiTEAcorp.Form3.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\12-014s\My Documents\applications\Database\WindowsFormsApplication1\Form3.cs:line 31
有人帮助我:(

2 个答案:

答案 0 :(得分:0)

string myExecutequery = "INSERT INTO Accountstbl (Username,Password) VALUES 
(@user,@pass)";

con.Open();

try
{
 OleDbCommand cmd = new OleDbCommand(myExecutequery, con);
 cmd.Parameters.Add("@user", user);
 cmd.Parameters.Add("@pass", pass);     
 cmd.ExecuteNonQuery();
 cmd.Dispose();
 cmd = null;
}
catch(Exception ex)
{
  throw new Exception(ex.ToString(), ex);
}
finally
{
 con.Close();
}

您的查询应该是这样的

string myExecutequery = "INSERT INTO Accountstbl (Username,Password) VALUES 
('"+ user +"','"+ pass +"')";

您需要在添加 oleDBCommand 之前打开连接,如

con.Open();

答案 1 :(得分:0)

试一试。

       string SqlString = "Insert Into Accountstbl (Username, Password) Values (@user, @pass)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\test.accdb")) //Your Connection string
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                string use = userTextBox.Text;
                string pass = passTextBox.Text;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@use", use);
                cmd.Parameters.AddWithValue("@pass", pass);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

我知道这应该可行,但我在Microsoft Access 2012中测试了这段代码,我得到了这个错误:

Syntax error in INSERT INTO statement.

这是MSAccess BUG: "Syntax Error in INSERT INTO..." on ADO Recordset.Update

的错误