A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马刘俊 中级黑马   /  2013-1-18 09:58  /  1569 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1.            string userName = txtUserName.Text;
  2.             string password = passwordBox.Password;
  3.             string sql = "select * from T_user where name = @un  ";
  4.             SqlParameter parameter = new SqlParameter ( "@un", userName );                     
  5.             SqlDataAdapter sda;  //申明一个适配器
  6.             DataSet ds = new DataSet ( );               //申明一个dataset
  7.             string constr = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;//获取连接字符串
  8.             using ( SqlConnection conn = new SqlConnection ( constr ) )  //建立连接对象
  9.             {
  10.                 conn.Open ( );                            //打开连接
  11.                 using ( SqlCommand cmd = new SqlCommand ( ) )  //建立连接对象
  12.                 {
  13.                     cmd.Connection = conn;
  14.                     cmd.CommandText = sql;
  15.                     cmd.Parameters.Add ( parameter );    //增加参数
  16.                     sda = new SqlDataAdapter (cmd);//使用命令对象作为参数创建适配器
  17.                     SqlCommandBuilder builder = new SqlCommandBuilder ( sda ); //使用sqlcommandbuilder对象为适配器构建命令对象
  18.                     sda.Fill ( ds );      //使用适配器填充dataset对象ds
  19.                 }
  20.             }
  21.             DataTable table = ds.Tables[0];   //获取dataset中的表
  22.              if ( table.Rows.Count<=0 )          //判断表中是否有数据,如果没有,提示用户名不存在
  23.             {
  24.                 MessageBox.Show ( "用户名不存在");
  25.                 return;
  26.             }
  27.             else if ( table.Rows.Count>1 )    //如果表中数据大于1条,提示用户名重复了
  28.             {
  29.                 MessageBox.Show ( "用户名重复" );
  30.                     return;
  31.             }
  32.             else if ( (Boolean)table.Rows[0]["IsLock"] ==true ) //表中只会有1条数据,但是已经被锁定,则通知用户锁定,等待解锁后再次登录
  33.             {
  34.                 MessageBox.Show ( "用户已锁定,请稍后再试" );
  35.                 return;
  36.             }
  37.          else{                               //用户名存在,且不重复,而且也未锁定  则判断密码是否正确
  38.                 DataRow row = table.Rows[0];
  39.                 string Dbpassword = ( string ) row["password"];
  40.                 if ( Dbpassword == password )      //密码正确,提示登录成功,并将dataset中的ErrorTime 字段设置为0
  41.                 {
  42.                     MessageBox.Show ( " 登陆成功");
  43.                     row["ErrorTime"] = 0;
  44.                     table.AcceptChanges ( ); //接收表更新
  45.                     sda.Update ( ds );// 通过适配器将更新传送回服务器
  46.                  }
  47.                 else                  //密码不正确 提示登录失败,并将ErrorTime 字段加1
  48.                 {
  49.                     MessageBox.Show ( " 登陆失败" );
  50.                     row["ErrorTime"] = (int)row["ErrorTime"]+1;
  51.                     if ( ( int ) row["ErrorTime"] >= 3 )  //如果加1后ErrorTime 字段的值大于 或等于3 则将IsLock字段设置为true,锁定用户,并将当前时间放入LockTime字段。
  52.                     {
  53.                         row["IsLock"] = true;
  54.                         row["LockTime"] = DateTime.Today;
  55.                         //table.AcceptChanges ( );
  56.                         //sda.Update ( table );
  57.                     }
  58.                     table.AcceptChanges ( );   //表更新
  59.                     sda.Update ( ds );     //提交更新到数据库
  60.                     
  61.                 }
  62.       }
复制代码
代码如上,运行后没有报任何错误,但是数据库的表始终没有更新呢,求指导哪里写错了。

评分

参与人数 1技术分 +1 收起 理由
潘梦军 + 1

查看全部评分

6 个回复

倒序浏览
额,没人指点下么
回复 使用道具 举报
你的没更新 是指 不会增加错误次数吗?
回复 使用道具 举报
是的 ,dataset中可以更新到,但是数据库不跟新
回复 使用道具 举报
table里面add了row了吗?
回复 使用道具 举报
我不明白   
table里面add了row了吗

是什么意思,能说详细点么,谢谢
回复 使用道具 举报
马锋 中级黑马 2013-1-27 09:43:57
7#
对DataSet的修改都是在内存中的,没有提交到数据库,可以new一个SqlCommandBuilder,再把你的适配器放进去,再更新..
不过你不觉得用ExecuteNonQuery更新更简单吗..

评分

参与人数 1技术分 +1 收起 理由
潘梦军 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马