如下提示mycmd.ExecuteNonQuery();出错,求问是什么原因?

protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection mycoon;
OleDbCommand mycmd;
mycoon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + MapPath("App_Data/student.mdb"));
mycoon.Open();

DateTime mydt = new DateTime();
mydt.ToString("yyyy-MM-dd");
mycmd = new OleDbCommand("INSERT INTO [liuyanban] (name,text,time) VALUES ('" + this.Label4.Text + "','" + this.TextBox1.Text + "','" + mydt + "')", mycoon);
mycmd.ExecuteNonQuery();
Response.Write("<script>alert(\"留言成功!\")</script>");

提示mycmd.ExecuteNonQuery();出错..原因是INSERT INTO 语句的语法错误。
求高手指教

PIPIONE
浏览 270回答 3
3回答

一只斗牛犬

protected void Button1_Click(object sender, EventArgs e){OleDbConnection mycoon;OleDbCommand mycmd;mycoon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|App_Data|\student.mdb"));mycoon.Open();DateTime mydt = new DateTime();mydt.ToString("yyyy-MM-dd");sqlString=string.Format("insert into liuyanban(name,text,time) values ('{0}','{1}','{2}')",this.Label4.Text,this.TextBox1.Text,mydt);mycmd = new OleDbCommand(sqlString, mycoon);mycmd.ExecuteNonQuery();Response.Write("<script>alert(\"留言成功!\")</script>");

守着星空守着你

text 和time都是关键字 要放在[]中正确的写法mycmd = new OleDbCommand("INSERT INTO [liuyanban] (name,[text],[time]) VALUES ('" + this.Label4.Text + "','" + this.TextBox1.Text + "','" + mydt + "')", mycoon);

FFIVE

this.Label4.Text 和 this.TextBox1.Text 里面是否有单引号?给他们加个Replace("'","''")试试如果还有问题请吧这个SQL的语句整个发上来看看,不是这种拼装的,是他的真正的SQL语句
打开App,查看更多内容
随时随地看视频慕课网APP