Sqlserver bağlantısı c#

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 WindowsFormsApplicationbaglanti
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlbaglanti = new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
sqlbaglanti.Open();
// textBox1.Text = “baglandi”;
MessageBox.Show(“Bağlandı”);
sqlbaglanti.Close();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection myconn = new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
string myInsertQuery = “INSERT INTO stok (id,adi,tur) Values(’1′, ‘hacı’,'yatar’)”;
SqlCommand mycmd = new SqlCommand(myInsertQuery);
mycmd.Connection = myconn;
myconn.Open();
mycmd.ExecuteNonQuery();
MessageBox.Show(“Eklendi”);
myconn.Close();
}
public DataTable SelectKisiler_ToTable()
{
SqlConnection z = new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
string sql = “SELECT * FROM stok”;
SqlDataAdapter dak = new SqlDataAdapter(sql, z);
z.Open();
DataSet ds = new DataSet();
dak.Fill(ds, “table_kisi”);
DataTable dt = ds.Tables[0];
z.Close();
return dt;
}
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = SelectKisiler_ToTable();
}
private void button4_Click(object sender, EventArgs e)
{
SqlConnection baglan = new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
string sql = “DELETE FROM stok WHERE adi LIKE ‘%h%’”;
SqlCommand c = new SqlCommand(sql, baglan);
//  c.Connection = baglan;
baglan.Open();
int k = c.ExecuteNonQuery();
MessageBox.Show(“Silindi”);
baglan.Close();
}
private void button5_Click(object sender, EventArgs e)
{
SqlConnection baglan = new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
string sql = “UPDATE  stok SET tur=’” + textBox1.Text + “‘  WHERE adi=’hacı’”;
SqlCommand c = new SqlCommand(sql, baglan);
c.Connection = baglan;
baglan.Open();
int k = c.ExecuteNonQuery();
MessageBox.Show(“Guncellendi”);
baglan.Close();
//SqlConnection conn =new SqlConnection(@”Data Source= .\SQLEXPRESS; Initial Catalog=FILMS;Integrated Security= True”);
//conn.Open();
//SqlCommand cmd = new SqlCommand(“UPDATE stok SET adi=@NewName, tur=@NewAddress” + ” WHERE id=@Id”, conn);
//    cmd.Parameters.AddWithValue(“@Id”, 1);
//    cmd.Parameters.AddWithValue(“@Name”, “Munna Hussain”);
//    cmd.Parameters.AddWithValue(“@Address”, “Kerala”);
//    int rows = cmd.ExecuteNonQuery();
//    MessageBox.Show(“guncellendi”);
//    conn.Close();
}
}
}

Hiç yorum yok:

Yorum Gönder