1 . อยากได้ตัวอย่าง อย่างง่าย เช่นกดปุ่มแล้ว ลงไปใน dataGridView ประมาณนี้
ครับลองทำตามดูนะครับลาก DataGridView , Button มาใน Form
แล้วดับเบิลคลิกที่ Button แล้วจะ button1_Click เกิดขึ้นมาแล้วเขียนโค้ดดังนี้ ( ก่อนที่จะเขียนโค้ดด้านล่างนี้ให้ ประกาศ using System.Data.SqlClient; ตรงส่วนบนสุดก่อนนะครับ)
private void button1_Click(object sender, EventArgs e)
{
/* สร้างการเชื่อมต่อกับฐานข้อมูล โดย สัญลักษณ์ . ( จุด ) หมายถึงชื่อ instance ของ Sql server ปัจจุบันครับอาจจะใช้ (local)ก็ได้ในกรณีที่ลง instance ตัวเดียวหรือถ้าลง Sql Server Express ด้วยแล้วถ้าต้องการใช้งาน express ก็เขียน .\SqlExpress
SqlConnection con = new SqlConnection(@"Server=.;Database=Northwind;Integrated Security=SSPI");
/* สร้าง Query เพื่อที่จะดึงข้อมูลจาก Table ที่ชื่อ Shippers ที่อยู่ในฐานข้อมูลที่ชื่อ Northwind (สามารถไป Download Northwind Database ได้)
SqlCommand com = new SqlCommand("SELECT * FROM Shippers", con);
con.Open(); //เปิด connection
SqlDataReader reader = com.ExecuteReader(); //สร้าง SqlDataReader เพื่ออ่านข้อมูล
DataTable dt = new DataTable(); //สร้าง DataTable เพื่อที่จะนำข้อมูลที่ได้จาก SqlDataReader มาเก็บเป็น DataTable
dt.Load(reader); //นำข้อมูลใส่ลงไปใน Datable
dataGridView1.DataSource = dt; //นำข้อมูลจาก DataTable ใส่ลงใน dataGridView
con.Close(); //ปิดการเชื่อมต่อ
}
ผลลัพธ์ ดังรูป

2. วิธีการต่อ db
ลองดูตัวอย่างจากข้างบนนะครับผมเขียนไว้แล้ว
3. กดปุ่มแล้ว select ตารางมาลงใน datagridview
ลองดูตัวอย่างจากข้างบนนะครับผมเขียนไว้แล้ว
4. วิธีการ add ข้อมูลอย่างง่าย เช่นกดปุ่มแล้วเพิ่มข้อมูลลงใน DB พร้อมกับ update ตารางในโปรแกรม
ให้เพิ่มอีกปุ่มเข้าไปแล้วดับเบิลคลิกที่ปุ่ม นี้แล้วเขียนโค้ดดังนี้
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server=.\SqlExpress;Database=Northwind;Integrated Security=SSPI");
SqlCommand com = new SqlCommand("INSERT INTO Shippers(CompanyName) VALUES (@data)", con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@data";
param.SqlDbType = SqlDbType.NVarChar;
param.Value = dataGridView1.CurrentRow.Cells[1].Value;
com.Parameters.Add(param);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
ผลลัพธ์จะไดัดังรูป
และในฐานข้อมูลก็จะไม่ข้อมูลเพิ่มเข้ามาดังรูป
ตัวอย่างโค้ดทั้งหมด
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server=.\SqlExpress;Database=Northwind;Integrated Security=SSPI");
SqlCommand com = new SqlCommand("SELECT * FROM Shippers", con);
con.Open();
SqlDataReader reader = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader); //นำข้อมูลใส่ลงไปใน Datable
dataGridView1.DataSource = dt; //นำข้อมูลจาก DataTable ใส่ลงใน dataGridView
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Server=.\SqlExpress;Database=Northwind;Integrated Security=SSPI");
SqlCommand com = new SqlCommand("INSERT INTO Shippers(CompanyName) VALUES (@data)", con);
SqlParameter param = new SqlParameter();
param.ParameterName = "@data";
param.SqlDbType = SqlDbType.NVarChar;
param.Value = dataGridView1.CurrentRow.Cells[1].Value;
com.Parameters.Add(param);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
}
[With great power comes great responsibility]