C#·
未将对象引用设置到对象的实例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();
		}
出错的语句是红色字体的那段代码。谢谢帮忙解决学习
------解决方案--------------------把这段代码
C# code
//连接对象 
string connectionString = @"Data Source=蒙奇'D'路飞\SURETRUENO;Initial Catalog=Student;Integrated Security=SSPI"; 
SqlConnection sqlCon = new SqlConnection(connectionString); 
sqlCon.Open(); //打开数据库连接
------解决方案--------------------
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();