using System.Data.SqlClient; namespace LabOrderDetails { class connectDB //คลาสสำหรับ connect ไปที่ ฐานข้อมูล sql server { public SqlConnection SqlStrConn() { string server = @".\sqlexpress"; //ชื่อ Server string db = "Northwind"; //ชื่อฐานข้อมูล string strConn = string.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True" , server, db); return new SqlConnection(strConn); } public SqlConnection SqlStrConn(string server, string db) //ส่งค่า Server และชื่อ Database { string strConn = string.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True" , server, db); return new SqlConnection(strConn); } } } //เรียกใช้ //SqlConnection conn; //conn = new connectDB().SqlStrConn(); //หรือ //SqlConnection conn; //conn = new connectDB().SqlStrConn(@"ServerName\sqlexpress","DatabaseName");