我在aspx建立了两个textbox和一个button,然后准备单机button提交数据写入access的info数据库里德student表里。public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =E:/info.mdb";
OleDbConnection myConnection = new OleDbConnection(connStr);
string cmd = "INSERT INTO student('"+TextBox1.Text+"','"+TextBox2.Text+"')";
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(cmd,myConnection); myCommand.ExecuteNonQuery();
}
}运行成功,点击button提交也没问题,但是我打开数据库,里面什么也没有,求教。
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =E:/info.mdb";
OleDbConnection myConnection = new OleDbConnection(connStr);
string cmd = "INSERT INTO student('"+TextBox1.Text+"','"+TextBox2.Text+"')";
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(cmd,myConnection); myCommand.ExecuteNonQuery();
}
}运行成功,点击button提交也没问题,但是我打开数据库,里面什么也没有,求教。
OleDbConnection myConnection=null;
try
{
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =E:/info.mdb";
myConnection= new OleDbConnection(connStr); string cmd = "INSERT INTO student('"+TextBox1.Text+"','"+TextBox2.Text+"')";
myConnection.Open();
OleDbCommand myCommand = new OleDbCommand(cmd,myConnection); myCommand.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
finally
{
myConnection.Close();
}
insert的语法应该是:
INSERT INTO 表 (字段1, 字段2) VALUES ('值1','值2')
这句写错了
string cmd="INSERT INTO student values('"+TextBox1.Text+"','"+TextBox2.Text+"')";
string cmd = "INSERT INTO student('"+TextBox1.Text+"','"+TextBox2.Text+"')";
因为是所有插入,所以我没写字段了啊我后来改了一下string cmd = "INSERT INTO student(name,gender)VALUES('"+TextBox1.Text+"','"+TextBox2.Text+"')";还是没写进去
连接字符串
在Button1_Click的开头加一句
throw new Exception("进来了");
看看是什么效果
ACCESS数据库的字段最好用[]-->[字段名]