我想执行一个SELECT
查询,该查询只会对 Universe DB 产生一次影响,并快速有效地获取记录列表,然后将其添加到List<T>
.
我尝试过使用U2DataAdapter
,U2DataReader
但不确定哪个更有效。
以下是用于执行SELECT
查询的两种不同方法:
方法一:
// 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);
}
方法二:
// 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);
}
}
感谢您就如何改进上述任何一种方法提供反馈,如果有比这两种方法更好的方法,请分享。