protected void Button2_Click(object sender, EventArgs e)
{OleDbConnectionconn=newOleDbConnection();conn.ConnectionString="Provider=Microsoft.J...
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source = " + Server.MapPath("App_Data/Myphonebook.mdb");
// string strVal = "'" + TextBox1.Text + "','" +ddlSex.SelectedValue+ "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "," + TextBox6.Text + "','" + TextBox3.Text + "'";
string strIns = "insert into 通信录(姓名,性别,年级,专业,籍贯,电话,Email) values('" + TextBox1.Text + "','" +ddlSex.SelectedValue+ "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "," + TextBox6.Text + "','" + TextBox3.Text + "')";
conn.Open();
OleDbCommand insCom = new OleDbCommand(strIns, conn);
// OleDbDataAdapter da = new OleDbDataAdapter();
insCom.ExecuteNonQuery();
//da.InsertCommand = insCom;
// da.InsertCommand.ExecuteNonQuery();
conn.Close();
Response.Write(1111111);
// Response.Write("<script language=javascript>alert('新纪录添加成功,请单击“返回”回到主页面!');</script>");
}
这行代码有错误么,怎么数据存不到Access数据库里面? 展开
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source = " + Server.MapPath("App_Data/Myphonebook.mdb");
// string strVal = "'" + TextBox1.Text + "','" +ddlSex.SelectedValue+ "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "," + TextBox6.Text + "','" + TextBox3.Text + "'";
string strIns = "insert into 通信录(姓名,性别,年级,专业,籍贯,电话,Email) values('" + TextBox1.Text + "','" +ddlSex.SelectedValue+ "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "," + TextBox6.Text + "','" + TextBox3.Text + "')";
conn.Open();
OleDbCommand insCom = new OleDbCommand(strIns, conn);
// OleDbDataAdapter da = new OleDbDataAdapter();
insCom.ExecuteNonQuery();
//da.InsertCommand = insCom;
// da.InsertCommand.ExecuteNonQuery();
conn.Close();
Response.Write(1111111);
// Response.Write("<script language=javascript>alert('新纪录添加成功,请单击“返回”回到主页面!');</script>");
}
这行代码有错误么,怎么数据存不到Access数据库里面? 展开
1个回答
展开全部
应该是数据库路径有错误
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source = " + Server.MapPath("~/App_Data/Myphonebook.mdb");
string strIns = string.Format("insert into 通信录(姓名,性别,年级,专业,籍贯,电话,Email) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", TextBox1.Text, ddlSex.SelectedValue, TextBox2.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox3.Text);
OleDbCommand insCom = new OleDbCommand(strIns, conn);
int result = -1;
conn.Open();
result = insCom.ExecuteNonQuery();
conn.Close();
Response.Write(result);
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source = " + Server.MapPath("~/App_Data/Myphonebook.mdb");
string strIns = string.Format("insert into 通信录(姓名,性别,年级,专业,籍贯,电话,Email) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", TextBox1.Text, ddlSex.SelectedValue, TextBox2.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox3.Text);
OleDbCommand insCom = new OleDbCommand(strIns, conn);
int result = -1;
conn.Open();
result = insCom.ExecuteNonQuery();
conn.Close();
Response.Write(result);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询