哪位大侠帮看看,怎么更新和删除不好使呢?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class select : System.Web.UI.Page
{
protected DataSet ds=new DataSet();
SqlDataAdapter adaper = new SqlDataAdapter();
protected DataTable dt;//类中定义的public或protected类型的字段能在aspx中访问
protected DataRow dr;
protected void Page_Load(object sender, EventArgs e) //查询
{
string str="server=JIN;integrated security=True;initial catalog=test";
SqlConnection conn = new SqlConnection(str);
SqlCommand selec=new SqlCommand("select * from mingzi",conn);
SqlCommand inser=new SqlCommand("insert into mingzi(id,namen) values(@id,@namen)",conn);
SqlCommand updat = new SqlCommand("update mingzi set id=@id,namen=@namen",conn);
SqlCommand delet=new SqlCommand("delete * from mingzi where id=@id",conn);
inser.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
inser.Parameters.Add(new SqlParameter("@namen",SqlDbType.NVarChar,10,"namen"));
updat.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
updat.Parameters.Add(new SqlParameter("@namen",SqlDbType.NVarChar,10,"namen"));
delet.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
adaper.SelectCommand = selec;
adaper.InsertCommand = inser;
adaper.UpdateCommand = updat;
adaper.DeleteCommand = delet;
adaper.Fill(ds);
dt=ds.Tables[0];
dr1 = dt.Rows[0];
Button1.Click += new EventHandler(Button1_Click);
Button2.Click += new EventHandler(Button2_Click);
}
void Button2_Click(object sender, EventArgs e) //修改
{
dt.Rows[0][0]= Text1.Value;
dt.Rows[0][1] = Text2.Value;
adaper.Update(ds);
}
void Button1_Click(object sender, EventArgs e) //添加
{
DataRow dr=dt.NewRow();
dr[0] = Text1.Value;
dr[1] = Text2.Value;
dt.Rows.Add(dr);
adaper.Update(ds);
}
protected void Button3_Click(object sender, EventArgs e) //删除
{
dt.Rows[0].Delete();
adaper.Update(ds);
}
}
------解决方案--------------------delete * from mingzi where id=@id
sql语法有误,去掉*
update mingzi set id=@id,namen=@namen
这个也奇怪,难道主键也要更新?而且是没有条件的批量更新?
LZ还是对sql不熟啊
------解决方案--------------------update mingzi set namen=@namen where id=@id一般不更新ID的,也就是唯一标识
------解决方案--------------------你这句:
update mingzi set id=@id,namen=@namen
就是更新全部数据了
update mingzi set id=@id,namen=@namen where + 你要更新行的条件