C#连接数据库出现ExecuteReader:Connection未初始化
namespacePersonal{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}p...
namespace Personal
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection SqlConnectione1;
private void form1_load(object sender, EventArgs e)
{
string Mycon = "server=9QDTQKLIAHUMMWW\\SQLEXPRESS;database=Personal;uid=sa;pwd=12345678";
SqlConnectione1 = new SqlConnection(Mycon);
SqlConnectione1.Open();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)/*登录*/
{
string str = "select ID,密码 from 登录用户 where ID= '"+ textBox1.Text +" 'and 密码='"+ textBox2.Text +"'";
SqlCommand Mycommand = new SqlCommand(str,SqlConnectione1);
SqlDataReader MyReader = Mycommand.ExecuteReader();
if (MyReader.Read())
{
Form2 t = new Form2();
t.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名不存在或密码错误!\n请重新登录!");
textBox2.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
} 展开
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection SqlConnectione1;
private void form1_load(object sender, EventArgs e)
{
string Mycon = "server=9QDTQKLIAHUMMWW\\SQLEXPRESS;database=Personal;uid=sa;pwd=12345678";
SqlConnectione1 = new SqlConnection(Mycon);
SqlConnectione1.Open();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)/*登录*/
{
string str = "select ID,密码 from 登录用户 where ID= '"+ textBox1.Text +" 'and 密码='"+ textBox2.Text +"'";
SqlCommand Mycommand = new SqlCommand(str,SqlConnectione1);
SqlDataReader MyReader = Mycommand.ExecuteReader();
if (MyReader.Read())
{
Form2 t = new Form2();
t.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名不存在或密码错误!\n请重新登录!");
textBox2.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
} 展开
1个回答
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询