0% found this document useful (0 votes)
130 views3 pages

Using System

The document discusses connecting to a SQL database and performing CRUD operations. It opens a connection to the Northwind database, selects all products data and displays it in a datagrid. It also includes code to insert a new product record on a button click by getting the values from textboxes and executing a nonquery command. Similarly it shows updating the datagrid with newly inserted data.

Uploaded by

cokitam_923734
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
130 views3 pages

Using System

The document discusses connecting to a SQL database and performing CRUD operations. It opens a connection to the Northwind database, selects all products data and displays it in a datagrid. It also includes code to insert a new product record on a button click by getting the values from textboxes and executing a nonquery command. Similarly it shows updating the datagrid with newly inserted data.

Uploaded by

cokitam_923734
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 3

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; using System.Data.SqlClient; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //ilk olarak bi baglant ayoruz SqlConnection baglanti =new SqlConnection(); baglanti.ConnectionString = "server=lab4-hoca;user=sa;pwd=a;database=Northwind;"; //connection string ile hangi bilgisayarn hangi data baseine hangi kullanc ad we ifryle baglanlacagn belirler sqlConn.Open();//baglant kurulur SqlCommand sqCom = new SqlCommand(); sqCom.Connection = sqlConn; sqCom.CommandText = "select * from products"; sqCom.CommandType = CommandType.Text; sqCom.ExecuteScalar(); DataTable dtProd = new DataTable(); SqlDataAdapter sqDa = new SqlDataAdapter(); sqDa.SelectCommand = sqCom;

//data adapter e bu komutun selectmi updatemi insertmi deletemi oldugunu belirlemeliyim sqDa.Fill(dtProd);// data table doldurur dataGridView1.DataSource = dtProd;// gridde gostermeyi saglar } private void button2_Click(object sender, EventArgs e) { SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = "server=lab4-hoca;user=sa;pwd=a;database=Northwind;"; //connection string ile hangi bilgisayarn hangi data baseine hangi kullanc ad we ifryle baglanlacagn belirler sqlConn.Open();//baglant kurulur SqlCommand sqCom = new SqlCommand(); sqCom.Connection = sqlConn; sqCom.CommandText = "insert into Products (ProductName,UnitPrice) values ('" + textBox1.Text + "'," + numericUpDown1.Value.ToString() + ")"; ; sqCom.CommandType = CommandType.Text; sqCom.ExecuteNonQuery(); button1_Click(sender, e); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { //ilk olarak bi baglant ayoruz SqlConnection sqlConn = new SqlConnection(); sqlConn.ConnectionString = "server=lab4-hoca;user=sa;pwd=a;database=Northwind;"; //connection string ile hangi bilgisayarn hangi data baseine hangi kullanc ad we ifryle baglanlacagn belirler sqlConn.Open();//baglant kurulur SqlCommand sqCom = new SqlCommand(); sqCom.Connection = sqlConn; sqCom.CommandText = "select * from products"; sqCom.CommandType = CommandType.Text; sqCom.ExecuteScalar();

DataTable dtProd = new DataTable(); SqlDataAdapter sqDa = new SqlDataAdapter(); sqDa.SelectCommand = sqCom; //data adapter e bu komutun selectmi updatemi insertmi deletemi oldugunu belirlemeliyim sqDa.Fill(dtProd);// data table doldurur dataGridView1.DataSource = dtProd;// gridde gostermeyi saglar

SqlConnection baglanti =new SqlConnection(); baglanti.ConnectionString="Data Source=ARELIK\\SQLEXPRESS;Initial Catalog=okul;Integrated Security=True"; baglanti.Open(); SqlCommand sorgu = new SqlCommand(); sorgu.Connection = baglanti; sorgu.CommandText="insert into ogrenci (Numara,Ad,Soyad,Bkod) values('" + tbNumara.Text + "','" + tbAd.Text + "','" + tbSoyad.Text + "','" + tbBkod.Text + "')"; sorgu.CommandType = CommandType.Text; sorgu.ExecuteNonQuery(); bKaydet_Click(sender, e);

You might also like