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

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

慕码人8056858
浏览 562回答 1
1回答

慕标5832272

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+"'";
打开App,查看更多内容
随时随地看视频慕课网APP