|
|
|
เอา function การดึง datatable แบบบ้าน ๆ มาใช้งานมาให้ติชมครับ 5555 |
|
|
|
|
|
|
|
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];
}
การใช้งานก็แค่
Code (C#)
DataTable dt = GetTableForm(sql, strConn, EnumConnectionType.SqlClient ); //ประมาณนั้น
Code (C#)
public static System.Windows.Forms.ComboBox DatabaseInCombobox(string _SQL, string strConn, EnumConnectionType ConnectionType)
{
ComboBox cmb = new ComboBox();
cmb.DataSource = GetTableForm(_SQL, strConn, ConnectionType);
return cmb;
}
public static string[] GetData(string field, string table, string strConn, EnumConnectionType ConnectionType )
{
DataTable dt = GetTableForm("Select " + field + " FROM " + table, strConn, ConnectionType);
string[] str= new string[dt.Rows.Count];
for (int i = 0;i<dt.Rows.Count;i++)
{
str[i] = dt.Rows[i][field].ToString();
}
return str;
}
public static DataRow GetDataRow(string sql ,string strConn, EnumConnectionType ConnectionType)
{
DataTable dt = GetTableForm(sql, strConn, ConnectionType);
return dt.Rows[0];
}
เผื่อจะมีประโยชน์บ้างไม่มากก็น้อยครับ
Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x), Windows
|
|
|
|
|
|
Date :
2015-02-28 15:40:00 |
By :
lamaka.tor |
View :
1026 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ
|
|
|
|
|
Date :
2015-02-28 15:47:32 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|