在做一个系统,正在做登陆部分,因为经常要连接数据库,就自己写了个类
 public class SQLcon
    {
        private string con = "server=(local);user id=sa;password=sa;database=student";        public SqlConnection ConnectToDatabase()
        {
            return new SqlConnection(con);
        }
        public bool Login(string uid, string pwd, SqlConnection mycon)     //判断是否登陆成功
        {
            if (mycon.State != ConnectionState.Open)
            {
                MessageBox.Show("数据库未打开");
                try
                {
                    mycon.Open();
                    SqlCommand login = new SqlCommand();
                    string str = " select UserID from UserLogin where UserName='" + uid + "' and PassWord ='" + pwd + "' ";
                    login.CommandText = str;
                    login.Connection = mycon;
                    SqlDataReader result = login.ExecuteReader();
                    mycon.Close();
                    if (result.HasRows)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据库异常!请重试!");
                    return false;
                }
            }
            else
            {
                MessageBox.Show("数据库已经打开");
                mycon.Close();
                return false;
            }
        }    }而登陆按钮        private void LoginBotton_Click(object sender, EventArgs e)
        {
            bool login = false;
            SQLcon conn = new SQLcon();
            SqlConnection newCon = conn.ConnectToDatabase();
            login = conn.Login(ID.Text.Trim(), Code.Text.Trim(),newCon);
            if (login)
            {
                MainForm form1 = new MainForm();
                form1.Show();
            }
            else
            {
                MessageBox.Show("登陆失败!");
            }        }
执行之后,try部分没有执行,直接到catch 部分,弹出 数据库异常
请大家帮忙找出问题,谢谢

解决方案 »

  1.   

    把 mycon.Close();这句移到finally{}中
    try
                    {
                        mycon.Open();
                        SqlCommand login = new SqlCommand();
                        string str = " select UserID from UserLogin where UserName='" + uid + "' and PassWord ='" + pwd + "' ";
                        login.CommandText = str;
                        login.Connection = mycon;
                        SqlDataReader result = login.ExecuteReader();
                         if (result.HasRows)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("数据库异常!请重试!");
                        return false;
                    }
    finally
    {                   mycon.Close();}
      

  2.   

    先分析错误提示是什么,再来问,另外你sql中的username和password之类的加上[],以免和保留字冲突
      

  3.   

    string str = " select count(1) from UserLogin where UserName='" + uid + "' and PassWord ='" + pwd + "' ";用SqlCommand.ExecuteScalar 方法
      

  4.   

    应该是 try还是执行了,要不到不了catch块。
    是不是 mycon.Open(); 出错了。在catch里面设断点或者记日志。把 ex.Message + ex.StackTrace 贴出来
      

  5.   

    运,没仔细看,2楼应该可行,你用的是 SqlDataReader。下面用法肯定出错:先关连接,再用 SqlDataReader:     mycon.Close();                     
         if (result.HasRows)