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

Lecture 5 2nd Course C#

The document provides an overview of advanced C# programming concepts, focusing on database operations such as inserting records and searching data using DataGrid View. It details two methods for inserting records into a table: simple queries and parameterized queries, emphasizing the security benefits of parameterized queries. Additionally, it includes code examples demonstrating how to insert data and copy records from one table to another in a SQL database.

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

Lecture 5 2nd Course C#

The document provides an overview of advanced C# programming concepts, focusing on database operations such as inserting records and searching data using DataGrid View. It details two methods for inserting records into a table: simple queries and parameterized queries, emphasizing the security benefits of parameterized queries. Additionally, it includes code examples demonstrating how to insert data and copy records from one table to another in a SQL database.

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

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

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


3rd Class – 2nd Course

Subject: Programming Language II (Advance C#)

Lecturer: Dr. Yousif A. Hamad


Lecture No 5

1
C# Objects (controls)
1-Command Search in DataGrid View
2-Command Insert into Table in Database
3-Copy data from one table to another table

INSERT ROW IN TABLE


The keyword INSERT INTO is used for inserting records in a table. There are 2 ways to insert records in
a table.

1. Insert using Simple Query


2. Insert using Parameterized Query

2
Direct Insert Record in a Table
This is the easiest way to insert records into a table but I strongly avoid it because it is less secure than
parameterized query.
Syntax

1. INSERT INTO table_name (column1, column2, column3, ...)


2. VALUES (value1, value2, value3, ...);

or,

1. INSERT INTO table_name


2. VALUES (value1, value2, value3, ...);

- Example of Search in DataGrid View using textbox


using System;
using System.Data.SqlClient;

namespace StudentinfoSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//connection string
SqlConnection con = new SqlConnection("Data source
=(localdb)\\ProjectsV13; Initial catalog=StudentDBSystem;Integrated
security=true");
private void Button3_Click(object sender, EventArgs e)
{
//command

con.Open();
SqlCommand cmd = new SqlCommand("select * from StudentInfo where
fname=@A", con);
cmd.Parameters.AddWithValue("@A",textBox1.Text);

SqlDataAdapter sdr = new SqlDataAdapter(cmd);


DataTable dt = new DataTable();

3
sdr.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}

- Example 1 of Insert (Save) Data from different


objects to table
using System;
using System.Data.SqlClient;
namespace StudentinfoSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//connection string

SqlConnection con = new SqlConnection("Data source =(localdb)\\ProjectsV13; Initial


catalog=StudentDBSystem;Integrated security=true");

private void Button4_Click(object sender, EventArgs e)


{
if (textBox2.Text != "" && textBox3.Text != "")
{
int IDD = int.Parse(textBox2.Text);
string fn = textBox3.Text;
string ln = textBox4.Text;
int Age1 = int.Parse(textBox5.Text);
string stage1 = comboBox1.SelectedItem. ToString ();
int ph1 = int.Parse(textBox7.Text);

con.Open();
//command

SqlCommand cmd = new SqlCommand("insert into StudentInfo values('" + IDD + "','" + fn +


"','" + ln + "','" + Age1 + "','" + stage1 + "','" + ph1 + "')", con);

cmd.ExecuteNonQuery();

con.Close();

4
// Following command to update the info in data grid view (function of Show data)

button1.PerformClick();
MessageBox.Show("Data has been inserted");
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox7.Text = "";
comboBox1.Text = "";

}
else
{
MessageBox.Show("check the contains");
}
}

- Example 2 of Insert (Save) Data from different


objects to table
using System;
using System.Data.SqlClient;

namespace InsertRecords
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;Initial Catalog=ComputerShop;Integrated Security=True");
//Replaced Parameters with Value
string query = "INSERT INTO Products (Name, Price, Date)
VALUES(@Name, @Price, @Date)";
SqlCommand cmd = new SqlCommand(query, con);

//Pass values to Parameters


cmd.Parameters.AddWithValue("@Name", "USB Keyboard");
cmd.Parameters.AddWithValue("@Price", "$20");
cmd.Parameters.AddWithValue("@Date", "25 May 2017");

try
{
con.Open();

5
cmd.ExecuteNonQuery();
Console.WriteLine("Records Inserted Successfully");
}
catch (SqlException e)
{
Console.WriteLine("Error Generated. Details: " + e.ToString());}
finally
{
con.Close();

} }}

Copy from one to another table

1. INSERT INTO Items(Column1,Column2,Column3) SELECT Column1,Column2,Colum


n3 FROM Produ

- Example 1 of copy info from one to another table

using System;
using System.Data.SqlClient;

namespace InsertRecords
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection(@"Data
Source=.\SQLEXPRESS;Initial Catalog=ComputerShop;Integrated Security=True");
//Replaced Parameters with Value

6
string query = "INSERT INTO Items(Name,Price,Date) SELECT
Name,Price,Date FROM Products";
SqlCommand cmd = new SqlCommand(query, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
Console.WriteLine("Records Inserted Successfully");
}
catch (SqlException e)
{
Console.WriteLine("Error Generated. Details: " +
e.ToString());
}
finally
{
con.Close();
Console.ReadKey();
}
}
}
}

Output

Records Inserted Successfully

You might also like