问题描述
我有登录表单,我想检查数据库中的用户名和密码。当我提交提交时我收到的错误是ExcuteReader:Connction Proprty尚未初始化
我尝试过:
private void btnOK_Click(object sender,EventArgs e)
{
if(txtUserName.Text ==)
{
MessageBox.Show(请输入用户名,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if(txtPassword.Text ==)
{
MessageBox.Show(请输入密码,错误 ,MessageBoxButtons.OK,MessageBoxIcon.Error);
txtPassword.Focus();
return;
}
试试
{
SqlConnection con = new SqlConnection(Data Source = ADMIN; Initial Catalog = SIS; Persist Security Info = True; User ID = sa; Password = admin @ 123 );
SqlCommand cmd = new SqlCommand(选择用户名,来自User_TB的User_Password,其中username ='+ txtUserName.Text +'和password ='+ txtPassword +');
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read()== true)
{
int i;
ProgressBar1.Visible = true;
ProgressBar1.Maximum = 5000;
ProgressBar1.Minimum = 0;
ProgressBar1.Value = 4;
ProgressBar1.Step = 1;
for(i = 0;我< = 5000; i ++)
{
ProgressBar1.PerformStep();
}
this.Hide();
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
i have login form,i want to check username and password from database.when i give the Submit i am getting error that is "ExcuteReader:Connction Proprty has not been initialized"
What I have tried:
private void btnOK_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if (txtPassword.Text == "")
{
MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPassword.Focus();
return;
}
try
{
SqlConnection con = new SqlConnection("Data Source=ADMIN;Initial Catalog=SIS;Persist Security Info=True;User ID=sa;Password=admin@123");
SqlCommand cmd=new SqlCommand("Select Username,User_Password from User_TB where username='"+txtUserName.Text+"' and password='"+txtPassword+"'");
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read()==true)
{
int i;
ProgressBar1.Visible = true;
ProgressBar1.Maximum = 5000;
ProgressBar1.Minimum = 0;
ProgressBar1.Value = 4;
ProgressBar1.Step = 1;
for (i = 0; i <= 5000; i++)
{
ProgressBar1.PerformStep();
}
this.Hide();
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
推荐答案
ExcuteReader:Connction Proprty has not been initialized
现在看看你的代码:
Now look at your code:
SqlConnection con = new SqlConnection("...");
SqlCommand cmd=new SqlCommand("Select ...");
SqlDataReader dr=cmd.ExecuteReader();
你没有将SqlConnection传递给SqlCommand!
尝试:
You don't pass the SqlConnection to the SqlCommand!
Try:
using (SqlCommand cmd=new SqlCommand("Select ...", con))
{
...
}
还有一件事:在尝试使用之前打开连接!
And one other thing: Open the connection before you try to use it!
这篇关于如何从数据库中读取用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!