【问题标题】:What is the fastest way to GET a list of records from Universe DB file using C#.NET?使用 C#.NET 从 Universe DB 文件获取记录列表的最快方法是什么?
【发布时间】:2019-09-01 18:39:53
【问题描述】:

我想执行一个 SELECT 查询,它只会对 Universe DB 产生一次访问,并快速有效地获取记录列表,然后将其添加到 List<T>

我尝试过使用U2DataAdapterU2DataReader,但不确定哪个更有效。

以下是用于执行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);                   
}

方法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);                  
  }                
}

感谢您对如何改进上述任何一种方法的反馈,如果有比这两种方法更好的方法,请分享。

【问题讨论】:

    标签: universe uniobjects u2netdk


    【解决方案1】:

    如果您还没有,我会查看 U2 Toolkit for .NET 提供的示例,希望您可以访问这些示例。安装目录中有一个示例文件夹。这提供了几个不同的示例。

    至于哪个效果更好,这可能取决于您要完成的工作以及数据的状态。我已经使用了这两种访问模式,它们都根据您的字典状态工作。至于返回一个列表如果您期待字符串,我会说这些方法中的任何一个都可以工作,这只是性能测试的问题。

    我不喜欢忽略类型,所以在上述情况下,我希望它返回 DataTable 或 List 或类似的东西。我认为这取决于您需要它成为对象的位置以及您只需要显示一些数据的位置。

    就个人而言,我一直很喜欢使用实体框架来解决这个问题,因此它会提前解决打字问题,并鼓励您更加注意字典项目。当您的元数据良好或您对它有足够的控制以使其良好时,这将起作用。然后,您可以使用 Linq 查询并返回一个 List,然后您可以对其进行抽象。我通常使用指向 Get、GetAll>、Upsert> 的接口模式。

    首先我创建一个文件模型。引用表中的文件名和列属性中属性的字典名称。如图所示,您甚至可以使用 I-Type。

    [Table("MY.FILE)]
    public class MyFile
    {
        [Key]
        [Column("MyAttribute0", Order = 1)]
        public string RecordID { get; set; }
        [Column("MyAttribute1")]
        public decimal RecordData1 { get; set; }
        Column("MyAttribute2")]
        public decimal RecordData2 { get; set; }
        Column("MyAttribute3")]
        public decimal RecordData2 { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        Column("MYITYPE1")]
        public decimal MyIType1{ get; set; }
    }
    

    然后确保在数据库的 dbContext 中引用此模型。您将根据示例使用连接字符串进行设置。

     public DbSet<MyFile> MyFiles { get; set; }
    

    然后,当您获取数据时,您会像这样。

     this.context.MyFiles.Where(f => f.RecordData2 == "Foo").ToList()
    

    祝你好运。

    【讨论】:

    • 两个问题:1) Entity Framework 是不是速度慢? 2) 我们可以在 Universe DB 上以某种方式使用 Entity Framework Core 吗?
    • 1.) 取决于你在做什么,因为根据我的经验,基本的 Universe CRUD 是相当合理的。 2.) Rocket U2 Toolkit for .NET 需要 .NET framework 4 或更高版本,因此 Core 可能无法工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    相关资源
    最近更新 更多