.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

ช่วยสอนวิธีติดต่อกับ DB โดยใช้ SQL server 2005 ตัวเต็ม กับ C# 2005 ตัวเต็มหน่อยครับ บน winApp Options · View
picky2hop
Posted: Tuesday, August 26, 2008 4:26:58 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 8/26/2008
Posts: 1

-อยากได้ตัวอย่าง อย่างง่าย เช่นกดปุ่มแล้ว ลงไปใน dataGridView ประมาณนี้

-วิธีการต่อ db

-กดปุ่มแล้ว select ตารางมาลงใน datagridview

-วิธีการ add ข้อมูลอย่างง่าย เช่นกดปุ่มแล้วเพิ่มข้อมูลลงใน DB พร้อมกับ update ตารางในโปรแกรม

 

ขอขอบคุณล่วงหน้าครับ

paedotnet
Posted: Sunday, August 31, 2008 11:16:08 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

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]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by