示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            string        connectionstring;
            SqlConnection conn;

            connectionstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\jak78\Desktop\OnlineFoodOrder\OnlineFoodOrder\Database1.mdf;Integrated Security=True";
            conn             = new SqlConnection(connectionstring);
            try
            {
                SqlCommand cmd = new SqlCommand("select * from Users where Username=@Username and Password =@Password", conn);
                cmd.Parameters.AddWithValue("@Username", textBox1.Text);
                cmd.Parameters.AddWithValue("@Password", textBox2.Text);
                conn.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet        ds    = new DataSet();
                adapt.Fill(ds);
                int     count = ds.Tables[0].Rows.Count;
                DataRow dr    = ds.Tables[0].Rows[0];
                if (count == 1)
                {
                    var x = dr["userid"];
                    us = Convert.ToInt32(x);
                    MessageBox.Show("Login Successful!");
                    if (textBox1.Text.Equals("admin"))
                    {
                        this.Hide();
                        var frm = new Admin();
                        frm.Show();
                    }
                    else
                    {
                        this.Hide();
                        var frm = new UserHome();
                        frm.Show();
                    }
                }
                else
                {
                    MessageBox.Show("Login Failed!");
                }
            }
            //}
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }