嗨,我有一个Windows窗体应用程序,需要根据提供的登录证书打开不同的页面。如果提供管理员证书(即用户名=管理员),则称为“AdminPage”的窗体表单应用程序应该被调用为Up。否则应该叫做“Main_Page”的窗体表单应用程序。如何根据登录凭证设置特定的“登录后选项”?
我的代码给我提示“与此命令关联的DataReader仍处于打开状态,必须先关闭它”。
这是我的代码;
try
{
SqlConnection cn = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand("select * from UserCredentials where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", cn);
SqlDataReader dr;
cn.Open();
dr = cmd.ExecuteReader();
int cnt = 0;
while (dr.Read())
{
cnt++;
}
if (cnt == 1)
{
MessageBox.Show("Successful Login...", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
string query = "select Username, Password from UserCredentials where Username='Administrator";
SqlCommand cmdA = new SqlCommand(query, cn);
dr = cmdA.ExecuteReader();
int k = 0;
while(dr.Read())
{
k++;
}
if(k == 1)
{
AdminPage A_P = new AdminPage();
A_P.Tag = this;
A_P.Show(this);
Hide();
}
Main_Page Mp = new Main_Page();
Mp.Tag = this;
Mp.Show(this);
Hide();
cn.Close();
textBox1.Clear();
textBox2.Clear();
}
else
{
MessageBox.Show("Invalid UserName or Password", "Message", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
textBox1.Clear();
textBox2.Clear();
}
}
catch (Exception err)
{
MessageBox.Show(err.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
你很容易被SQL注入,在文本框中放一个'''看看会发生什么。改为使用参数化查询。 –