示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                String uname, pass, dbuname, dbpass;
                uname = textBox1.Text;
                pass  = textBox2.Text;
                String str = "Data Source=(localdb)\\Projects;Initial Catalog=Database2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";


                String        sql  = "Select username, password from dbo.test where username='******' and password='******'";
                SqlConnection conn = new SqlConnection(str);
                SqlCommand    cmd  = new SqlCommand(sql);
                conn.Open();
                cmd.Connection = conn;
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    dbuname = rdr["username"].ToString();
                    dbpass  = rdr["password"].ToString();
                    if (pass == dbpass)
                    {
                        MessageBox.Show("Invalid Username!!!");
                    }

                    else
                    {
                        LoginForm2 lf2 = new LoginForm2();
                        lf2.Show();
                        this.Hide();
                    }
                }
                conn.Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Boolean err = false;

            if (textBox1.Text == "")
            {
                MessageBox.Show("Set your User Name");
                err = true;
            }

            else if (textBox2.Text == "")
            {
                MessageBox.Show("Enter your name");
                err = true;
            }

            else if (textBox4.Text == "")
            {
                MessageBox.Show("Enter Password");
                err = true;
            }

            else if (textBox4.Text != textBox5.Text)
            {
                MessageBox.Show("Passwords don't match");
                err = true;
            }

            if (err == false)
            {
                try
                {
                    String str = "Data Source=(localdb)\\Projects;Initial Catalog=Database2;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";


                    String sql = "insert into dbo.test (username, password, name, mobile) values ('" + textBox1.Text + "','" + textBox4.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";

                    SqlConnection conn = new SqlConnection(str);

                    conn.Open();

                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.ExecuteNonQuery();

                    //SqlDataAdapter adapter = new SqlDataAdapter();
                    //DataSet ds =new DataSet();
                    //DataRow dr;
                    //dr = ds.Tables[0].NewRow();
                    //dr["username"] = textBox1.Text;
                    //dr["name"] = textBox2.Text;
                    //dr["mobile"] = textBox3.Text;
                    //dr["password"] = textBox4.Text;
                    //ds.Tables[0].Rows.Add(dr);

                    //SqlCommandBuilder builder;
                    //builder = new SqlCommandBuilder(adapter);
                    //adapter = builder.DataAdapter;
                    //adapter.Update(ds,"UserDetails");
                    conn.Close();
                    LoginForm2 lf2 = new LoginForm2();
                    lf2.Show();
                    this.Hide();
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString()); Console.WriteLine(ex.ToString()); }
            }
        }