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

C# Codes No.1: Using Using Using Using Using Using Using Using Sing

This C# code uses SQL commands to perform CRUD operations on a database table. It connects to a SQL database, displays data in a grid view, and includes coded buttons to insert, delete, update, and search records. The buttons execute SQL statements and refresh the grid view with the updated data.

Uploaded by

sasuke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C# Codes No.1: Using Using Using Using Using Using Using Using Sing

This C# code uses SQL commands to perform CRUD operations on a database table. It connects to a SQL database, displays data in a grid view, and includes coded buttons to insert, delete, update, and search records. The buttons execute SQL statements and refresh the grid view with the updated data.

Uploaded by

sasuke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C# codes No.

1
SQL ‫نوسينى سیسته‌می‬ -1
‌‫نوسينى ناوي پرۆژه‌كه‬ -2
‫ دا‬#C ‫ له‌ گه‌ل‬SQL ‫كؤدى به‌ستنه‌وه‌ی‬ -3
Grid view ‌‫بينيني داتا له‬ -4
View button`s code -5
Insert button`s code -6
Delete button`s code -7
update button`s code -8
Search button`s code -9

Using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 1

using System.Data.SqlClient;
namespace student 2
{
public partial class Form1 : Form 3
{

SqlConnection con = new SqlConnection(@"Data


Source=.\SQLEXPRESS;AttachDbFilename=c:\users\mazi\documents\visual
studio 2010\Projects\supas\supas\Database1.mdf;Integrated
Security=True;User Instance=True");
4
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select *from Hay1", con);
sda.Fill(dt);

Page 1
dataGridView1.DataSource = dt;
}
private void form1_load(object sender, EventArgs e)
{
5
}

private void button1_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select *from student",
con);
sda.Fill(dt);
dataGridView1.DataSource = dt
} 6

private void button2_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("insert into
student(ID, Name)values('" + textBox1.Text + "','" + textBox2.Text +
"')", con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
7
MessageBox.Show("saved data");
}

private void button3_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("delete from Hay1
where ID ='"+textBox1.Text +"'",con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Delete data");
}

Page 2
8

private void button4_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("Update student
set ID ='"+textBox1.Text +"',Name='"+textBox2.Text+"'",con);
sda.Fill(dt);
dataGridView1.DataSource = dt;
MessageBox.Show("Update data");
}

private void button5_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select *from student where
Name like'"+textBox2.Text+"%'", con);
sda.Fill(dt);

dataGridView1.DataSource = dt;

try
}
textBox2.text = dt.Rows[0]["name"].tostring ();
}
catch(Exception)
{
MessageBox.show("Not Found");
}
}

Page 3

You might also like