首页 新闻 会员 周边

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

0
悬赏园豆:50 [已解决问题] 解决于 2011-07-15 15:50

       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;
           
        }

问题补充:

报出的异常是

未处理的“System.FormatException”类型的异常出现在 winform.exe 中。

其他信息: 输入字符串的格式不正确。

飞翔的蚂蚁的主页 飞翔的蚂蚁 | 初学一级 | 园豆:131
提问于:2011-07-15 15:18
< >
分享
最佳答案
0

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+"'";

收获园豆:50
冰 鱼 | 菜鸟二级 |园豆:282 | 2011-07-15 15:47
非常感谢,问题解决了。 谢谢
飞翔的蚂蚁 | 园豆:131 (初学一级) | 2011-07-15 15:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册