为PetaPoco添加Fill方法

因为公司框架中的很多地方用到DataTable,直接将DataTable作为数据源绑定到DataGridView,所以为PetaPoco添加了两个方法,一个是用以填充单个DataTable,一个是用以多结果集时填充DataSet。

 

  
  
  
  
  1. #region operation Fill  
  2.  
  3.         /// <summary>  
  4.         /// 填充一个DataSet  
  5.         /// </summary>  
  6.         /// <param name="ds">引用的DataSet</param>  
  7.         /// <param name="sql">sql语句</param>  
  8.         /// <param name="args">参数</param>  
  9.         public void Fill(DataSet ds, string sql, params object[] args)  
  10.         {  
  11.             OpenSharedConnection();  
  12.  
  13.             try 
  14.             {  
  15.                 using (var cmd = CreateCommand(_sharedConnection, sql, args))  
  16.                 {  
  17.                     using (DbDataAdapter dbDataAdapter = _factory.CreateDataAdapter())  
  18.                     {  
  19.                         dbDataAdapter.SelectCommand = (DbCommand)cmd;  
  20.                         dbDataAdapter.Fill(ds);  
  21.                     }  
  22.                 }  
  23.             }  
  24.             finally 
  25.             {  
  26.                 CloseSharedConnection();  
  27.             }  
  28.         }  
  29.  
  30.         /// <summary>  
  31.         /// 填充一个DataTable  
  32.         /// </summary>  
  33.         /// <param name="dt">DataTable的引用</param>  
  34.         /// <param name="sql">Sql语句</param>  
  35.         /// <param name="args">参数</param>  
  36.         public void Fill(DataTable dt, string sql, params object[] args)  
  37.         {  
  38.             OpenSharedConnection();  
  39.  
  40.             try 
  41.             {  
  42.                 using (var cmd = CreateCommand(_sharedConnection, sql, args))  
  43.                 {  
  44.                     using (DbDataAdapter dbDataAdapter = _factory.CreateDataAdapter())  
  45.                     {  
  46.                         dbDataAdapter.SelectCommand = (DbCommand)cmd;  
  47.                         dbDataAdapter.Fill(dt);  
  48.                     }  
  49.                 }  
  50.             }  
  51.             finally 
  52.             {  
  53.                 CloseSharedConnection();  
  54.             }  
  55.         }  
  56.  
  57.         #endregion 

 

你可能感兴趣的:(Datatable,dataset,PetaPOCO)