用户代码未处理OleDbException
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\数据库\\135.mdb";
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.JEt.OLEDB.4.0;Data Source=" + strCon + ";");
myConn.Open();
if (myConn.State == ConnectionState.Open)
{
Label1.Text = "Access数据库打开";
myConn.Close();
}
if (myConn.State == ConnectionState.Closed)
{
Label2.Text = "Access数据库关闭";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\数据库\\135.mdb";
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.JEt.OLEDB.4.0;Data Source=" + strCon + ";");
string strsql = "insert into 135.mdb_表1(姓名,年龄)values ('" + TextBox1 + "','" + TextBox2 + "')";
OleDbCommand comm = new OleDbCommand(strsql, myConn);
if (myConn.State == ConnectionState.Closed)
{
myConn.Open();
}
if (Convert.ToInt32(comm.ExecuteNonQuery()) >0)//这是出错的地方
Label3.Text = "添加成功";
}
else
{
Label4.Text = "添加失败";
}
myConn.Close();
}
}
无法运行,提示用户代码未处理OleDbException,insert into 语法错误
求教啊
------解决方案--------------------TextBox1.Text
TextBox2.Text
------解决方案-------------------- string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\\数据库\\135.mdb";
OleDbConnection myConn = new OleDbConnection("Provider = Microsoft.JEt.OLEDB.4.0;Data Source=" + strCon + ";");
连接字符串有问题,怎么有两个Provider=Microsoft.Jet.OLEDB.4.0
其次,sql语句有问题
string strsql = "insert into 135.mdb_表1(姓名,年龄)values ('" + TextBox1 + "','" + TextBox2 + "')";
TextBox1,TextBox2 怎么把控件的ID传进去了呢?
改成
extBox1.Text TextBox2.Text
------解决方案--------------------楼上的已经说得很清楚了
------解决方案--------------------