You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.7 KiB

2 years ago
  1. using EasyNet.DBUtility;
  2. using EasyNet.Manager;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Text;
  9. namespace EntityBuilder
  10. {
  11. public class TableHelper
  12. {
  13. /// <summary>
  14. /// 获取局域网内的所有數據庫服务器名称
  15. /// </summary>
  16. /// <returns>服务器名称数组</returns>
  17. public static List<string> GetSqlServerNames()
  18. {
  19. var dataSources = SqlClientFactory.Instance.CreateDataSourceEnumerator().GetDataSources();
  20. var column = dataSources.Columns[@"InstanceName"];
  21. var column2 = dataSources.Columns[@"ServerName"];
  22. var rows = dataSources.Rows;
  23. var Serverlist = new List<string>();
  24. var array = string.Empty;
  25. for (int i = rows.Count - 1; i >= 0; i--)
  26. {
  27. var str2 = rows[i][column2] as string;
  28. if (((!(rows[i][column] is string str)) || (str.Length == 0)) || (@"MSSQLSERVER" == str))
  29. {
  30. array = str2;
  31. }
  32. else
  33. {
  34. array = str2 + @"/" + str;
  35. }
  36. Serverlist.Add(array);
  37. }
  38. Serverlist.Sort();
  39. return Serverlist;
  40. }
  41. /// <summary>
  42. /// 查詢sql中的非系统库
  43. /// </summary>
  44. /// <param name="connection"></param>
  45. /// <returns></returns>
  46. public static List<string> DatabaseList(string connection)
  47. {
  48. var getCataList = new List<string>();
  49. const string cmdStirng = @"select name from sys.databases where database_id > 4";
  50. var connect = new SqlConnection(connection);
  51. using (var cmd = new System.Data.SqlClient.SqlCommand(cmdStirng, connect))
  52. {
  53. try
  54. {
  55. if (connect.State == ConnectionState.Closed)
  56. {
  57. connect.Open();
  58. IDataReader dr = cmd.ExecuteReader();
  59. getCataList.Clear();
  60. while (dr.Read())
  61. {
  62. getCataList.Add(dr[@"name"].ToString());
  63. }
  64. dr.Close();
  65. }
  66. }
  67. catch (Exception)
  68. {
  69. throw;
  70. }
  71. finally
  72. {
  73. if (connect != null && connect.State == ConnectionState.Open)
  74. {
  75. connect.Dispose();
  76. }
  77. }
  78. return getCataList;
  79. }
  80. }
  81. public static List<DbTableInfo> GetTables()
  82. {
  83. if (AdoHelper.DbType == DatabaseType.SQLSERVER)
  84. {
  85. return GetMSSQLTables();
  86. }
  87. else if (AdoHelper.DbType == DatabaseType.MYSQL)
  88. {
  89. return GetMySQLTables();
  90. }
  91. else
  92. {
  93. throw new Exception(@"暂时不支持其他數據庫類型");
  94. }
  95. }
  96. /// <summary>
  97. /// 获取列名
  98. /// </summary>
  99. /// <param name="connection"></param>
  100. /// <returns></returns>
  101. public static List<DbTableInfo> GetMSSQLTables()
  102. {
  103. var connection = (SqlConnection)DbFactory.CreateDbConnection(AdoHelper.ConnectionString);
  104. var tablelist = new List<DbTableInfo>();
  105. try
  106. {
  107. if (connection.State == ConnectionState.Closed)
  108. {
  109. connection.Open();
  110. var objTable = connection.GetSchema(@"Tables");
  111. foreach (DataRow row in objTable.Rows)
  112. {
  113. var tb = new DbTableInfo
  114. {
  115. Name = row[2].ToString(),
  116. DbObjectType = row[3].ToString() == @"VIEW" ? DbObjectType.View : DbObjectType.Table
  117. };
  118. tablelist.Add(tb);
  119. }
  120. }
  121. }
  122. catch
  123. {
  124. throw;
  125. }
  126. finally
  127. {
  128. if (connection != null && connection.State == ConnectionState.Closed)
  129. {
  130. connection.Dispose();
  131. }
  132. }
  133. return tablelist;
  134. }
  135. /// <summary>
  136. /// 获取列名
  137. /// </summary>
  138. /// <param name="connection"></param>
  139. /// <returns></returns>
  140. public static List<DbTableInfo> GetMySQLTables()
  141. {
  142. var sql = @"select TABLE_NAME as name from INFORMATION_SCHEMA.`TABLES` WHERE TABLE_SCHEMA = '" + AdoHelper.DbName + @"'";
  143. var m = ManagerFactory.GetManager();
  144. var tablelist = DbManager.Query<DbTableInfo>(sql);
  145. return tablelist;
  146. }
  147. public static List<ColumnInfo> GetColumnField(string TableName)
  148. {
  149. if (AdoHelper.DbType == DatabaseType.SQLSERVER)
  150. {
  151. return GetMSSQLColumnField(TableName);
  152. }
  153. else if (AdoHelper.DbType == DatabaseType.MYSQL)
  154. {
  155. return GetMySQLColumnField(TableName);
  156. }
  157. else
  158. {
  159. throw new Exception(@"暂时不支持其他數據庫類型");
  160. }
  161. }
  162. /// <summary>
  163. /// 獲取字段
  164. /// </summary>
  165. /// <param name="TableName"></param>
  166. /// <returns></returns>
  167. public static List<ColumnInfo> GetMSSQLColumnField(string TableName)
  168. {
  169. var sb = new StringBuilder();
  170. if (TableName.IndexOf(@"OVW") > -1)
  171. {
  172. sb.Append(@" Select c.name As DbColumnName , t.name As DataType,'' AS IsIdentity,'' AS IsPrimarykey, CASE c.isnullable WHEN 1 THEN '√' ELSE '' END as IsNullable ");
  173. sb.Append(@" From SysObjects As o , SysColumns As c , SysTypes As t ");
  174. sb.Append(@" Where o.type in ('u','v') And o.id = c.id And c.xtype = t.xtype AND t.name<>'sysname' And o.Name = '").Append(TableName).Append(@"' ");
  175. }
  176. else
  177. {
  178. sb.Append(@" SELECT a.name as DbColumnName,");
  179. sb.Append(@" b.name as DataType,");
  180. sb.Append(@" CASE COLUMNPROPERTY(a.id,a.name,'IsIdentity') WHEN 1 THEN '√' ELSE '' END as IsIdentity, ");
  181. sb.Append(@" CASE WHEN EXISTS ( SELECT * FROM sysobjects WHERE xtype='PK' AND name IN ( SELECT name FROM sysindexes WHERE id=a.id AND indid IN ( SELECT indid FROM sysindexkeys ");
  182. sb.Append(@" WHERE id=a.id AND colid IN ( SELECT colid FROM syscolumns WHERE id=a.id AND name=a.name ) ) ) ) THEN '√' ELSE '' END as IsPrimarykey,");
  183. sb.Append(@" CASE a.isnullable WHEN 1 THEN '√' ELSE '' END as IsNullable ");
  184. sb.Append(@" FROM syscolumns a ");
  185. sb.Append(@" LEFT JOIN systypes b ON a.xtype=b.xusertype ");
  186. sb.Append(@" INNER JOIN sysobjects c ON a.id=c.id AND c.xtype='U' AND c.name<>'dtproperties' ");
  187. sb.Append(@" LEFT JOIN syscomments d ON a.cdefault=d.id ");
  188. sb.Append(@" WHERE c.name = '").Append(TableName).Append(@"' ");
  189. sb.Append(@" ORDER BY c.name, a.colorder");
  190. }
  191. //使用Mast框架查詢數據
  192. var m = ManagerFactory.GetManager();
  193. var list = DbManager.Query<ColumnInfo>(sb.ToString());
  194. return list;
  195. }
  196. /// <summary>
  197. /// 獲取字段
  198. /// </summary>
  199. /// <param name="TableName"></param>
  200. /// <returns></returns>
  201. public static List<ColumnInfo> GetMySQLColumnField(string TableName)
  202. {
  203. var sb = new StringBuilder();
  204. sb.Append(@" SELECT COLUMN_NAME as DbColumnName,");
  205. sb.Append(@" DATA_TYPE as DataType,");
  206. sb.Append(@" CASE EXTRA WHEN 'auto_increment' THEN '√' ELSE '' END as IsIdentity, ");
  207. sb.Append(@" CASE COLUMN_KEY WHEN 'PRI' THEN '√' ELSE '' END as IsPrimaryKey, ");
  208. sb.Append(@" CASE IS_NULLABLE WHEN 'YES' THEN '√' ELSE '' END as IsNullable ");
  209. sb.Append(@" from INFORMATION_SCHEMA.COLUMNS ");
  210. sb.Append(@" Where table_name = '").Append(TableName).Append(@"' ");
  211. sb.Append(@" AND table_schema = '").Append(AdoHelper.DbName).Append(@"' ");
  212. //使用Mast框架查詢數據
  213. var m = ManagerFactory.GetManager();
  214. var list = DbManager.Query<ColumnInfo>(sb.ToString());
  215. return list;
  216. }
  217. }
  218. }