为什么会出现connection没有初始化的错误提示?

wozuihaole wozuihaole 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题如题目…………
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();}
}
}
}
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部