我无法通过文本框在ms访问数据库中插入数据

时间:2013-02-27 06:50:07

标签: c# ms-access-2007 oledb

我有一个数据库,我使用MS ACCESS 2007.我想通过文本框将数据插入数据库。我在下面有这个代码,但是我遇到了错误。

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Sisc-stronghold\mis!\wilbert.beltran\DataBase\DataStructure.accdb");

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO Accountstbl (Username, Password)" + "VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";
                cmd.Parameters.AddWithValue("@Username", textBox1.Text);
                cmd.Parameters.AddWithValue("@Password", textBox2.Text);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                textBox1.Text = ex.ToString();
            }

这是错误

System.Data.OleDb.OleDbException:INSERT INTO语句中的语法错误。    在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)    在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object& executeResult)    在System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)    在System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior,Object& executeResult)    在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)    在System.Data.OleDb.OleDbCommand.ExecuteNonQuery()    at VirginiTEAcorp.Form3.button1_Click(Object sender,EventArgs e)位于C:\ Documents and Settings \ 12-014s \ My Documents \ applications \ Database \ WindowsFormsApplication1 \ Form3.cs:line 34

2 个答案:

答案 0 :(得分:1)

您已声明参数但未使用它们,如果您使用以下语句,则无需使用参数

cmd.CommandText = "INSERT INTO Accountstbl (Username, Password)" + "VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";

但是如果你想使用参数,那么你需要改变命令文本,如下所示......

cmd.CommandText = "INSERT INTO Accountstbl (Username, Password) VALUES (@UserName,@Password)";

答案 1 :(得分:1)

也许你可以试试这段代码

 private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO userinfo (FirstName, LastName, Age, Address, Course)" + "VALUES (@First_Name, @Last_Name, @Age, @Address, @Course)";
            cmd.Parameters.AddWithValue("@First_Name", textBox1.Text);
            cmd.Parameters.AddWithValue("@Last_Name", textBox2.Text);
            cmd.Parameters.AddWithValue("@Age", textBox3.Text);
            cmd.Parameters.AddWithValue("@Address", textBox5.Text);
            cmd.Parameters.AddWithValue("@Course", textBox5.Text);
            cmd.Connection = conn;
            conn.Open();
            clear();
            cmd.ExecuteNonQuery();
            {
                MessageBox.Show("Data Added!");
                conn.Close();
            }
            dataholder();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

如果您要声明参数。你可能不得不在某种程度上使用它们。希望它有所帮助:)