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

HOW TO: วิธีตั้งค่าให้ Visual C# Express สามารถติดต่อกับ SQL Server ได้ Options · View
ball
Posted: Wednesday, April 30, 2008 2:52:53 AM

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

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

ปกติแล้วถ้าต้องการสร้าง Windows Application ด้วย Visual C# 2005, 2008 Express Edition นั้นจะไม่สามารถใช้ Data Source เพื่อไปติดต่อกับ Database ที่อยู่ใน SQL Server ได้ จะทำได้แต่เพียงเป็นแบบไฟล์เท่านั้น

วันนี้ผมจะมาสอนวิธีที่ทำให้เราสามารถใช้ตัว Express ไปติดต่อ SQL Server โดนผ่าน Data Source ได้ครับ

สมมติว่า ตอนนี้เราได้มี Database ชื่อ NorthWind อยู่ใน Server เรียบร้อยแล้ว

1) เปิด Visual C# Express ขึ้นมา แล้วสร้างโปรเจ็ค Windows Application มา 1 โปรเจ็ค

2) เปิดไฟล์ Settings.settings

3) เปลี่ยน Type เป็น (Connection string) Name เป็น NorthWind (อะไรก็ได้)

4) ที่ Value ใส่ Server=.\SqlExpress;Database=NorthWind;Integrated Security=true;

5) กดปุ่ม Synchronize => ตอบ OK => Save แล้วปิดไฟล์นี้ไป

ุ6) ตอนนี้ที่ Solution Explorer จะมีไฟล์ app.config เพิ่มขึ้นมา ให้เปิดไฟล์นี้ครับ

ึ7) เพื่มโค๊ดนี้เข้าไปตามรูปครับ providerName="System.Data.SqlClient"

8) กดปุ่ม Save แล้วปิดไฟล์ app.config .....เสร็จแล้ว

9) ทดสอบว่าใช้ได้หรือไม่นั้นให้ไปเปิด Form1 ขึ้นมาแล้วกดที่เมนู Data => Show Data Sources จะปรากฏหน้าต่าง Data Sources แล้วคลิ๊กที่ Add New Data Source

10) เลือก Database แล้วกดปุ่ม Next ถ้ามีให้เลือกแบบนี้ถือว่าใช้ได้แล้วครับ (โดยที่ไม่ต้องไปกดปุ่ม New Connection อีกแล้ว)

11) ขึ้นตอนต่อไปก็จะเหมือนกับการใช้ใน Visual Studio Professional Edition แล้วครับ



Imagination is more Important than Knowledge... /A.Einstein
minkzzzzzzz
Posted: Thursday, May 01, 2008 2:54:29 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 1/30/2008
Posts: 16

ถ้าทำแบบนี้ ก้อไม่ต้องเขียนโค้ด เปิดปิด database รึป่าวค่ะ

ball
Posted: Thursday, May 01, 2008 8:57:25 AM

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

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

หลังจากทำวิธีนี้แล้วจะได้ไฟล์และคลาส NorthwindDataSet ซึ่งเราสามารถนำมันมาใช้ติดต่อฐานข้อมูลด้วยวิธี Disconnected แสดงว่าเราไม่ต้องเปิดปิด Database เองครับ



Imagination is more Important than Knowledge... /A.Einstein
minkzzzzzzz
Posted: Thursday, May 01, 2008 7:45:50 PM
Rank: มือสมัครเล่น
Groups: Member

Joined: 1/30/2008
Posts: 16

ทำแล้วมันขึ้นแบบรูป แก้ยังไง

 

 

 

ball
Posted: Thursday, May 01, 2008 8:43:46 PM

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

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

กด OK แล้วก็ Save ได้เลยครับ

เออ... แต่รู้สึกว่าคุณจะใช้ Visual Studio Profession Edition อยู่แล้วนะ ไม่ต้องใช้วิธีนี้ก็ได้ครับ วิธีนี้สำหรับ Express Edition



Imagination is more Important than Knowledge... /A.Einstein
Stoneman
Posted: Wednesday, October 01, 2008 5:06:12 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/1/2008
Posts: 2
Location: BKK

แล้วมีวิธีเซต SQL Express ให้มันติดต่อ C#.net ได้รึเปล่าครับ คือผมสร้างเวปขึ้นมาแล้วให้มันติดต่อกับ SQL Express เพื่อแสดงบน GridView ทำยังไงก็ไม่ได้ครับ งงมาเป็นอาทิตย์แล้วครับ กรุณาช่วยทีนะครับ

 protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Server=./STONEMAN;
            Initial Catalog=DB;User ID=sa;Password=stoneman");
        SqlCommand com = new SqlCommand("SELECT * FROM Football", con);
        con.Open(); //จะติดตรงที่ตัวแปรนี้ตลอดครับ
        SqlDataReader reader = com.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(reader);
        GridView1.DataSource = dt;
        con.Close();
   }

ERROR ที่เจอครับ:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

 

 

paedotnet
Posted: Thursday, October 02, 2008 1:04:22 AM

Rank: มือเทพ
Groups: Member

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

ลองเปลี่ยนจาก  Server=./STONEMAN เป็น .\STONEMAN นะครับ หรือ  .\SqlExpress



[With great power comes great responsibility]
Stoneman
Posted: Thursday, October 02, 2008 2:57:39 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/1/2008
Posts: 2
Location: BKK

paedotnet wrote:

ลองเปลี่ยนจาก  Server=./STONEMAN เป็น .\STONEMAN นะครับ หรือ  .\SqlExpress

เปลี่ยนแล้วครับมันขึ้น
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Cannot open database requested in login 'DB'. Login fails.
Login failed for user 'sa'.

Computer name : Stoneman

Server : Stoneman

Service ใน SQL Server Configuration Manager: SQL Server(MSSQLSERVER)

ใน SQL Server Surface Area Configuration: Remote Connections เป็น Local and Remote Connections เลือกเป็น  Usins both TCP/IP and Named pipes

แลัวผมก็เปิด cmd Prompt ขึ้นยมาเพื่อใช้คำสั่ง netstat -an -p tcp | find /i "1433" ทดสอบว่ามันเห็น port 1433 ของ SQL หรือไม่มันก็เจอนะครับ

เป็นไปได้มั้ยมันต้องไปเซตอะไรเกี่ยวกับ Remote Instance อะไรสักอย่าง ไม่รู้จริงๆ ครับ

ข้าน้อยขอคำชี้แนะจากท่านอาจาร์ด้วยครับ

 

 

 

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