namespace TORServices.TestDatabase
{
public class _database
{
public enum EnumConnectionType{ ODBC, OLEDB, SqlClient }
public string ConnectionString;
public EnumConnectionType ConnectionType;
public _database(string connStr,EnumConnectionType connType)
{
ConnectionString = connStr; ConnectionType = connType;
}
public System.Data.DataTable Datatable(string _SQL )
{
System.Data.DataSet ds = new System.Data.DataSet();
switch (ConnectionType)
{
case EnumConnectionType.ODBC:
System.Data.Odbc.OdbcConnection conn1 = new System.Data.Odbc.OdbcConnection(ConnectionString);
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(ConnectionString);
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(ConnectionString);
conn3.Open();
System.Data.SqlClient.SqlDataAdapter adapter3 = new System.Data.SqlClient.SqlDataAdapter(_SQL, conn3);
adapter3.Fill(ds);
break;
}
return ds.Tables[0];
}
public System.Data.DataRow Datarow(string sql)
{
System.Data.DataTable dt = Datatable(sql);
return dt.Rows[0];
}
}
}
โค้ด แบบมี Interface Code (C#)
namespace TORServices.TestInterface
{
public interface iDatabase
{
System.Data.DataRow Datarow(string sql);
System.Data.DataTable Datatable(string sql);
}
public class DataODBC : iDatabase
{
public string ConnectionString;
public DataODBC(string connStr) { ConnectionString = connStr; }
public System.Data.Odbc.OdbcConnection OdbcConnection(string connectionString = "")
{
System.Data.Odbc.OdbcConnection con = new System.Data.Odbc.OdbcConnection(connectionString);
try { con.Open(); return con; }
catch (Exception ex) { throw ex; }
}
public System.Data.DataRow Datarow(string sql, int rowIndax = 0)
{
System.Data.DataTable dt = Datatable(sql);
return (dt != null) ? dt.Rows[0] : null;
}
public System.Data.DataTable Datatable(string sql)
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.Odbc.OdbcDataAdapter adapter1 = new System.Data.Odbc.OdbcDataAdapter(sql, OdbcConnection(ConnectionString));
adapter1.Fill(ds);
return ds.Tables[0];
}
catch { return null; }
}
}
public class DataOleDb : iDatabase
{
public string ConnectionString;
public DataOleDb(string connStr) { ConnectionString = connStr; }
public System.Data.OleDb.OleDbConnection OleDbConnection(string connectionString = "")
{
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
try { con.Open(); return con; }
catch (Exception ex) { throw ex; }
}
public System.Data.DataRow Datarow(string sql, int rowIndax = 0)
{
System.Data.DataTable dt = Datatable(sql);
return (dt != null) ? dt.Rows[0] : null;
}
public System.Data.DataTable Datatable(string sql)
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.OleDb.OleDbDataAdapter adapter1 = new System.Data.OleDb.OleDbDataAdapter(sql, this.OleDbConnection(ConnectionString));
adapter1.Fill(ds);
return ds.Tables[0];
}
catch { return null; }
}
}
public class DataSqlClient : iDatabase
{
public string ConnectionString;
public DataSqlClient(string connStr) { ConnectionString = connStr; }
public System.Data.SqlClient.SqlConnection SqlConnection(string connectionString = "")
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
try { con.Open(); return con; }
catch (Exception ex) { throw ex; }
}
System.Data.DataRow Datarow(string sql, int rowIndax = 0)
{
System.Data.DataTable dt = Datatable(sql);
return (dt != null) ? dt.Rows[0] : null;
}
System.Data.DataTable Datatable(string sql)
{
try
{
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlDataAdapter adapter1 = new System.Data.SqlClient.SqlDataAdapter(sql, SqlConnection(ConnectionString));
adapter1.Fill(ds);
return ds.Tables[0];
}
catch { return null; }
}
}
}
ลองใช้งาน Code (C#)
public class TestDATA
{
public void Test()
{
iDatabase idata = new DataOleDb("Conn String ByToR");
System.Data.DataTable dt1 = idata.Datatable("SQL By ToR");
TORServices.TestDatabase._database db = new TestDatabase._database("Conn String ByToR", TestDatabase._database.EnumConnectionType.ODBC);
System.Data.DataTable dt2 = db.Datatable("SQL By ToR");
}
}