Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Lecture 4 C# With SQL 3ed Class

The document is a lecture on creating a login project using C# with an SQL database, presented by Dr. Yousif A. Hamad. It includes code snippets for establishing a database connection, validating user credentials, and retrieving object information based on an ID entered in a textbox. The project demonstrates basic database operations within a Windows Forms application.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 4 C# With SQL 3ed Class

The document is a lecture on creating a login project using C# with an SQL database, presented by Dr. Yousif A. Hamad. It includes code snippets for establishing a database connection, validating user credentials, and retrieving object information based on an ID entered in a textbox. The project demonstrates basic database operations within a Windows Forms application.

Uploaded by

yousif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‫س‬ ‫ح‬‫ل‬‫ا‬ ‫ل‬ ‫ع‬ ‫س‬‫ق‬ ‫م‬ ‫ل‬ ‫مع‬‫كل عل الح س تكن ل جي ال‬

‫ م وم ا وب‬/ ‫ية وم ا وب و و و ا و اب‬


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();
}
}

You might also like