4.5 .net core下直接執行SQL語句並生成DataTable 面向雲的.net core開發框架

.net core能夠執行SQL語句,可是隻能生成強類型的返回結果。例如var blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs").ToList()。而不容許返回DataSet、DataTable等弱類型。可能因爲這個緣由沒有實如今.net core中DataTable,然而DataTable仍是可能會用到的。咱們這裏就有一個數據倉庫的需求,容許用戶自行編寫相似SQL語句,而後執行,以表格展現。由於語句是變幻無窮的,所以我也不知道用戶的語句輸出的是啥,更沒法以類型來定義,所以只能採用DataTable方式。 html

以前.net framework下,能夠經過dataadpater很方便的填充datatable,而後將datatable的數據推送到客戶端展現。可是.net core下,已經沒有DataTable和DataSet,咱們只能自行實現MicroDataTable。sql

這裏咱們也按照DataTable的方式,MicroDataTable的列定義爲MicroDataColumn,行定義爲MicroDataRow。代碼以下: 數據庫

 

 1     public class MicroDataTable
 2     {
 3         /// <summary>
 4         /// 整個查詢語句結果的總條數,而非本DataTable的條數
 5         /// </summary>
 6         public int TotalCount { get; set; }
 7 
 8         public List<MicroDataColumn> Columns { get; set; } = new List<MicroDataColumn>();
 9 
10         public List<MicroDataRow> Rows { get; set; } = new List<MicroDataRow>();
11 
12         public MicroDataColumn[] PrimaryKey { get; set; }
13 
14         public MicroDataRow NewRow()
15         {
16             return new MicroDataRow(this.Columns, new object[Columns.Count]);
17         }
18     }
19 
20     public class MicroDataColumn
21     {
22         public string ColumnName { get; set; }
23         public Type ColumnType { get; set; }
24     }
25 
26     public class MicroDataRow
27     {
28         private object[] _ItemArray;
29         public List<MicroDataColumn> Columns { get; private set; }
30 
31         public MicroDataRow(List<MicroDataColumn> columns, object[] itemArray)
32         {
33             this.Columns = columns;
34             this._ItemArray = itemArray;
35         }
36 
37         public object this[int index]
38         {
39             get { return _ItemArray[index]; }
40             set { _ItemArray[index] = value; }
41         }
42         public object this[string columnName]
43         {
44             get
45             {
46                 int i = 0;
47                 foreach (MicroDataColumn column in Columns)
48                 {
49                     if (column.ColumnName == columnName)
50                         break;
51                     i++;
52                 }
53                 return _ItemArray[i];
54             }
55             set
56             {
57                 int i = 0;
58                 foreach (MicroDataColumn column in Columns)
59                 {
60                     if (column.ColumnName == columnName)
61                         break;
62                     i++;
63                 }
64                 _ItemArray[i] = value;
65             }
66         }
67 }
View Code

 

須要注意的是TotalCount屬性,在分頁狀況下,是指查詢語句在數據庫中查詢出的全部記錄條數,而MicroDataTable的數據是當前頁面的記錄。框架

 

對於從數據庫中獲取DataTable的作法,採用相似SqlHelper的方式編寫DbContext的ExecuteDataTable擴展方法,傳入SQL語句和SQL語句的參數,生成MicroDataTable: ide

 

 1         public static MicroDataTable ExecuteDataTable(this DbContext context, string sql, params object[] parameters)
 2         {
 3             var concurrencyDetector = context.Database.GetService<IConcurrencyDetector>();
 4 
 5             using (concurrencyDetector.EnterCriticalSection())
 6             {
 7                 var rawSqlCommand = context.Database.GetService<IRawSqlCommandBuilder>().Build(sql, parameters);
 8 
 9                 RelationalDataReader query = rawSqlCommand.RelationalCommand.ExecuteReader(context.Database.GetService<IRelationalConnection>(), parameterValues: rawSqlCommand.ParameterValues);
10 
11                 return MicroDataTableHelper.FillDataTable(query.DbDataReader, 0, int.MaxValue);
12             }
13         }
14 
15         public static MicroDataTable ExecuteDataTable(this DbContext context, string sql, int pageIndex, int pageSize, params object[] parameters)
16         {
17             var concurrencyDetector = context.Database.GetService<IConcurrencyDetector>();
18 
19             using (concurrencyDetector.EnterCriticalSection())
20             {
21                 var rawSqlCommand = context.Database.GetService<IRawSqlCommandBuilder>().Build(sql, parameters);
22 
23                 RelationalDataReader query = rawSqlCommand.RelationalCommand.ExecuteReader(context.Database.GetService<IRelationalConnection>(), parameterValues: rawSqlCommand.ParameterValues);
24 
25                 return MicroDataTableHelper.FillDataTable(query.DbDataReader, 0, int.MaxValue);
26             }
27         }

 

這個方法仍是須要部分.net framework core的技巧的,流程是根據SQL和參數建立原生的SQLCommand,執行ExecuteReader方法返回DataReader,再把DataReader填充到MicroDataTable中。注意的是,IConcurrencyDetector在.net core的描述是這樣的:This API supports the Entity Framework Core infrastructure and is not intended to be used directly from your code. This API may change or be removed in future releases。咱們只能先這樣實現,之後看是否ef.core可否改變或者給出更好的方式。post

上面程序中,最後有一句話MicroDataTableHelper.FillDataTable,這個方法的主要功能是從DataReader填充到MicroDataTable的。優化

 

 1         public static MicroDataTable FillDataTable(DbDataReader reader, int pageIndex, int pageSize)
 2         {
 3             bool defined = false;
 4 
 5             MicroDataTable table = new MicroDataTable();
 6 
 7             int index = 0;
 8             int beginIndex = pageSize * pageIndex;
 9             int endIndex = pageSize * (pageIndex + 1) - 1;
10 
11             while (reader.Read())
12             {
13                 object[] values = new object[reader.FieldCount];
14 
15                 if (!defined)
16                 {
17                     for (int i = 0; i < reader.FieldCount; i++)
18                     {
19                         MicroDataColumn column = new MicroDataColumn()
20                         {
21                             ColumnName = reader.GetName(i),
22                             ColumnType = reader.GetFieldType(i)
23                         };
24 
25                         table.Columns.Add(column);
26                     }
27 
28                     defined = true;
29                 }
30 
31                 if (index >= beginIndex && index <= endIndex)
32                 {
33                     reader.GetValues(values);
34 
35                     table.Rows.Add(new MicroDataRow(table.Columns, values));
36                 }
37 
38                 index++;
39             }
40 
41             table.TotalCount = index;
42 
43             return table;
44         }

 

上面這個程序,是循序漸進的寫法,效率應該不過高。最近時間緊,沒有分析原先的Datatable裝載方式,之後有時間優化吧。下面給出一個當時用.net framework從datareader獲取分頁數據到datatable的程序,僅做參考。當時這段程序使用了table.beginloaddata/endloaddata方式,效率明顯有提高。ui

 1             using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
 2             {
 3                 int fieldCount = reader.FieldCount;
 4                 for (int i = 0; i < fieldCount; i++)
 5                 {
 6                     table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
 7                 }
 8 
 9                 object[] values = new object[fieldCount];
10                 int currentIndex = 0;
11                 int startIndex = pageSize * pageIndex;
12                 try
13                 {
14                     table.BeginLoadData();
15                     while (reader.Read())
16                     {
17                         if (startIndex > currentIndex++)
18                             continue;
19 
20                         if (pageSize > 0 && (currentIndex - startIndex) > pageSize)
21                             break;
22 
23                         reader.GetValues(values);
24                         table.LoadDataRow(values, true);
25                     }
26                 }
27                 finally
28                 {
29                     table.EndLoadData();
30                     try  //lgy:因爲鏈接阿里雲ADS數據庫cmd.Cancel()會報錯,因此把錯誤忽略了。
31                     {
32                         cmd.Cancel();
33                     }
34                     catch                 
35                     {                       
36                     }
37                     reader.Close();
38                 }
39             }

 

面向雲的.net core開發框架this

相關文章
相關標籤/搜索