从c#.net添加记录以访问数据库时出错

从c#.net添加记录以访问数据库时出错

问题描述:

代码:

    int MaxRows = 0;
    int inc = 0;

    private void Form2_Load(object sender, EventArgs e)
    {
        con = new System.Data.OleDb.OleDbConnection();
        ds1 = new DataSet();

        con.ConnectionString = " Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Documents and Settings/user/My Documents/anchu.accdb";
        string sql = "SELECT * From Table1";
        da = new System.Data.OleDb.OleDbDataAdapter(sql, con);

        con.Open();

        da.Fill(ds1, "Table1");
        //NavigateRecords();

        con.Close();
        //con.Dispose();
    }
    /*private void NavigateRecords()
    {
        DataRow drow = ds1.Tables["Table1"].Rows[0];

        textBox1.Text = drow.ItemArray.GetValue(0).ToString();
        textBox2.Text = drow.ItemArray.GetValue(1).ToString();
        textBox3.Text = drow.ItemArray.GetValue(2).ToString();
        textBox4.Text = drow.ItemArray.GetValue(3).ToString();
    }*/

    private void groupBox1_Enter(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbCommandBuilder cb;
        cb = new System.Data.OleDb.OleDbCommandBuilder(da);

        DataRow drow = ds1.Tables["Table1"].NewRow();
        drow[0] = textBox1.Text;
        drow[1] = textBox2.Text;
        drow[2] = textBox3.Text;
        drow[3] = textBox4.Text;

        ds1.Tables["Table1"].Rows.Add(drow);

        MaxRows = MaxRows + 1;
        inc = MaxRows - 1;
        da.Update(ds1, "Table1");

        MessageBox.Show("Entry Added");
    }
}

}

我在该行中遇到错误:

da.Update(ds1,"Table1");

da.Update(ds1, "Table1");

错误说..OleDBException未处理

The error says..OleDBException was unhandled

并在其下方显示..INSERTINTO语句中的语法错误.

and below that it says..Syntax error in INSERT INTO statement.

我也正确连接了访问数据库.

I have connected my access database correctly as well.

我的表格格式如下

FIELD NAME
序号(主键)
名称
公司名称
联络电话

FIELD NAME
Sno.(primary key)
Name
Company Name
Contact Number

数据类型 文本 文本 文本 文字

Datatype Text Text Text Text

可以是/两者都是:

1)打开和关闭更新语句周围的连接

1)open and close a connection around the update statement

2)设置适配器的updatecommand属性.请参见 msdn 页面

2)set the updatecommand property of the adapter. see msdn page