I want to perform a SELECT
query which would make a hit to the Universe DB once only and get the list of records quickly and efficiently which would then be added to a List<T>
.
I have tried to use U2DataAdapter
as well as U2DataReader
but not sure which one is more efficient.
Below are two different approaches used for executing a SELECT
query:
Approach 1:
// Connection
U2ConnectionStringBuilder csb = new U2ConnectionStringBuilder();
csb.Server = "server";
csb.Database = "db";
csb.UserID = "user";
csb.Password = "pwd";
csb.ServerType = "UNIVERSE";
csb.AccessMode = "Native";
csb.RpcServiceType = "uvcs";
U2Connection con = new U2Connection(csb.ToString());
con.Open();
U2Command cmd = con.CreateCommand();
cmd.CommandText = "Action=Select;File=SOME.FILE;Attributes=COL1,COL2,COL3,COL4;Where=COL2=XYZ";
// Code to get the data
U2DataAdapter da = new U2DataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
var list = new List<T>();
// storing result in List<T>. this takes most of the time
foreach(DataRow dr in dt.Rows)
{
T obj = new T
{
col1 = item.ItemArray[0].ToString(),
col2 = item.ItemArray[1].ToString(),
col3 = item.ItemArray[2].ToString(),
col4 = item.ItemArray[3].ToString(),
};
list.Add(obj);
}
Approach 2:
// Connection
U2ConnectionStringBuilder csb = new U2ConnectionStringBuilder();
csb.Server = "server";
csb.Database = "db";
csb.UserID = "user";
csb.Password = "pwd";
csb.ServerType = "UNIVERSE";
U2Connection con = new U2Connection(csb.ToString());
con.Open();
U2Command cmd = con.CreateCommand();
cmd.CommandText = "SELECT COL1,COL2,COL3,COL4 FROM SOME.FILE WHERE COL2= 'XYZ'";
// Code to get the data
var dr= cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string col1 = dr.GetString(0);
string col2 = dr.GetString(1);
string col3 = dr.GetString(2);
string col4 = dr.GetString(3);
T obj = new T { col1, col2, col3, col4 };
list.Add(obj);
}
}
Your feedback is appreciated on how to improve any one of the above mentioned approaches and if there is any better approach than these two then please do share it.