س حلا ل ع سق م ل معكل عل الح س تكن ل جي ال
م وم ا وب/ ية وم ا وب و و و ا و اب
3ed Class
Subject: C# with SQL
Lecturer: Dr. Yousif A. Hamad
Lecture No 4
# Login Project Using C# code with SQL database
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginFormWithDB
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data
Source=(localdb)\\MSSQLLocalDB;Initial
Catalog=LoginDB;Integrated Security=True;");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from LoginTB
where UN=@AB and Pass= @B ", con);
cmd.Parameters.AddWithValue("@AB", textBox1.Text);
cmd.Parameters.AddWithValue("@B", textBox2.Text);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Form2 f2 = new Form2();
this.Hide();
f2.Show();
}
else
MessageBox.Show("User name or Password not correct");
con.Close();
}
}
}
# How to search for object information using textbox-based ID and display the information over their
textbox as shown in windows.
private void textBox5_Search (object sender, EventArgs e)
{
if (textBox5.Text! = "")
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from TB1 where
ID=@Id ", con);
cmd.Parameters.AddWithValue("@Id", textBox5.Text);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
textBox1.Text = sdr.GetValue(0).ToString();
textBox2.Text = sdr.GetValue(1).ToString();
textBox3.Text = sdr.GetValue(2).ToString();
textBox4.Text = sdr.GetValue(3).ToString();
}
con.Close();
}
}