Welcome Guest Search | Active Topics | Members | Log In | Register

การเขียนโปรแกรมติดต่อฐานข้อมูล(SELECT,INSERT,DELETE,UPDATE) Options · View
paedotnet
Posted: Monday, December 24, 2007 6:55:01 PM

Rank: มือเทพ
Groups: Member

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

การเขียนโปรแกรมติดต่อ Database

ในตัวอย่างต่างๆผมจะใช้ ฐานข้อมูล northwind นะครับ โดยการนำ Northwind เข้ามาใน Microsoft sqlserver 2005
ให้เปิด Microsoft SqlServer 2005 ขึ้นมา แล้วไปที่  File->Open->File...
ดังรูปที่


 

จากนั้นจะมีหน้าต่าง Open File เกิดขึ้นมา ให้ไปเลือกไฟล์ Microsoft SQL Server Query File ที่ชื่อ Northwind
จากนั้นคลิกที่ปุ่ม Open จากนั้นก็จะกลับเข้ามาหน้า Query ใน Microsoft Sqlserver ให้เราทำการ execute (หรือกด F5) แล้ว ไป Refresh ตรง folder ที่ชื่อ Databases ที่อยู่ใน Object Explorer (ถ้าไม่มี Object Explorer ก็ไปที่ แถบเมนู ที่ชื่อ View->Object Explorer

หลังจากที่เราได้นำฐานข้อมูลที่ชื่อ Northwind เข้ามาแล้ว ต่อจากนี้เราจะมาเริ่มเขียนโปรแกรมติดต่อฐานข้อมูลกันโดย
ในส่วนนี้ผมจะอธิบายถึง การ select,delete,update,insert,DataTable ฯ

คลาสของ System.Data.SqlClient ได้แก่
SqlConnection  ทำหน้าที่ในการเชื่อมต่อกับฐานข้อมูล
SqlCommand     ทำหน้าที่ในการใช้งานคำสั่ง SQL Query ต่างๆเช่น SELECT,UPDATE,DELETE,INSERT ฯ
SqlDataAdapter ทำหน้าที่เป็นตัวกลางเชื่อมต่อระหว่าง DataSource และ DataSet
SqlDataReader  ทำหน้าทำหน้าที่อ่านข้อมูลที่ได้จากการ Execute คำสั่ง
SqlError       ทำหน้าทีจัดการกับข้อผิดพลาดต่างๆ
SqlParameter   ทำหน้าที่จัดการกับ parameter
SqlTransaction ทำหน้าที่จัดการกับ Transaction ต่างๆ

 

เปิด Visual Studio 2008 หรือ 2005 ขึ้นมา
ไปที่     File->New->Project เลือก Visual C# ->Console Application

ในการเชื่อมต่อฐานข้อมูลนั้นมีสิ่งที่สำคัญคือ
รูปแบบที่จะกำหนดการเชื่อมต่อ

string constr = "Server = (local);Database =Northwind;Integrated Security = SSPI";

ตรง Server = (local) เป็นการกำหนด server ที่จะเชื่อมปกติถ้าเราติดตั้ง instance เดียวเช่น Microsoft SQLServer Express เราจะใช้ (local)/SqlExpress หรือ ./SqlExpress แต่ในตัวอย่างนี้ผมติดตั้งเป็น เวอร์ชั้นเต็ม
ผมก็ใช้ (local) หรือ . แทนก็ได้
ตรง Server เราอาจจะใช้ ตัวอื่นแทนก็ได้เช่น
Data Source = (local)
หรือ
address = (local)
หรือ
network address =(local)
หรือ
addr = (local)

ส่วน Database เป็นการกำหนดชื่อฐานข้อมูลที่ต้องการติดต่อเช่น northwind หรืออาจจะใช้ Initial Catalog ก็ได้
ส่วน Integrated Security เป็นการกำหนด Authentication Mode ค่าของตัวนี้สามารถเป็น true,sspi,no,false,yes หรืออาจจะใช้ Trusted_Connection แทนก็ได้

ตัวอย่างการกำหนดการเชื่อมต่อ

string constr = "Data Source = (local);Initial Catalog = Northwind;Integrated Security = SSPI";
 

 

หรือ
 

string constr = "Data Source = (local);Initial Catalog =northwind;User id = aaa; Password =123";
 

 

หรือ
 

string constr =  "Server =(local);Database = northwind;Trusted_Connection =true";


 

ตัวอย่างการใช้งาน SELECT เพื่อดูข้อมูลจากตาราง Shippers ในฐานข้อมูล Northwind

 

using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
    public static void Main()
    {
        string constr = "Server = (local);Database =Northwind;Integrated Security=SSPI";
        string cmdstr = "SELECT * FROM Shippers";  //กำหนดคำสั่ง sql
        SqlConnection con = new SqlConnection();
        con.ConnectionString = constr;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = cmdstr;
        cmd.Connection = con;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader(); //สร้าง reader เพื่ออ่านข้อมูล
        while (reader.Read())
        {
            Console.WriteLine("ShipperID : {0}\nShipperName : {1}", reader[0], reader[1]);
            Console.WriteLine();
        }
        reader.Close();
        con.Close();
        Console.ReadLine();
    }
}

 

ผลลัพธ์แสดงดังรูป


การใช้งาน ExecuteScalar
ExecuteScalar จะใช้กับ function Scalar ต่างๆเช่น COUNT,MAX,MIN ฯ

ตัวอย่างการนับจำนวนแถวทั้งหมด

using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
    public static void Main()
    {
        SqlConnection con = new SqlConnection("Data Source = .;Database =Northwind;Integrated Security = SSPI");
        SqlCommand com = new SqlCommand("SELECT COUNT(*) FROM Shippers", con);
        con.Open();
        Console.WriteLine(com.ExecuteScalar());
        con.Close();
        Console.ReadLine();
    }
}


 

ตัวอย่างการ หาค่าสูงสุด (MAX)

using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
    public static void Main()
    {
        SqlConnection con = new SqlConnection("Data Source = .;Database =Northwind;Integrated Security = SSPI");
        SqlCommand com = new SqlCommand("SeLECT MAX(Freight) AS Max_Freight FROM Orders", con);
        con.Open();
        Console.WriteLine(com.ExecuteScalar());
        con.Close();
        Console.ReadLine();
    }
}

 

ผลลัพธ์จะได้ดังรูป


การ ใช้งาน ExecuteNonQuery

using System;
using System.Data;
using System.Data.SqlClient;
public class TestInsert
{
    public static void Main()
    {
        SqlConnection con = new SqlConnection("Data Source = .;Database =Northwind;Integrated Security = SSPI");
        SqlCommand com = new SqlCommand(string.Format("INSERT INTO  Shippers VALUES('{0}','{1}')","MyCompanys","(978)333-4322)"));
        com.Connection = con;
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        Console.ReadLine();
    }
}

ผลลัพธ์จะมีแถวใหม่เกิดขึ้นมา


การใช้งาน Parameter
ในการใช้งาน Parameter เราจะกำหนด parameter ต่างๆโดยใช้ @ นำหน้าชื่อ parameter เช่น @name,@lastname

ตัวอย่างการใช้งาน parameter


using System;
using System.Data;
using System.Data.SqlClient;
public class TestInsert
{
    public static void Main()
    {
        Console.WriteLine("Enter Companyname");
        string shippername = Console.ReadLine();
        Console.WriteLine("Enter phone");
        string phone = Console.ReadLine();
        string constr = "Server = (local);Database=northwind;Integrated Security =SSPI";
        string comstr = "INSERT INTO Shippers VALUES(@name,@phone)";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(comstr, con);
        SqlParameter param = new SqlParameter(); //กำหนด parameter
        con.Open();
        param.ParameterName = "@name"; //กำหนดชื่อของ parameter
        param.SqlDbType = SqlDbType.NVarChar; //กำหนดชนิดที่จะเก็บเป็น parameter
        param.Value = shippername; //กำหนดค่าที่จะให้กับ parameter
        SqlParameter param2 = new SqlParameter();
        param2.ParameterName = "@phone";
        param2.SqlDbType = SqlDbType.NVarChar;
        param2.Value = phone;
        cmd.Parameters.Add(param); //ต้องมีคำสั่งนี้เพื่อบอกว่าได้เพิ่มตัวแปรของ parameter เข้าไป
        cmd.Parameters.Add(param2);
        cmd.ExecuteNonQuery(); //ใช้ ExecuteNonQuery() เพื่อ Insert ข้อมูล
        con.Close();
        Console.ReadLine();
    }
}

ผลลัพธ์จะมีแถวใหม่เกิดขึ้นมา


การใช้งาน UPDATE

ตัวอย่างโค้ด

using System;
using System.Data;
using System.Data.SqlClient;
public class TestUpdate
{
    public static void Main()
    {
        string constr = "Server = (local);Database=northwind;Integrated Security =SSPI";
        string comstr = "UPDATE Shippers SET CompanyName = @companyname WHERE Shipperid=5";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand(comstr, con);
        con.Open();
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@companyname";
        param.SqlDbType = SqlDbType.NVarChar;
        param.Value = "aabbcc@zz";
        cmd.Parameters.Add(param);
        cmd.ExecuteNonQuery();
        con.Close();
        Console.ReadLine();
    }
}

 

การเรียกดูข้อมูลโดยใช้ SELECT

ตัวอย่าง

 using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
    public static void Main()
    {
        SqlConnection con = new SqlConnection("Server = .;Database=northwind;Integrated Security=true");
        SqlCommand com = new SqlCommand("SELECT * FROM Shippers;SELECT TOP 10 * FROM Customers", con);//กำหนดให้เลือกมาสองตาราง
        con.Open();
        SqlDataReader reader = com.ExecuteReader();
        Console.BackgroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("Show Shippers Table");
        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + "           " + reader.GetString(1));
        }
        reader.NextResult(); //แสดงตารางถัดไป
        Console.BackgroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("Show Custimers Table");
        while (reader.Read())
        {
            Console.WriteLine(reader[0] + "            " + reader[1]+"              "+reader[2]);
        }
        reader.Close();
        con.Close();
        Console.ReadLine();
    }
}


ผลลัพธ์แสดงดังรูป

 

การ DELETE

 

using System;
using System.Data;
using System.Data.SqlClient;
public class TestDelete
{
    public static void Main()
    {
        Console.WriteLine("Enter ShipperId For deleting");
        int id = Convert.ToInt32(Console.ReadLine());
        string constr = "Address = (local);Initial Catalog = Northwind;Integrated Security = SSPI";
        string comstr = "DELETE  FROM Shippers WHERE ShipperId = @id";
        SqlConnection con = new SqlConnection(constr);
        SqlCommand com = new SqlCommand(comstr, con);
        con.Open();
        com.Parameters.Add("@id", SqlDbType.Int, 4, "ShipperID"); //กำหนด Parameter
        com.Parameters["@id"].Value = id; //กำหนดค่าให้กับ parameter
        com.ExecuteNonQuery();
        con.Close();
    }
}

ผลลัพธ์ให้ใส่ id ที่ต้องการลบลงไป แล้วไปตรวจสอบดูในฐานข้อมูล จะมีข้อมูลถูกลบตาม id ที่เรากำหนด

 

การใช้งาน DataTable

using System;
using System.Data;
using System.Data.SqlClient;
public class TestDataTable
{
    public static void Main()
    {
        string constr = "Server = (local);Database=northwind;Integrated Security = SSPI";
        string comstr = "SELECT * FROM Orders";
        SqlConnection con = new SqlConnection();
        con.ConnectionString = constr;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = comstr;
        cmd.Connection = con;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = reader.GetSchemaTable(); //กำหนด DataTable
        foreach (DataRow r in dt.Rows)
        {
            foreach (DataColumn c in dt.Columns)
            {
                Console.WriteLine("Row : {0}   ,   Col : {1}", r[c], c.ColumnName);
            }
        }
        reader.Close();
        con.Close();
        Console.ReadLine();
    }
}

 

 

 

 



[With great power comes great responsibility]
zerozaaa
Posted: Saturday, June 14, 2008 6:12:50 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

จะแก้ยังให้คับให้ติดต่อ บันทึกข้อมูล ในdatabase ได้คับ ต้องใช้คำสั่งอะไรต่อเหรอคับ ช่วยทีคับ

paedotnet
Posted: Sunday, June 15, 2008 7:58:57 PM

Rank: มือเทพ
Groups: Member

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

ตรงใต้คำสั่ง SqlCommand scm = new SqlCommand(addSql,Conn);

ก็เขียนโค้ดดังนี้

scm.ExecuteNonQuery();
 
 
แต่วิธีนี้จะเป็นวิธีธรรมดานะครับถ้ามีการ insert ข้อมูลจำนวนมากผมแนะนำว่าให้ทำเป็น parameter หรือ Store procedure จะดีกว่า หรือว่าใช้ LINQ ก็จะดีนะครับมันจะสดวกกว่าวิธีนี้


[With great power comes great responsibility]
zerozaaa
Posted: Monday, June 16, 2008 4:01:52 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ผมทำแล้ว มัน  erorr   ครับ น่าจะ ใช้ Parameter เดี๋ยวจะลองดูคับ ขอบคุณคับ

zerozaaa
Posted: Monday, June 16, 2008 4:13:18 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

การเชื่อมต่อ database แบบนี้

string constr = "Server = (local);Database=northwind;Integrated Security = SSPI";

และแบบนี้

 strConn = WebConfigurationManager.ConnectionStrings["computer project"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strConn);

ต่างกันอย่างไรคับ

 

ball
Posted: Monday, June 16, 2008 9:40:23 AM

Rank: อาจารย์
Groups: Administration

Joined: 12/1/2007
Posts: 459
Location: Bangkok

ต่างกันที่ เราจะนำ ConnectionString ไปเก็บเอาไว้ใน web.config ครับ โดยที่ไฟล์ web.config จะมีค่าดังนี้

<connectionStrings>
  <add name="computer project"
       connectionString="Server=(local);Database=northwind;Integrated Security=SSPI"
       providerName="System.Data.SqlClient"/>
</connectionStrings>

แล้วเราก็จะเรียกใชแบบนี้

strConn = WebConfigurationManager.ConnectionStrings["computer project"].ConnectionString;

ซึ่งเป็นวิธีที่ดีกว่าแบบ hard code นะครับ



Imagination is more Important than Knowledge... /A.Einstein
zerozaaa
Posted: Thursday, July 03, 2008 4:48:50 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ช่วยแก้ไข้ทีคับไม่รู้ว่าถูกหรือเปล่าคับ มีติด นิดหนึ่งช่วยทีคับ จะทำระบบ login

mocking1
Posted: Thursday, July 03, 2008 9:46:35 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

ผมคาดว่า น่าจะเกิดจาก if check ลองจาก

if (ds.Tables["register_customer"].Rows.Count = 0)

เป็น

if (ds.Tables["register_customer"].Rows.Count == 0)
zerozaaa
Posted: Thursday, July 03, 2008 2:10:49 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ขอบคุณคับ

zerozaaa
Posted: Friday, July 04, 2008 4:55:02 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ถ้าต้องการ นำข้อมูลจาก DATABASE เข้า TextBox ต้องทำไงคับ อย่างนี้

            string strConn;
            string sqlProducts;
            strConn = WebConfigurationManager.ConnectionStrings["My_data"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strConn);
            Conn.Open();

           sqlProducts = "SELECT  user_cus,pass_cus  FROM register_customer WHERE user_cus = '"+Session["user"]+"'";       // เมื่อSELECT มาแล้วจะนำเข้า TextBox อย่างไรคับ

           SqlDataAdapter da = new SqlDataAdapter(sqlProducts, Conn);

           TextBox1.Text = user_cus;     //แบบนี้มันผิดคับต้องทำยังไงหรือต้องใช้ Parameter คับ ช่วยแนะนำทีคับ

           TextBox2.Text = pass_cus;

mocking1
Posted: Friday, July 04, 2008 10:47:41 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

ผมคิดว่าน่าจะใช้แบนี้นะ

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlProducts, Conn);
ds.Fill(ds);
con.Close();

TextBox1.Text = ds.Table[0].Rows[0][0].ToString();
TextBox2.Text = ds.Table[0].Rows[0][1].ToString();
 

แต่ผมไม่ค่อยแน่ใจนะเพราะยังไม่เคยลอง แต่ได้ผลไงบอกด้วยนะ

zerozaaa
Posted: Friday, July 04, 2008 2:18:08 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ขอบคุณคับ ได้ผลคับ  ช่วยอธิบาย         ds.Table[0].Rows[0][0].ToString();  หน่อยคับว่า  Table[0] และ Rows[0][0] มันเป็นยังไงคับ

 

mocking1
Posted: Monday, July 07, 2008 3:29:25 PM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

zerozaaa wrote:

ขอบคุณคับ ได้ผลคับ  ช่วยอธิบาย         ds.Table[0].Rows[0][0].ToString();  หน่อยคับว่า  Table[0] และ Rows[0][0] มันเป็นยังไงคับ

 

 

ในส่วนของ Table[0] หมายความว่าให้ใช้ Table แรกซึ่ง DataSet สามารถมีได้ หลาย Table เราจะอ้างโดยใช้ชื่อก็ได้ อย่างในตัวอย่าง da.Fill(ds,"customer");  เราก็สามารถอ้างได้ ds.Table["customer"]

 

ส่วน Row[0][0] หมายความว่า ให้เลือกที่ Row ตำแหน่ง แรก(0 คือตำแหน่งแรก) แล้ว 0 ตัวที่ 2 หมายถึง Columns  แรก (0 คือตำแหน่ง คอลัม แรก)

zerozaaa
Posted: Tuesday, July 08, 2008 4:55:50 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ผมทำ Textbox.text โดยที่ ป้อนข้อมูลลงไป คลิกที่ Button แล้วมันจะupdate แล้วมันไม่ยอม update คับ แต่ถ้า มีค่า string ที่ระบุตายตัวไว้มัน update ได้ ช่วยแน่นำทีคับต้องทำไง โค๊ดที่เขียนก็

และอีกโค๊ดหนึ่งที่ใช้ Paramiter  แต่ ก็ ไมได้ผลคับ

แล้วผมต้องควรทำหรือกแก้ไขยังไรคับช่วยแนะนำทีคับ ติดตรงนี้มานานแล้วไม่รู้ไปถามใครคับ

mocking1
Posted: Tuesday, July 08, 2008 9:59:57 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

งงคำถามนิดหน่อยครับ แต่ถ้าเป็นผมตรง sqlUpdate จะใช้

 

sqlUpdate = "UPDATE register_customer SET name_cus = @name_c WHERE id_cus = '"+Tid.Text+"'";
SqlParameter param = new SqlParameter();
param.ParameterName = "@name_c";
param.SqlDbType = SqlDbType.NVarChar; // ต้องตรงกับ Type ด้วย
 
zerozaaa
Posted: Tuesday, July 08, 2008 10:55:53 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

คือปัญหามีอยู่ว่า  ผมได้ select ข้อมูลจาก database ลงที่ Textbox .textและจะเปลียน ค่าโดยไปกรอกข้อมูล ลง ที่Textbox.text ที่ select มา และกดปุ่ม มันจะ update ค่ากับที่ป้อนเข้าไป

และผมได้ทำตามที่บอก แล้ว มันไม่ error นะคับ แต่มันไม่ update ค่า ใหม่ที่ป้อนเข้าไป น่ะคับ แต่ถ้ากำหนดค่า ไว้ที่ตัวแปร แล้ว บันทึกเข้าไปมันก็ update แต่เมื่อเปลียนเป็น ให้ป้อนเข้าไปที่

Textbox.text มันไม่ update คับ

อันนี้ที่ทำไว้คับ แนะนำทีคับ

mocking1
Posted: Tuesday, July 08, 2008 2:06:16 PM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

ผมคิดว่า Type ของ Parameter อาจจะไม่ตรงก็เป็นได้

ball
Posted: Wednesday, July 09, 2008 1:46:39 AM

Rank: อาจารย์
Groups: Administration

Joined: 12/1/2007
Posts: 459
Location: Bangkok

เขียนโค๊ดหนักไปรึเปล่า

ถ้าเป็นเวปทำไมถึงไม่ไปหัดใช้ FormView + SqlDataSource ล่ะครับ



Imagination is more Important than Knowledge... /A.Einstein
mocking1
Posted: Wednesday, July 09, 2008 8:59:41 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 6/26/2008
Posts: 16
Location: thailand

ถ้าเป็นผม คงต้อง debug ดูแล้วล่ะครับ แล้วก็เช็คดูว่า ค่าที่ได้รับถูกต้องหรือเปล่า

nikorn
Posted: Monday, July 14, 2008 2:16:44 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/14/2008
Posts: 9
Location: good

 

ช่วยแก้ให้ทีเถอะ สาธุ

ball
Posted: Monday, July 14, 2008 2:32:10 PM

Rank: อาจารย์
Groups: Administration

Joined: 12/1/2007
Posts: 459
Location: Bangkok

select * from plant

ไม่ใช่ form ครับ



Imagination is more Important than Knowledge... /A.Einstein
nikorn
Posted: Thursday, July 17, 2008 4:01:33 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/14/2008
Posts: 9
Location: good

 ขอบคุณมากครับอาจารย์ ผมแก้อยู่ 3 วัน เลยครับ วันสุดท้ายแล้วเด้อที่จะสอบ กำลังทำโปรเจคคับ เหมือนพระเจ้ามาประทานพรเลยครับ  

zerozaaa
Posted: Thursday, July 17, 2008 7:43:32 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

 ผมต้องการ SELECT จาก Table 2 Table ลงใน Gridview เดียวไม่รู้รู้ว่า จะใช้แบบนี้เปล่าคับ ช่วยแนะนำทีคับ

และ การ IN SERT โดยการ Join Table ต้องเขียนยังไงบ้างคับช่วยสอนที่คับ

zerozaaa
Posted: Friday, July 18, 2008 5:59:24 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ตอบทีคับ

buffkoy
Posted: Monday, July 21, 2008 10:46:52 AM
Rank: มืออาชีพ
Groups: Member

Joined: 7/7/2008
Posts: 30

string x = ds.table["Room"].rows[--------]["Status"].tostring();

 

if(x=="ว่าง")

{ button.blackcolor = color.red;} 

ผมจะทำยังไงให้มันไปค้น rows จากดาต้าเบสเองอะคับ แบบ ไม่ต้องประกาศ ว่า rows[0] อะ

 ว่า ถ้าเจอ คำว่า ว่าง แล้ว จะทำงานตามเงื่อนไขอะครับ  แนะนำทีคับ ช่วยทีคับพี่ๆ

buffkoy
Posted: Monday, July 21, 2008 10:47:30 AM
Rank: มืออาชีพ
Groups: Member

Joined: 7/7/2008
Posts: 30

string x = ds.table["Room"].rows[--------]["Status"].tostring();

 

if(x=="ว่าง")

{ button.blackcolor = color.red;} 

ผมจะทำยังไงให้มันไปค้น rows จากดาต้าเบส ในทุกๆแถว เองอะคับ แบบ ไม่ต้องประกาศ ว่า rows[0] อะ

 ว่า ถ้าเจอ คำว่า ว่าง แล้ว จะทำงานตามเงื่อนไขอะครับ  แนะนำทีคับ ช่วยทีคับพี่ๆ

zerozaaa
Posted: Monday, July 28, 2008 1:17:15 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 71

ถ้าประกาศ ROWS[x] ได้ไหมคับโดยที่ใช้

int counrows = ds_order.Tables[0].Rows.Count; //นับจำนวน Rows ทั้งหมดมีกี่ แถว

for (int x = 0; x <= countnum ; x++){ // ให้+ ค่าไปเรื่อยๆๆจนกว่า มีค่ามากหรือเท่ากับค่า Rows

string idproduct = ds_order.Tables[0].Rows[x][0].ToString();  // ค้นหาแต่ล่ะ Rows

ยกตัวอย่างน่ะคับ ให้ค้นหาอะไรก็เขียนคับ และ สวนของ Rows หรือ Column ก็เหมือนกันคับ

}

ผมคิดว่าเป็นอย่างนี้นะคับ แต่ควรใส่ if ซ้อนอีกนะคับให้ค้นตามเงื่อนไข

ngo_somc
Posted: Tuesday, July 29, 2008 2:31:57 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/7/2008
Posts: 9
Location: Thai

foreach(DataRow dr in ds.Table["Room"])

{

if(dr.cells["Status"].tostring() == "ว่าง")

{

button.backcolor = color.red;

}

}

nikorn
Posted: Tuesday, July 29, 2008 8:16:37 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/14/2008
Posts: 9
Location: good

strConn = WebConfigurationManager.ConnectionStrings["diagnose"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();

sqlPlant = "Select * from durien";
SqlDataAdapter da4 = new SqlDataAdapter(sqlPlant, Conn);

DataSet ds4 = new DataSet();
da4.Fill(ds4, "durien");

 

int counrows = ds4.Tables[0].Columns.Count; //นับจำนวน Rows ทั้งหมดมีกี่ แถว
int countcolum = ds4.Tables[0].Rows.Count;

for (int x = 0; x < countcolum ; x++){ // ให้+ ค่าไปเรื่อยๆๆจนกว่า มีค่ามากหรือเท่ากับค่า Rows
for (int y = 0; y < counrows; y++)
{

//string[] rule=new string[x];
string id = ds4.Tables[0].Rows[x][y].ToString(); // ค้นหาแต่ล่ะ Rows
 

Label1.Text += id.ToString();
 

//ยกตัวอย่างน่ะคับ ให้ค้นหาอะไรก็เขียนคับ และ สวนของ Rows หรือ Column ก็เหมือนกันคับ
}

Label2.Text +="\n";

}
 

 

ผมต้องการเก็บข้อมูลที่อ่านมาไว้ในอาร์เลย์ครับผม

nikorn
Posted: Tuesday, July 29, 2008 10:11:36 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 7/14/2008
Posts: 9
Location: good

using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
public static void Main()
{
string constr = "Server = (local);Database =Northwind;Integrated Security=SSPI";
string cmdstr = "SELECT * FROM Shippers"; //กำหนดคำสั่ง sql
SqlConnection con = new SqlConnection();
con.ConnectionString = constr;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = cmdstr;
cmd.Connection = con;
con.Open();
SqlDataReader reader = cmd.ExecuteReader(); //สร้าง reader เพื่ออ่านข้อมูล
while (reader.Read())
{
Console.WriteLine("ShipperID : {0}\nShipperName : {1}", reader[0], reader[1]);
Console.WriteLine();
}
reader.Close();
con.Close();
Console.ReadLine();
}
}
 

 

ช่วยทำเป็นเว็บให้เป็นบุญหน่อยครับ แล้วเก็บค่าที่ได้ไว้ในตัวแปรได้ไหมครับ

assza
Posted: Tuesday, September 30, 2008 4:22:10 PM
Rank: มือสมัครเล่น
Groups: Member

Joined: 9/17/2008
Posts: 16

using System;
using System.Data;
using System.Data.SqlClient;
public class TestSelect
{
    public static void Main()
    {
        SqlConnection con = new SqlConnection("Server = .;Database=northwind;Integrated Security=true");
        SqlCommand com = new SqlCommand("SELECT * FROM Shippers;SELECT TOP 10 * FROM Customers", con);//กำหนดให้เลือกมาสองตาราง
        con.Open();
        SqlDataReader reader = com.ExecuteReader();
        Console.BackgroundColor = ConsoleColor.DarkGreen;
        Console.WriteLine("Show Shippers Table");
        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + "           " + reader.GetString(1));
        }
        reader.NextResult(); //แสดงตารางถัดไป
        Console.BackgroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("Show Custimers Table");
        while (reader.Read())
        {
            Console.WriteLine(reader[0] + "            " + reader[1]+"              "+reader[2]);
        }
        reader.Close();
        con.Close();
        Console.ReadLine();
    }
}

ช่วยอธิบายหน่อยคับ ว่างทำงานไงงับ

ของผมมันerrorที่ TestSelect
 

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