为了账号安全,请及时绑定邮箱和手机立即绑定

异常错误 ,请大侠帮忙瞧瞧

异常错误 ,请大侠帮忙瞧瞧

慕码人8056858 2018-12-07 02:51:48
       private void button1_Click(object sender, EventArgs e)        {            string uname;            string upwd;             uname = textBox1.Text.ToString();             upwd = textBox2.Text;             int num=1;            int db;            db = getDB(uname, upwd);             if (num == db)            {                MessageBox.Show("全部成功", "好");            }            else             {                MessageBox.Show("NO", "好");            }         }         public static int getDB(string uname,string upwd)         {            SqlConnection con = new SqlConnection("server=XPSP2USER;uid=sa;pwd=sasa;database=demoDB");             con.Open();             string sql = "select * from utable where id='"+uname+"'and pwd='"+upwd+"'";             SqlCommand com = new SqlCommand(sql,con);                                               MessageBox.Show("数据库连接成功","好的");            int yz;           try            {               yz = Convert.ToInt16(com.ExecuteScalar());                             }            catch(Exception e){               throw ;                   }                        con.Close();             return yz;                    }
查看完整描述

1 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

yz = Convert.ToInt16(com.ExecuteScalar());

com.ExecuteScalar()这个是返回第一行第一列啊,现在估计返回的不是数字,所以转成int出错了。

把这个sql语句改下:

string sql = "select * from utable where id='"+uname+"'and pwd='"+upwd+"'";

改成:string sql = "select count(*) from utable where id='"+uname+"'and pwd='"+upwd+"'";

查看完整回答
反对 回复 2019-01-21
  • 1 回答
  • 0 关注
  • 561 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信