private void frmLogin_Load(object sender, System.EventArgs e)
{
//连接对象
string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open(); //打开数据库连接
} private void btnLogin_Click(object sender, System.EventArgs e)
{
//检测用户是否输入用户名
if (this.txtName.Text == string.Empty)
{
MessageBox.Show("请输入用户名!");
this.txtName.Focus();
return;
}
//检测用户是否输入密码
else if (this.txtPwd.Text == string.Empty)
{
MessageBox.Show("请输入密码!");
this.txtPwd.Focus();
return;
}
//读取所填用户名的密码
SqlCommand sqlCom = sqlCon.CreateCommand();
string sql = "SELECT UserName, Password FROM User WHERE (UserName = '" +txtName.Text.Trim()+"')";
                
sqlCom.CommandText = sql;
SqlDataReader sqlRd = sqlCom.ExecuteReader(); //判断是否存在该用户
if (!sqlRd.HasRows)
{
MessageBox.Show("用户名不存在!");
return;
}
                
//读取数据库中的内容,并于当前输入比较
while (sqlRd.Read())
{
//判断用户输入与数据库内容是否匹配
if (sqlRd["Password"].ToString().Trim() != txtPwd.Text.Trim())
{
MessageBox.Show("密码不正确!");
txtName.Focus();
return;
}
else
{
this.Hide();
Student Student=new Student();
Student.ShowDialog();
}
} //关闭数据库连接
sqlRd.Close();
sqlCon.Close();
this.Close();
}出错的语句是红色字体的那段代码。谢谢帮忙解决学习

解决方案 »

  1.   

    把这段代码//连接对象 
    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
    SqlConnection sqlCon = new SqlConnection(connectionString); 
    sqlCon.Open(); //打开数据库连接 
    放在:SqlCommand sqlCom = sqlCon.CreateCommand(); 
    前面试试!!
      

  2.   

    private void frmLogin_Load(object sender, System.EventArgs e) 

    //连接对象 
    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
    SqlConnection sqlCon = new SqlConnection(connectionString); 
    sqlCon.Open(); //打开数据库连接 

    这里面的sqlCon对象的生命周期,只有在这个函数里面,是局部变量
      

  3.   

    恩 是生存期问题 在form_load函数结束后 sqlCon就消亡了 
      

  4.   

    private void btnLogin_Click(object sender, System.EventArgs e)
    {
    //检测用户是否输入用户名
    if (this.txtName.Text == string.Empty)
    {
    MessageBox.Show("请输入用户名!");
    this.txtName.Focus();
    return;
    }
    //检测用户是否输入密码
    else if (this.txtPwd.Text == string.Empty)
    {
    MessageBox.Show("请输入密码!");
    this.txtPwd.Focus();
    return;
    }
    else
    {
    //连接对象
    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI";
    SqlConnection myCon = new SqlConnection(connectionString);
    myCon.Open(); //打开数据库连接
    //读取所填用户名的密码
    SqlCommand myCom = myCon.CreateCommand();
    string sql = "SELECT UserName, Password FROM User WHERE (UserName = N'" +txtName.Text.Trim()+"')";
                    
    myCom.CommandText = sql;
    SqlDataReader myRd = myCom.ExecuteReader(); //判断是否存在该用户
    if (!myRd.HasRows)
    {
    MessageBox.Show("用户名不存在!");
    return;
    }
                    
    //读取数据库中的内容,并于当前输入比较
    while (myRd.Read())
    {
    //判断用户输入与数据库内容是否匹配
    if (myRd["Password"].ToString().Trim() != txtPwd.Text.Trim())
    {
    MessageBox.Show("密码不正确!");
    txtName.Focus();
    return;
    }
    else
    {
    this.Hide();
    Student Student=new Student();
    Student.ShowDialog();
    }
    }
    //关闭数据库连接
    myRd.Close();
    myCon.Close();
    this.Close();
    }
    }
    我把代码修改到登陆按钮后。红色字体的那部分就错误了。。
    想了很久都想不懂。是我的数据库问题还是其他问题呢!?
      

  5.   

    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
    SqlConnection sqlCon = new SqlConnection(connectionString); 
    sqlCon.Open(); //打开数据库连接 
    这种东西要放在构造函数中
      

  6.   

    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
    SqlConnection sqlCon = new SqlConnection(connectionString); 
    sqlCon.Open();这几句不能放在load事件里直接在类下面声明吧
    string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
    SqlConnection sqlCon = new SqlConnection(connectionString); 然后在要用的地方打开连接sqlCon.Open();