如何在登录选项后设置特定的""这取决于登录凭据?

时间:2016-09-21 14:59:11

标签: c#

您好我有一个Windows窗体应用程序,需要打开不同的页面,具体取决于提供的登录凭据。如果提供了管理员凭据(即Username = Administrator),则窗口表单应用程序称为" AdminPage"应该打电话给。另外一个名为" 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);
            }

1 个答案:

答案 0 :(得分:0)

try { 

SqlConnection con = new SqlConnection(“Data Source = PV10 \ LOCALSERVER; Initial Catalog = SmallSoftwareDB; Integrated Security = True; Pooling = False”); con.Open(); string Lg =“SELECT Username,Password FROM UserCredentials where Username ='”+ textBox1.Text +“'AND Password ='”+ textBox2.Text +“'”; SqlCommand cmd = new SqlCommand(Lg,con); SqlDataReader博士; dr = cmd.ExecuteReader(); int t = 0; while(dr.Read()){t ++; } if(t == 1){MessageBox.Show(“Successful Login”,“”,MessageBoxButtons.OK,MessageBoxIcon.Information); ){AdminPage Ap = new AdminPage(); Ap.Tag =这个; Ap.Show(本); textBox1.Clear(); textBox2.Clear();隐藏(); } else {Main_Page Mp = new Main_Page(); Mp.Tag =这个; Mp.Show(本); textBox1.Clear(); textBox2.Clear();隐藏(); } else {MessageBox.Show(“无效的用户名或密码”,“”,MessageBoxButtons.RetryCancel,MessageBoxIcon.Stop); textBox1.Clear(); textBox2.Clear(); catch(Exception ex){MessageBox.Show(ex.Message,“”,MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Warning); }}