using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtsno.Focus();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
OleDbConnection con1 = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("~\\app_data\\studentdb_Backup.mdb"));
OleDbCommand c1 = new OleDbCommand();
string strVal =","+ txtsno.Text + "," + txtsname.Text + "," + txtsex.Text + "," + txtclassno.Text + "," + txtaddress.Text;
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values("+strVal+")";
//OleDbCommand cmd = new OleDbCommand("insert into tblstudent(学号,姓名,性别,班级,地址)values(@sno,@sname,@sex,@classno,@address)", con1);
OleDbCommand insCom = new OleDbCommand(strIns, con1);
OleDbDataAdapter da = new OleDbDataAdapter();
con1.Open();
da.InsertCommand = insCom;
da.InsertCommand.ExecuteNonQuery();
con1.Close();
Response.Write("<script language=javascript>alert(''新纪录添加成功,请单击”返回“回到主页面!);</script>");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtsno.Focus();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
OleDbConnection con1 = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("~\\app_data\\studentdb_Backup.mdb"));
OleDbCommand c1 = new OleDbCommand();
string strVal =","+ txtsno.Text + "," + txtsname.Text + "," + txtsex.Text + "," + txtclassno.Text + "," + txtaddress.Text;
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values("+strVal+")";
//OleDbCommand cmd = new OleDbCommand("insert into tblstudent(学号,姓名,性别,班级,地址)values(@sno,@sname,@sex,@classno,@address)", con1);
OleDbCommand insCom = new OleDbCommand(strIns, con1);
OleDbDataAdapter da = new OleDbDataAdapter();
con1.Open();
da.InsertCommand = insCom;
da.InsertCommand.ExecuteNonQuery();
con1.Close();
Response.Write("<script language=javascript>alert(''新纪录添加成功,请单击”返回“回到主页面!);</script>");
}
}
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values("+strVal+")";
这句有错误吧!多一个分号!其它的没看,你把错误信息贴出来啊,string strVal =txtsno.Text + "’,‘" + txtsname.Text + "’,‘" + txtsex.Text + "’,‘" + txtclassno.Text + "’,‘" + txtaddress.Text;
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values(‘"+strVal+"’)";看我改的,删了第一个单引号,加了好多的单引号。其它的错误没看(代码太乱了,看不下去),只是最明显的看到了这个,
{
OleDbConnection con1 = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("~\\app_data\\studentdb_Backup.mdb"));
OleDbCommand c1 = new OleDbCommand();
string strVal =txtsno.Text + "’,‘" + txtsname.Text + "’,‘" + txtsex.Text + "’,‘" + txtclassno.Text + "’,‘" + txtaddress.Text;
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values(‘"+strVal+"’)";
OleDbCommand insCom = new OleDbCommand(strIns, con1);
try
{
con1.Open();
insCom.ExecuteNonQuery();
Response.Write("<script language=javascript>alert(''新纪录添加成功,请单击”返回“回到主页面!);</script>");
}catch{}
finally
{con1.Close();}
}
string strIns="insert into tblstudent(学号,姓名,性别,班级,地址)values("+strVal+")";=============》
要注意空格跟字段的类型,这里假设你所有的字段都是字符类型的string strIns=string.Format("insert into tblstudent(学号,姓名,性别,班级,地址)
values('{0}','{1}','{2}','{3}','{4}')",txtsno.Text,txtsname.Text,txtsex.Text,txtclassno.Text,txtaddress.Text);