C# - INSERT INTO语句中的语法错误(System.Data.dll中发生了'System.Data.OleDb.OleDbException'类型的未处理异常)

时间:2016-04-17 15:20:04

标签: c# database oledb unhandled-exception oledbconnection

    System.Data.OleDb.OleDbCommandBuilder cb;   //command builder
    cb = new System.Data.OleDb.OleDbCommandBuilder(boosterbaseDa); //re-establish temporary connection 

    DataRow dRow = boosterbaseDs.Tables[drpCardSet.Text].NewRow();  //create new row in set

    dRow[0] = drpCardSet.Text + "-EN" + txtCardSetNo.Text; //grab the data
    dRow[1] = txtCardName.Text;
    dRow[2] = drpCardRarity.Text;
    dRow[3] = drpCardCatagory.Text;
    dRow[4] = updCardInStock.Value;

    MessageBox.Show(dRow[0] + ", " + dRow[1] + ", " + dRow[2] + ", " + dRow[3] + ", " + dRow[4]);

    boosterbaseDs.Tables[drpCardSet.Text].Rows.Add(dRow);   //add row

    boosterbaseMaxRows = boosterbaseMaxRows + 1;

    string cardTable = drpCardSet.Text;

    boosterbaseDa.Update(boosterbaseDs, drpCardSet.Text); 

我似乎被困在最后一行,错误为

  

System.Data.dll中出现未处理的“System.Data.OleDb.OleDbException”类型异常,INSERT INTO语句中出现语法错误

我意识到有类似的问题,但我严重坚持这一点。

该表确实存在,我可以建立与数据库的连接。

进入(进入表“LOB”)的数据示例包括LOB-EN001,Bob,Common,Monster,1。

提前谢谢你 - 山姆

1 个答案:

答案 0 :(得分:0)

这是一种解决方案。

string connetionString = null;
OleDbConnection connection;
OleDbDataAdapter oledbAdapter = new OleDbDataAdapter();
string sql = null;
connetionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=../boosterbase.mdb";
connection = new OleDbConnection(connetionString);
sql = "insert into " + drpCardSet.Text + " values('" + drpCardSet.Text + "-EN" + txtCardSetNo.Text + "','" + txtCardName.Text + "','" + drpCardRarity.Text + "', '" + drpCardCatagory.Text + "', '" + updCardInStock.Text + "')";
try
{
    connection.Open();
    oledbAdapter.InsertCommand = new OleDbCommand(sql, connection);
    oledbAdapter.InsertCommand.ExecuteNonQuery();
    MessageBox.Show("Row(s) Inserted !! ");
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

来自http://csharp.net-informations.com/dataadapter/insertcommand-oledb.htm

通过Update requires a valid InsertCommand when passed DataRow collection with new rows