为什么会出现connection没有初始化的错误提示?
问题如题目…………
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace student
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnYes_Click(object sender, EventArgs e)
{
string userName = txtName.Text;
string password = txtPwd.Text;
string conString = "Server=.;User ID=sa;Pwd=123;DataBase=student";
SqlConnection connection = new SqlConnection(conString);
string sql = string.Format("select count(*) from [User] where userName='{0}' and password='{1}'",userName,password);
try
{
connection.Open();
SqlCommand command = new SqlCommand();
int num = (int)command.ExecuteScalar();
if (num > 0)
{
MessageBox.Show("欢迎进入成绩管理系统!", "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
MainForm mainForm = new MainForm();
this.Visible = false;
}
else
{
MessageBox.Show("输入的用户名或者密码错误!", "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"操作数据库出错!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{connection.Close();}
}
}
}
[解决办法]