修改数据库里的数据SQL语法错误.???!
现在我做了两个页面。第一个updata_list.aspx用了一个标签接受来自数据库的数据,并按照标题显示出来,代码如下。没有错误.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 update_list : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string constring = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "//db.mdb";
OleDbConnection con = new OleDbConnection(constring);
string sql = "select * from newsdb ";
OleDbCommand com = new OleDbCommand(sql, con);
com.Connection.Open();
OleDbDataReader dr;
dr = com.ExecuteReader();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<table ");
while (dr.Read())
{
sb.Append("<tr> <td><a href='update.aspx?id=" + dr[0] + "'>" + dr[1] + "</a></td></tr>");
}
sb.Append("</table>");
Label1.Text = sb.ToString();
}
}
然后又建立了一个页面updata.aspx.接受来自updata_list.aspx的id值,并且想修改数据。但被提示SQL语法错误!请大家帮忙看看哪个地方出错了!!谢谢了~~~updata.aspx代码如下:
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 update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string cid = Request.QueryString["id"];
string constring = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "//db.mdb";
OleDbConnection con = new OleDbConnection(constring);
string sql = "select * from newsdb where id= " + cid;
OleDbCommand com = new OleDbCommand(sql, con);
com.Connection.Open();
OleDbDataReader dr;
dr = com.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr[1].ToString();
TextBox2.Text = dr[2].ToString();
TextBox3.Text = dr[3].ToString();
TextBox4.Text = dr[4].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string constring = "provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath(".") + "//db.mdb";
OleDbConnection con = new OleDbConnection(constring);
con.Open();
string sql = "update newsdb set title='" + TextBox1.Text + "', set sj='" + TextBox2.Text + "',set come='" + TextBox3.Text + "',set content='" +
TextBox4.Text + "' ";
Response.Write(sql );
OleDbCommand com = new OleDbCommand(sql, con);
com.ExecuteNonQuery();
Panel1.Visible = false;
Panel2.Visible = true;
Label1.Text = "新闻修该成功!";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("update_list.aspx");
}
}