想在一个Info数据库(access)中的Class表中添加记录,就是执行不了!
提示错误原因:未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中。
请高手看看,错误在何处!
代码:
private void button1_Click(object sender, System.EventArgs e)
{ if(textBox1.Text==""&&textBox2.Text=="")
{
MessageBox.Show("所有项都是必须填写的!");
return;
} string myConStr="Provider=Microsoft.Jet.OLEDB.4.0;";
myConStr+="Data Source=Info.mdb;";
OleDbConnection myCon=new OleDbConnection(myConStr);
myCon.Open();

string InsertSQL = "INSERT INTO [Class]([Name],[State])" 
+ "VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
OleDbCommand myCom=new OleDbCommand(InsertSQL,myCon);
//ExcuteSQL(InsertSQL);
myCom.CommandText=InsertSQL;
myCom.ExecuteNonQuery();
myCon.Close();
}

解决方案 »

  1.   

    数据库路径要用绝对路径 暂且假设你放在程序运行目录,调试时候记载bin/debug,你可以修改为你真是的路径,改了一下,你试试private void button1_Click(object sender, System.EventArgs e)
    { if(textBox1.Text==""&&textBox2.Text=="")
    {
    MessageBox.Show("所有项都是必须填写的!");
    return;
    } string myConStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\Info.mdb;";
    OleDbConnection myCon=new OleDbConnection(myConStr);
    myCon.Open();

    string InsertSQL = "INSERT INTO [Class]([Name],[State])" 
    + " VALUES('" + textBox1.Text + "','" + textBox2.Text + "')";
    OleDbCommand myCom=new OleDbCommand(InsertSQL,myCon);
    //ExcuteSQL(InsertSQL);
    //myCom.CommandText=InsertSQL;
    myCom.ExecuteNonQuery();
    myCon.Close();
    }
      

  2.   

    应该是你拼出来的sql有问题,跟踪下看看你执行的sql是否正确
      

  3.   

    "VALUES('"  改为" VALUES('"还有 Data Source=Info.mdb 要指明数据库所在的绝对路径 、或相对路径