Browse Source

融合一些最近用的方法

yuzhengyang 3 years ago
parent
commit
fd82aac9e8

+ 42 - 0
Azylee.Utils/Azylee.Core/DataUtils/DataTableUtils/DataRowTool.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.DataTableUtils
+{
+    public static class DataRowTool
+    {
+        public static string GetValue(DataRow row, string key)
+        {
+            string rs = "";
+            try
+            {
+                if (row.Table.Columns.Contains(key))
+                {
+                    return row[key].ToString();
+                }
+            }
+            catch { }
+            return rs;
+        }
+
+        public static string GetValueWithNull(DataRow row, string key)
+        {
+            string rs = null;
+            try
+            {
+                if (row.Table.Columns.Contains(key))
+                {
+                    if (!Convert.IsDBNull(row[key]))
+                    {
+                        rs = row[key]?.ToString();
+                    }
+                }
+            }
+            catch { }
+            return rs;
+        }
+    }
+}

+ 20 - 0
Azylee.Utils/Azylee.Core/DataUtils/DataTableUtils/DataTableTool.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.DataTableUtils
+{
+    public class DataTableTool
+    {
+        public static bool Ok(DataTable table)
+        {
+            if (table != null && table.Rows != null && table.Rows.Count > 0)
+            {
+                return true;
+            }
+            return false;
+        }
+    }
+}

+ 15 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/NameFormat.cs

@@ -7,6 +7,11 @@ namespace Azylee.Core.DataUtils.StringUtils
 {
     public class NameFormat
     {
+        /// <summary>
+        /// 转换为驼峰命名
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
         public static string ToCamelCase(string s)
         {
             string result = "";
@@ -42,6 +47,11 @@ namespace Azylee.Core.DataUtils.StringUtils
             }
             return result;
         }
+        /// <summary>
+        /// 转换为驼峰命名(首字母大写)
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
         public static string ToUpCamelCase(string s)
         {
             string result = "";
@@ -71,6 +81,11 @@ namespace Azylee.Core.DataUtils.StringUtils
             }
             return result;
         }
+        /// <summary>
+        /// 转换为下划线命名
+        /// </summary>
+        /// <param name="s"></param>
+        /// <returns></returns>
         public static string ToUnderline(string s)
         {
             string result = "";

+ 29 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/StringFinder.cs

@@ -0,0 +1,29 @@
+using Azylee.Core.DataUtils.CollectionUtils;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    public class StringFinder
+    {
+        public static bool has(string s, params string[] array)
+        {
+            bool result = true;
+            if (Str.Ok(s) && Ls.ok(array))
+            {
+                foreach (var item in array)
+                {
+                    if (!s.Contains(item))
+                    {
+                        result = false;
+                        break;
+                    }
+                }
+                return result;
+            }
+            return false;
+        }
+    }
+}

+ 64 - 0
Azylee.Utils/Azylee.Core/DbUtils/DbSqls/SqlGenerate.cs

@@ -0,0 +1,64 @@
+using Azylee.Core.DataUtils.DataTableUtils;
+using Azylee.Core.DataUtils.StringUtils;
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DbUtils.DbSqls
+{
+    /// <summary>
+    /// Sql语句生成
+    /// </summary>
+    public static class SqlGenerate
+    {
+        /// <summary>
+        /// 生成insert语句
+        /// </summary>
+        /// <param name="tableName">表名</param>
+        /// <param name="table">数据表</param>
+        /// <param name="insertSql">插入语句,默认:INSERT INTO,可自定义为:INSERT IGNORE INTO</param>
+        /// <param name="splitLine">按VALUES分割行</param>
+        /// <param name="spaceLine">每行间隔空行</param>
+        /// <returns></returns>
+        public static List<string> Insert(string tableName, DataTable table, string insertSql = "INSERT INTO", bool splitLine = false, bool spaceLine = false)
+        {
+            List<string> list = new List<string>();
+            if (DataTableTool.Ok(table))
+            {
+                // 获取所有列名
+                List<string> colNameList = new List<string>();
+                for (int i = 0; i < table.Columns.Count; i++)
+                {
+                    DataColumn column = table.Columns[i];
+                    if (StringTool.Ok(column.ColumnName)) colNameList.Add(column.ColumnName);
+                }
+
+                // 遍历所有行,转换为插入语句
+                for (int j = 0; j < table.Rows.Count; j++)
+                {
+                    DataRow row = table.Rows[j];
+                    string cols = "", vals = "";
+
+                    for (int k = 0; k < colNameList.Count; k++)
+                    {
+                        string value = DataRowTool.GetValueWithNull(row, colNameList[k]);
+
+                        cols += $"`{colNameList[k]}`";
+                        if (k < colNameList.Count - 1) cols += ", ";
+
+                        vals += value != null ? $"'{value}'" : "NULL";
+                        if (k < colNameList.Count - 1) vals += ", ";
+                    }
+                    string _splitLine = splitLine ? Environment.NewLine : "";
+                    string _spaceLine = spaceLine ? Environment.NewLine : "";
+                    string sql = $"{insertSql} `{tableName}` ({cols}) {_splitLine}VALUES ({vals});{_spaceLine}";
+                    list.Add(sql);
+                }
+
+            }
+            return list;
+        }
+    }
+}