|
 |
|
C# WinApp มีโค้ดที่ใช้ ตรวจสอบว่า Connectionstring ต้องใช้กับ connection ประเภทไหนไม๊ครับ |
|
 |
|
|
 |
 |
|
Code (C#)
public enum EnumConnectionType
{ ODBC, OLEDB, SqlClient }
public static System.Data.DataTable GetTableForm(string _SQL, string strConn, EnumConnectionType ConnectionType )
{
System.Data.DataSet ds = new System.Data.DataSet();
switch (ConnectionType)
{
case EnumConnectionType.ODBC:
System.Data.Odbc.OdbcConnection conn1 = new System.Data.Odbc.OdbcConnection(strConn);
conn1.Open();
System.Data.Odbc.OdbcDataAdapter adapter1 = new System.Data.Odbc.OdbcDataAdapter(_SQL, conn1);
adapter1.Fill(ds);
break;
case EnumConnectionType.OLEDB:
System.Data.OleDb.OleDbConnection conn2 = new System.Data.OleDb.OleDbConnection(strConn);
conn2.Open();
System.Data.OleDb.OleDbDataAdapter adapter2 = new System.Data.OleDb.OleDbDataAdapter(_SQL, conn2);
adapter2.Fill(ds);
break;
case EnumConnectionType.SqlClient:
System.Data.SqlClient.SqlConnection conn3 = new System.Data.SqlClient.SqlConnection(strConn);
conn3.Open();
System.Data.SqlClient.SqlDataAdapter adapter3 = new System.Data.SqlClient.SqlDataAdapter(_SQL, conn3);
adapter3.Fill(ds);
break;
}
return ds.Tables[0];
}
ปกติผมจะใช้ โค้ด นี้ในการเรียกใช้ DataTable ซึ่งหลักๆก็จะเป็น SQL Access Excel ซึ่ง Connectionstring ก้มักจะต่างกันออกไป
ประมาณว่า
Code
"Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=DataBaseName;User Id=UserName;Password=Secret;"
Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\myPath\myDb.mdb;Uid=Admin;Pwd=;
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mypath\myDb.mdb;User id=admin;Password=
ควาวจริง มี รูปแบบเยอะกว่านี้นะครับ
ตอนนี้ผมอยากตัด EnumConnectionType ConnectionType ออกแล้ว รับมาแค่ strConn
เพื่อจะมาเดาเอาว่า ConnectionType จะเป็นอะไร
ปัญหาคือ
เราจะรู้ได้ยังไงว่า strConn ที่มาเป็น Connection แบบ OLEDB หรือ ODBC หรือ Sql ครับ
หรือพอเรารับ strConn เข้ามาแล้วจะใช้ Connection แบบไหนนั่นเองครับ
Tag : .NET, C#, VS 2012 (.NET 4.x), Windows

|
|
 |
 |
 |
 |
Date :
2017-02-14 11:52:50 |
By :
lamaka.tor |
View :
953 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ปกติแล้วเรื่อง ConnectionString เรื่องใหญ่ครับ ที่เห็นๆ กันก็ทำแค่ Config ว่าจะใช้ Database ประเภทไหนครับ ส่วนพวก ConnectionString ก็ตามที่เลือก Type ของ Database เลยครับ Fix ใน Code ไปเลย
|
 |
 |
 |
 |
Date :
2017-02-14 13:19:47 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|