以下是代码
protected void Button3_Click(object sender, EventArgs e)
{
try
{
string a = Session["用户名"].ToString();
SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
con.Open();
string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text + "',QQ号码='" + TextBox1.Text + "',备注='" + TextBox3.Text + "' where 学号='" + a + "'";
SqlCommand com = new SqlCommand(UpateSql, con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("Select * From reader", con);
DataSet ds = new DataSet();
ada.Fill(ds);
con.Close();
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}
}
执行之后没错,也提示修改成功,但是数据库的表里没有更新。我想实现的是更新reader表里的三列,其他列都不动,不知道代码有没有写错,高手帮忙看看了。在线等
protected void Button3_Click(object sender, EventArgs e)
{
try
{
string a = Session["用户名"].ToString();
SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
con.Open();
string UpateSql = "Update reader SET 电子邮箱='" + TextBox2.Text + "',QQ号码='" + TextBox1.Text + "',备注='" + TextBox3.Text + "' where 学号='" + a + "'";
SqlCommand com = new SqlCommand(UpateSql, con);
com.ExecuteNonQuery();
SqlDataAdapter ada = new SqlDataAdapter("Select * From reader", con);
DataSet ds = new DataSet();
ada.Fill(ds);
con.Close();
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}
}
执行之后没错,也提示修改成功,但是数据库的表里没有更新。我想实现的是更新reader表里的三列,其他列都不动,不知道代码有没有写错,高手帮忙看看了。在线等
你那成功提示总归都是提示的
=======
单步调试 查看UpateSql 的值,确定参数都有值
将UpateSql 的值复制到SQL查询分析器中执行一下 检查语句错误
int n=com.ExecuteNonQuery();
判断n是否大于0
检查你的学号是不是字符文本类型,如果是纯数字,建议用int
要判断是否修改成功,改一下:
int AffectRows = com.ExecuteNonQuery();
if(AffectRows>0)
//成功
else
//失败
DataSet ds = new DataSet();
ada.Fill(ds);仅仅是更新? 你要这个干嘛?
DataSet ds = new DataSet();
ada.Fill(ds);是有什么用的啊。更新之后又查出来所有?
是指这个吗?
按5楼去做!放一个断点即可 获取到sql语句 你在sql查询分析器里执行一下 就什么都明白了
那就 .trim()一下 去掉空格这个有可能的 比如 数据库字段长度一定 你空格太多 超出长度 无法更新
你现在只有获取sql语句 然后在查询分析器里 去执行 才知道真正的原因!
学会用断点调试程序。
protected void Button3_Click(object sender, EventArgs e)
{
int i = 0;
try
{
string a = Session["用户名"].ToString();
SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
con.Open();
string UpateSql = "Update reader SET 电子邮箱='" + (TextBox2.Text).Trim() + "',QQ号码='" + (TextBox1.Text).Trim() + "',备注='" + (TextBox3.Text).Trim() + "' where 学号='" + a + "'";
SqlCommand com = new SqlCommand(UpateSql, con);
i= com.ExecuteNonQuery(); //接收一下值,如果大于0 就是更新成功了; if (i > 0)
{
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}
}
catch
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}
}试试这段代码把。不行就设个断点,一走就明白了。
很正常,也许没有符合条件的记录给你修改,比如update student set name='test' where id=3,假如没有id=3的记录也可以操作成功,只是受影响的行数为0而已,可以用字段最好不要用中文
检查你的学号是不是字符文本类型,如果是纯数字,建议用int
要判断是否修改成功,改一下:
int AffectRows = com.ExecuteNonQuery();
if(AffectRows>0)
//成功
else
//失败另外给个建议,字段名最好不要用中文
2 如果不能更新,在 com.ExecuteNonQuery();这儿设置int i= com.ExecuteNonQuery();再把i的值打印出来,看是否大于0,如果大于0则说明更新成功了
3 还是不行,检查sql语句,在sql server 2000中,用查询分析器,把你的语句写到里面,值都用固定的,看能不能成功,不能就是sql语句写错了,检测from where等前后是不是有空格(一般很容易忽略的),还有字段是不是都是varchar类型的,有的是数字
能说的就这么多了、、、还有一点,如果你是点了编辑,到这个页面修改,那么应该是根据id获取的信息显示在textbox里面的,这个时候根据id获取的信息应该写在page_load里面的if(IsPostBack){}里面,不然更新不了、、、
com.ExecuteNonQuery()的值判断过了,是大于0的。
写的UPDATE语句在查询分析器里试过了可以更新。最后红字的不太明白,能详细说下吗
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.SqlClient;public partial class 个人信息 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strconn = ConfigurationManager.AppSettings["connStr"];
SqlConnection conn = new SqlConnection(strconn);
string strsql = "";
string a = Session["用户名"].ToString();
conn.Open();
strsql = "select * from reader where 学号='" + a + "'";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
Response.Write("没有记录");
}
else
{
Label1.Text = ds.Tables[0].Rows[0]["学号"].ToString();
Label2.Text = ds.Tables[0].Rows[0]["姓名"].ToString();
Label3.Text = ds.Tables[0].Rows[0]["政治面貌"].ToString();
Label4.Text = ds.Tables[0].Rows[0]["性别"].ToString();
Label5.Text = ds.Tables[0].Rows[0]["专业班级"].ToString();
Label6.Text = DateTime.Parse(ds.Tables[0].Rows[0]["入学时间"].ToString()).ToShortDateString();
Label7.Text = ds.Tables[0].Rows[0]["手机号码"].ToString();
Label8.Text = ds.Tables[0].Rows[0]["虚拟网"].ToString();
TextBox1.Text = ds.Tables[0].Rows[0]["QQ"].ToString();
TextBox2.Text = ds.Tables[0].Rows[0]["dzyx"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["bz"].ToString();
}
conn.Close(); } protected void Button3_Click(object sender, EventArgs e)
{
int i = 0;
try
{
string a = Session["用户名"].ToString();
SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
con.Open();
string UpateSql = "Update reader SET dzyx='" + (TextBox2.Text).Trim() + "',QQ='" + (TextBox1.Text).Trim() + "',bz='" + (TextBox3.Text).Trim() + "' where 学号='" + a + "'";
SqlCommand com = new SqlCommand(UpateSql, con);
i = com.ExecuteNonQuery(); //接收一下值,如果大于0 就是更新成功了; if (i > 0)
{
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
} }
catch
{
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}我把这个CS文件的所有代码都贴上来吧。想实现的是登录之后,根据用户名读取该用户的信息,用户可以修改自己的QQ、电子邮箱和备注。
{
if (!this.Page.IsPostBack)
{
this.BindInfoById(id);//调用方法,通过id获取信息到页面
} this.DataBind();
}
protected void BtnMod_Click(object sender, EventArgs e)//按钮
{
this.DataUpdate(id);//调用方法,修改数据
}
我是说你要这样写,
不然容易造成数据回滚,就是你那样更新成功了,可是数据库里面还是没有更新