0% found this document useful (0 votes)
8 views

Lecture 3 C# With SQL 3ed Class

The document outlines steps for creating a database using SQL Server Management Studio and connecting it with Visual Studio C#. It includes code snippets for searching, deleting, displaying data in a DataGridView, browsing images, and inserting data into the database using various input methods. The lecture is part of a C# with SQL course taught by Dr. Yousif A. Hamad for 3rd class students.

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)
8 views

Lecture 3 C# With SQL 3ed Class

The document outlines steps for creating a database using SQL Server Management Studio and connecting it with Visual Studio C#. It includes code snippets for searching, deleting, displaying data in a DataGridView, browsing images, and inserting data into the database using various input methods. The lecture is part of a C# with SQL course taught by Dr. Yousif A. Hamad for 3rd class students.

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/ 8

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

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


3ed Class

Subject: C# with SQL

Lecturer: Dr. Yousif A. Hamad


Lecture No 3
How to create Database using SQL Server Management studio and connect
with Visual studio C#
Step 1- copy and save the server’s name

Step 2- Create a new Database


Step 3- Create a new Table

Step 4- Identifying the table columns and save


- Command Search in DataGrid View using ID, Name, or any
info that have been saved in the table.

private void Search_Click(object sender, EventArgs e)


{
// Search the data into dataGview
con.Open();

SqlCommand cmd = new SqlCommand("select * from TB1 where ID=@ID ",


con);
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

da.Fill(dt);

dataGridView1.DataSource = dt;

con.Close();

- Command Delete Selected Item in DataGridView


{ con.Open();

int SelRow =
Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);

SqlCommand cmd = new SqlCommand("delete TB1 Where ID=@ID", con);

cmd.Parameters.AddWithValue("@ID", SelRow);

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("Data has been deleted");


}
- Display selected row from datagridview to textbox
private void dataGridView1_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
if(e.RowIndex !=-1)
{
DataGridViewRow dgvr = dataGridView1.Rows[e.RowIndex];
textBox1.Text = dgvr.Cells[0].Value.ToString();
textBox2.Text = dgvr.Cells[1].Value.ToString();
textBox3.Text = dgvr.Cells[2].Value.ToString();
textBox4.Text = dgvr.Cells[3].Value.ToString();

}
}
- To Browse Image and set the uploaded image in the
same size of Picture box follow this code:

How to set the size mode of the Image in PictureBox


private void BrowseImage_Click(object sender, EventArgs e)
{
// Stretches the image to fit the pictureBox.
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif;
*.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
}}

- Insert data into database through Combo Box,


dateTimePicker, Radio Button, Image, Picture Box, and Date.
using System.Data.SqlClient;

using

conn.Open();

con.Open();
string BDay = dateTimePicker1.Value.ToString();
string City = comboBox1.SelectedItem.ToString();
string FN = textBox1.Text.ToString();
string LN = textBox2.Text.ToString();
string gen;
if (radioButton1.Checked == true)
gen = radioButton1.Text;
else
gen = radioButton2.Text;
MemoryStream ms1 = new MemoryStream();
//pictureBox1.Image.Save(ms1,
System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] img_arr1 = new byte[ms1.Length];
ms1.Read(img_arr1, 0, img_arr1.Length);

SqlCommand cmd = new SqlCommand("insert into MyInfo


values('" + FN + "','" + LN + "','" + gen + "','"+BDay+"','"
+ img_arr1 + "','" + City + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Done");
}
}

- insert into specific columns into database

string query = "INSERT INTO MyTBL (FN, LN, Age, Img) VALUES (‘" + strFN +"’, ‘"
+strLN +"’, ‘"+ intAge+"’, ‘" + ByteImg +”’)";

You might also like