问题描述
//更新用户信息:参数为根据用户提供的ID更新用户名和密码publicboolupdateData(stringid,stringname,stringpwd){//创建连接对象SqlConnectionconn;Stringstr="DataSource=WIN-U1VRAJH204P;InitialCatalog=information;IntegratedSecurity=True";conn=newSqlConnection(str);//打开链接conn.Open();//创建命令对象SqlCommandcmd;StringstrSql="updateTUsersetsName=@name,pwd=@pwdwheresid=@id";cmd=newSqlCommand(strSql,conn);//设置参数信息并赋值:第一种方法cmd.Parameters.Add("@id",SqlDbType.Char,4);cmd.Parameters["@id"].Value=id;cmd.Parameters.Add("@name",SqlDbType.Char,10);cmd.Parameters["@name"].Value=name;cmd.Parameters.Add("@pwd",SqlDbType.Char,6);cmd.Parameters["@pwd"].Value=pwd;//执行增加信息的命令inti;try{i=cmd.ExecuteNonQuery();}catch(SqlExceptione){returnfalse;}if(i==1){returntrue;}else{returnfalse;}//修改按键privatevoidbutton3_Click(objectsender,EventArgse){if(listView1.SelectedItems.Count!=1){MessageBox.Show("请选择修改的记录,一次只能选择一条记录!");return;}//获取选择的记录ListViewItemitemSelected=listView1.SelectedItems[0];//获取选择的记录值StringstuNo=itemSelected.SubItems[0].Text;StringstuName=itemSelected.SubItems[1].Text;StringstuAge=itemSelected.SubItems[2].Text;gengxingstuInfo=newgengxing();//设置初始化值stuInfo.StuNo=stuNo;stuInfo.StuName=stuName;stuInfo.Stupwd=stuAge;}//修改窗体usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacexueshenlogin{publicpartialclassgengxing:Form{privateStringstuNo;//学号privateStringstuName;//姓名privatestringstupwd;//年龄publicStringStuNo//学号属性{get{returnstuNo;}set{stuNo=value;}}publicStringStuName//姓名属性{get{returnstuName;}set{stuName=value;}}publicstringStupwd//密码属性{get{returnstupwd;}set{stupwd=value;}}publicgengxing(){InitializeComponent();}privatevoidbutton2_Click(objectsender,EventArgse){stuNo=gsid.Text;stuName=gname.Text;stupwd=gpwd.Text;stringUsid,Uname,Upwd;Usid=gsid.Text.Trim();Uname=gname.Text.Trim();Upwd=gpwd.Text.Trim();fengzhuangobj=newfengzhuang();if(obj.updateData(Usid,Uname,Upwd)){MessageBox.Show("更新成功");this.Close();}else{MessageBox.Show("更新失败!");}}//装载privatevoidgengxing_Load(objectsender,EventArgse){InitializeComponentValue();}//初始化学生信息privatevoidInitializeComponentValue(){gsid.Text=stuNo;gname.Text=stuName;gpwd.Text=stupwd;}privatevoidbutton1_Click(objectsender,EventArgse){gsid.Text="";gname.Text="";gpwd.Text="";gsid.Focus();}}}
解决方案
解决方案二:
说清楚什么问题
解决方案三:
怎么打都是更新失败
解决方案四:
解决方案五:
我调试了一下返回SqlExceptionfalse就失败的