Sfoglia il codice sorgente

DataTable Extensions

lith 1 anno fa
parent
commit
d82a178a86

+ 31 - 0
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Data/DataTable_Foreach_Extensions.cs

@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Runtime.CompilerServices;
+
+namespace Vit.Extensions
+{
+    public static partial class DataTable_Foreach_Extensions
+    {
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static void Foreach(this DataTable dt,Action<DataRow> action)
+        {
+            foreach (DataRow dr in dt.Rows)
+            {
+                action(dr);
+            }
+        }
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static IEnumerable<T> Select<T>(this DataTable dt, Func<DataRow, T> getField)
+        {
+            foreach (DataRow dr in dt.Rows)
+            {
+                yield return getField(dr);
+            }
+        }
+
+
+    }
+}

+ 50 - 0
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Data/DataTable_ToString_Extensions.cs

@@ -0,0 +1,50 @@
+using System;
+using System.Data;
+using System.Runtime.CompilerServices;
+using System.Text;
+
+namespace Vit.Extensions
+{
+    public static partial class DataTable_ToString_Extensions
+    {
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static string ToString(this DataTable dt, string fieldSeparator = null, string rowSeparator = null)
+        {
+            var build = new StringBuilder();
+            var columnCount = dt.Columns.Count;
+            foreach (DataRow dr in dt.Rows)
+            {
+                for (var t = 0; t < columnCount; t++)
+                {
+                    if (t != 0) build.Append(fieldSeparator);
+
+                    build.Append(dr[t]);
+                }
+                build.Append(rowSeparator);
+            }
+
+            return build.ToString();
+        }
+
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static string ToString(this DataTable dt, Func<object, string> getValue, string fieldSeparator = null, string rowSeparator = null)
+        {
+            var build = new StringBuilder();
+            var columnCount = dt.Columns.Count;
+            foreach (DataRow dr in dt.Rows)
+            {
+                for (var t = 0; t < columnCount; t++)
+                {
+                    if (t != 0) build.Append(fieldSeparator);
+
+                    build.Append(getValue(dr[t]));
+                }
+                build.Append(rowSeparator);
+            }
+
+            return build.ToString();
+        }
+
+    }
+}

+ 99 - 0
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Data/IDataReader_ReadData_Extensions.cs

@@ -0,0 +1,99 @@
+using System;
+using System.Data;
+using System.Runtime.CompilerServices;
+
+namespace Vit.Extensions
+{
+    public static class IDataReader_ReadData_Extensions
+    {
+
+
+        #region ReadDataTable    
+        /// <summary>
+        /// 加载数据到DataTable。若不存在数据,则返回空DataTable
+        /// </summary>
+        /// <param name="reader"></param>
+        /// <param name="maxRowCount"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static DataTable ReadDataTable(this IDataReader reader, int maxRowCount = int.MaxValue)
+        {
+            //var dt = new DataTable();
+            //dt.Load(reader);
+            //return dt;             
+
+
+            if (reader.IsClosed || maxRowCount <= 0) return null;
+
+
+            DataTable dt = new DataTable();
+            int fieldCount = reader.FieldCount;
+            for (int fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
+            {
+                if (reader.IsDateTime(fieldIndex))
+                {
+                    dt.Columns.Add(reader.GetName(fieldIndex), typeof(DateTime));
+                    continue;
+                }
+
+                dt.Columns.Add(reader.GetName(fieldIndex), reader.GetFieldType(fieldIndex));
+            }
+
+            if (!reader.Read()) return dt;
+
+            dt.BeginLoadData();
+            object[] objValues = new object[fieldCount];
+            int curRowCount = 0;
+
+            while (true)
+            {
+                reader.GetValues(objValues);
+                dt.LoadDataRow(objValues, true);
+
+                curRowCount++;
+
+                if (curRowCount >= maxRowCount)
+                {                   
+                    break;
+                }
+
+                if (!reader.Read()) break;
+            }
+
+            dt.EndLoadData();
+            return dt;
+        }
+        #endregion
+
+
+
+
+
+        #region ReadDataSet
+
+        /// <summary>
+        /// (Lith Framework)
+        /// </summary>
+        /// <param name="reader"></param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static DataSet ReadDataSet(this IDataReader reader)
+        {
+            var ds = new DataSet();
+            while (!reader.IsClosed)
+            {
+                var dt = reader.ReadDataTable();
+                if (dt == null) break;
+                ds.Tables.Add(dt);
+
+                if (!reader.NextResult()) break;
+            }
+            return ds;
+        }
+        #endregion
+
+
+
+
+    }
+}

+ 45 - 0
dotnet/Library/Vit/Vit.Core/Vit.Core/Extensions/Data/IDataReader_ReadValue_Extensions.cs

@@ -0,0 +1,45 @@
+using System.Data;
+using System.Runtime.CompilerServices;
+
+namespace Vit.Extensions
+{
+    public static partial class IDataReader_ReadValue_Extensions
+    {
+
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static bool IsDateTime(this IDataReader dr, int i)
+        {
+            var typeName = dr.GetDataTypeName(i);
+            return typeName == "DATE" || typeName == "TIME" || typeName == "DATETIME" || typeName == "DATETIME2";
+
+            //return dr.GetDataTypeName(i) == "DATETIME";
+            //return dr.GetDataTypeName(i).ToLower().Contains("datetime");
+        }
+
+
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="dr"></param>
+        /// <param name="i"></param>
+        /// <param name="DateTimeFormat">时间序列化格式</param>
+        /// <returns></returns>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+        public static string Serialize(this IDataReader dr, int i, string DateTimeFormat = "yyyy-MM-dd HH:mm:ss")
+        {
+            if (dr.IsDBNull(i))
+            {
+                return null;
+            }
+
+            if (IsDateTime(dr, i))
+            {
+                return dr.GetDateTime(i).ToString(DateTimeFormat);
+            }
+            return dr[i].ToString();
+        }
+
+
+    }
+}