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.

789 lines
30 KiB

3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Text;
  6. using static Apewer.Source.SourceUtility;
  7. #if NETFRAMEWORK
  8. using System.Data.Sql;
  9. #endif
  10. namespace Apewer.Source
  11. {
  12. /// <summary>连接 SQL Server 数据库的客户端。</summary>
  13. [Serializable]
  14. public class SqlClient : DbClient
  15. {
  16. #region connection
  17. private SqlConnection _conn = null;
  18. private string _connstr = "";
  19. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  20. /// <exception cref="ArgumentNullException"></exception>
  21. /// <exception cref="ArgumentException"></exception>
  22. public SqlClient(IDbConnection connection, Timeout timeout = null) : base(timeout)
  23. {
  24. if (connection == null) throw new ArgumentNullException(nameof(connection), "指定的连接无效。");
  25. var conn = connection as SqlConnection;
  26. if (conn == null) throw new ArgumentException(nameof(connection), "指定的连接不是支持的类型。");
  27. _conn = conn;
  28. _connstr = conn.ConnectionString;
  29. }
  30. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  31. public SqlClient(string connectionString, Timeout timeout = null) : base(timeout)
  32. {
  33. _connstr = connectionString ?? "";
  34. }
  35. /// <summary>使用连接凭据创建数据库连接实例。</summary>
  36. /// <exception cref="ArgumentNullException"></exception>
  37. public SqlClient(string address, string store, string user, string pass, Timeout timeout = null) : base(timeout)
  38. {
  39. if (address.IsEmpty()) throw new ArgumentNullException(nameof(address));
  40. if (store.IsEmpty()) store = "master";
  41. var cs = $"data source = {address ?? ""}; initial catalog = {store}; ";
  42. if (string.IsNullOrEmpty(user)) cs += "integrated security = sspi; ";
  43. else
  44. {
  45. cs += $"user id = {user}; ";
  46. if (!string.IsNullOrEmpty(pass)) cs += $"password = {pass}; ";
  47. }
  48. if (timeout != null) cs += $"connection timeout = {timeout.Connect}; ";
  49. _connstr = cs;
  50. }
  51. #endregion
  52. #region override
  53. /// <summary>查询数据库中的所有表名。</summary>
  54. public override string[] TableNames() => QueryStrings("select [name] from [sysobjects] where [type] = 'u' order by [name]");
  55. /// <summary>查询数据库实例中的所有数据库名。</summary>
  56. public override string[] StoreNames() => QueryStrings("select [name] from [master]..[sysdatabases] order by [name]", new string[] { "master", "model", "msdb", "tempdb" });
  57. /// <summary>查询表中的所有列名。</summary>
  58. public override string[] ColumnNames(string tableName) => QueryStrings($"select [name] from [syscolumns] where [id] = object_id('{TextUtility.AntiInject(tableName)}')");
  59. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  60. protected override string Initialize(TableStructure structure, string table)
  61. {
  62. var model = structure.Model;
  63. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  64. // 检查现存表。
  65. var exists = false;
  66. var tables = TableNames();
  67. if (tables.Length > 0)
  68. {
  69. var lower = table.ToLower();
  70. foreach (var tn in tables)
  71. {
  72. if (TextUtility.IsEmpty(tn)) continue;
  73. if (tn.ToLower() == lower)
  74. {
  75. exists = true;
  76. break;
  77. }
  78. }
  79. }
  80. if (exists)
  81. {
  82. // 获取已存在的列名。
  83. var columns = ColumnNames(table);
  84. if (columns.Length > 0)
  85. {
  86. var lower = new List<string>();
  87. foreach (var column in columns)
  88. {
  89. if (TextUtility.IsBlank(column)) continue;
  90. lower.Add(column.ToLower());
  91. }
  92. columns = lower.ToArray();
  93. }
  94. // 找出新列。
  95. var newColumns = new List<ColumnAttribute>();
  96. foreach (var column in structure.Columns)
  97. {
  98. // 检查 Independent 特性。
  99. if (structure.Independent && column.Independent) continue;
  100. // 去重。
  101. var lower = column.Field.ToLower();
  102. if (columns.Contains(lower)) continue;
  103. var type = Declaration(column);
  104. if (string.IsNullOrEmpty(type)) return $"类型 {column.Type} 不受支持。";
  105. newColumns.Add(column);
  106. }
  107. // 执行:添加列。
  108. if (newColumns.IsEmpty()) return null;
  109. foreach (var column in newColumns)
  110. {
  111. var type = Declaration(column);
  112. var sql = $"alter table [{table}] add {type}; ";
  113. var execute = Execute(sql, null, false);
  114. if (!execute.Success) return execute.Message;
  115. }
  116. return null;
  117. }
  118. else
  119. {
  120. var sqlcolumns = new List<string>();
  121. var primaryKey = new List<ColumnAttribute>();
  122. foreach (var column in structure.Columns)
  123. {
  124. // 检查 Independent 特性。
  125. if (structure.Independent && column.Independent) continue;
  126. var type = Declaration(column);
  127. if (!column.Independent)
  128. {
  129. if (column.Incremental) type += " identity";
  130. if (column.PrimaryKey)
  131. {
  132. primaryKey.Add(column);
  133. type += " not null";
  134. }
  135. }
  136. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  137. sqlcolumns.Add(type);
  138. }
  139. if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表:没有定义任何字段。";
  140. // 无主键时直接建表,有主键时使用事务。
  141. if (primaryKey.Count < 1)
  142. {
  143. // 执行建表。
  144. var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  145. var execute = Execute(sql, null, false);
  146. if (!execute.Success)
  147. {
  148. if (ThrowAdoException) throw new SqlException(execute.Message);
  149. else return execute.Message;
  150. }
  151. return null;
  152. }
  153. else
  154. {
  155. return this.InTransaction(() =>
  156. {
  157. // 执行建表。
  158. var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  159. var execute = Execute(sql, null, false);
  160. if (!execute.Success)
  161. {
  162. if (ThrowAdoException) throw new SqlException(execute.Message);
  163. else return execute.Message;
  164. }
  165. // 添加主键。
  166. var sql_pk_fields = primaryKey.Map(x => $"[{x.Field}]").Join(", ");
  167. var sql_pk = $"alter table [{table}] add constraint [pk_{table}] primary key clustered ({sql_pk_fields}) ";
  168. var execute_pk = Execute(sql_pk, null, false);
  169. if (!execute_pk.Success)
  170. {
  171. if (ThrowAdoException) throw new SqlException(execute_pk.Message);
  172. else return execute_pk.Message;
  173. }
  174. return null;
  175. });
  176. }
  177. }
  178. }
  179. /// <summary>插入记录。返回错误信息。</summary>
  180. public override string Insert(object record, string table = null, bool adjust = true)
  181. {
  182. if (record == null) return "参数无效。";
  183. if (adjust) FixProperties(record);
  184. var structure = TableStructure.Parse(record.GetType());
  185. if (structure == null) return "无法解析记录模型。";
  186. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  187. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  188. // 排除字段。
  189. var excluded = new List<string>();
  190. foreach (var ca in structure.Columns)
  191. {
  192. // 排除不使用 ORM 的属性。
  193. if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
  194. }
  195. var ps = structure.CreateParameters(record, Parameter, excluded);
  196. var psc = ps.Length;
  197. if (psc < 1) return "数据模型不包含字段。";
  198. var names = new List<string>(psc);
  199. var values = new List<string>(psc);
  200. foreach (var column in ps)
  201. {
  202. names.Add($"[{column.ParameterName}]");
  203. values.Add("@" + column.ParameterName);
  204. }
  205. var sb = new StringBuilder();
  206. sb.Append("insert into ", table, "(", string.Join(", ", names.ToArray()), ") ");
  207. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  208. var sql = sb.ToString();
  209. var execute = Execute(sql, ps, false);
  210. if (execute.Success) return null;
  211. return execute.Message;
  212. }
  213. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  214. /// <remarks>不更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  215. public override string Update(IRecord record, string table = null, bool adjust = true)
  216. {
  217. if (record == null) return "参数无效。";
  218. if (adjust)
  219. {
  220. FixProperties(record);
  221. SetUpdated(record);
  222. }
  223. var structure = TableStructure.Parse(record.GetType());
  224. if (structure == null) return "无法解析记录模型。";
  225. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  226. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  227. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  228. // 排除字段。
  229. var excluded = new List<string>();
  230. if (structure.Key != null) excluded.Add(structure.Key.Field);
  231. foreach (var ca in structure.Columns)
  232. {
  233. // 排除不使用 ORM 的属性、自增属性和主键属性。
  234. if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
  235. }
  236. var ps = structure.CreateParameters(record, Parameter, excluded);
  237. var psc = ps.Length;
  238. if (psc < 1) return "数据模型不包含字段。";
  239. var items = new List<string>();
  240. foreach (var p in ps)
  241. {
  242. var pn = p.ParameterName;
  243. items.Add($"[{pn}] = @{pn}");
  244. }
  245. var key = record.Key.SafeKey();
  246. var sql = $"update {table} set {string.Join(", ", items.ToArray())} where [{structure.Key.Field}]='{key}'";
  247. var execute = Execute(sql, ps, false);
  248. if (execute.Success) return null;
  249. return execute.Message;
  250. }
  251. /// <summary></summary>
  252. public override string ConnectionString => _connstr;
  253. /// <summary></summary>
  254. protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new SqlDataAdapter((SqlCommand)command);
  255. /// <summary></summary>
  256. protected override IDbConnection GetConnection()
  257. {
  258. if (_conn == null) _conn = new SqlConnection(_connstr);
  259. return _conn;
  260. }
  261. /// <summary></summary>
  262. protected override IDataParameter CreateParameter() => new SqlParameter();
  263. /// <summary></summary>
  264. protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
  265. {
  266. if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
  267. else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
  268. }
  269. /// <summary></summary>
  270. protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
  271. {
  272. if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
  273. else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
  274. }
  275. /// <summary></summary>
  276. protected override string List(string tableName, string flagField, long flagValue)
  277. {
  278. if (flagValue == 0) return $"select * from [{tableName}]";
  279. else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
  280. }
  281. #endregion
  282. #region special
  283. /// <summary>清空与指定连接关联的连接池。</summary>
  284. public static void ClearPool(IDbConnection connection)
  285. {
  286. var special = connection as SqlConnection;
  287. if (special != null)
  288. {
  289. try
  290. {
  291. SqlConnection.ClearPool(special);
  292. }
  293. catch { }
  294. }
  295. }
  296. /// <summary>清空与指定连接关联的连接池。</summary>
  297. public static void ClearPool(IDbAdo instance)
  298. {
  299. var connection = instance?.Connection;
  300. if (connection != null) ClearPool(connection);
  301. }
  302. /// <summary>清空连接池。</summary>
  303. public static void ClearAllPools()
  304. {
  305. try
  306. {
  307. SqlConnection.ClearAllPools();
  308. }
  309. catch { }
  310. }
  311. /// <summary>获取列信息。</summary>
  312. public virtual ColumnInfo[] ColumnsInfo(string tableName)
  313. {
  314. if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
  315. var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
  316. using (var query = Query(sql))
  317. {
  318. var ab = new ArrayBuilder<ColumnInfo>();
  319. for (var i = 0; i < query.Rows; i++)
  320. {
  321. var info = new ColumnInfo();
  322. info.Name = query.Text(i, "name");
  323. info.Type = XType(query.Int32(i, "xtype"));
  324. info.Length = query.Int32(i, "length");
  325. ab.Add(info);
  326. }
  327. return ab.Export();
  328. }
  329. }
  330. /// <summary>创建索引。</summary>
  331. /// <remarks>索引名称已存在或无索引字段时,不抛出异常。</remarks>
  332. public virtual string Initialize(TableIndex index)
  333. {
  334. if (index == null)
  335. {
  336. if (ThrowAdoException) throw new ArgumentNullException(nameof(index));
  337. return $"参数 {nameof(index)} 无效。";
  338. }
  339. // 查询索引是否存在,若存在则直接退出。
  340. var sql1 = "select distinct i.name from sys.indexes i left join sys.all_objects o on i.object_id = o.object_id where i.name is not null and o.type = 'U' and i.is_primary_key = 0 and i.is_unique = 0";
  341. var names = this.Column(sql1, false);
  342. var exist = names.Find(x => string.Equals(x, index.Name, StringComparison.CurrentCultureIgnoreCase));
  343. if (exist.NotEmpty()) return $"索引已存在。";
  344. // 执行创建。
  345. var fields = new List<string>();
  346. foreach (var field in index.Fields)
  347. {
  348. if (field.Order == Order.Ascend) fields.Add($"[{field.Column.Field}]");
  349. else fields.Add($"[{field.Column.Field}] desc");
  350. }
  351. if (fields.IsEmpty()) return "无索引字段。";
  352. var sql2 = $"create index {index.Name} on [{index.Table.Name}] ({fields.Join(", ")})";
  353. var execute = Execute(sql2);
  354. if (!execute.Success)
  355. {
  356. if (ThrowAdoException) throw new SqlException(execute.Message);
  357. return execute.Message;
  358. }
  359. return null;
  360. }
  361. /// <summary>批量插入,必须在 DataTable 中指定表名,或指定 tableName 参数。</summary>
  362. /// <exception cref="ArgumentNullException"></exception>
  363. /// <exception cref="ArgumentException"></exception>
  364. /// <exception cref="Exception"></exception>
  365. public virtual void BulkCopy(DataTable table, string tableName = null)
  366. {
  367. // 检查 table 参数。
  368. if (table == null) throw new ArgumentNullException(nameof(table));
  369. if (table.Rows.Count < 1) return;
  370. // 检查 tableName 参数。
  371. if (tableName.IsEmpty()) tableName = table.TableName;
  372. if (tableName.IsEmpty()) throw new ArgumentException("未指定表名。");
  373. // 连接数据库。
  374. var connect = Connect();
  375. if (connect.NotEmpty()) throw new Exception(connect);
  376. // 准备参数。
  377. var options = SqlBulkCopyOptions.Default;
  378. var trans = Transaction as SqlTransaction;
  379. if (trans == null) options |= SqlBulkCopyOptions.UseInternalTransaction;
  380. // 批量插入。
  381. var bc = null as SqlBulkCopy;
  382. try
  383. {
  384. bc = new SqlBulkCopy((SqlConnection)Connection, options, trans);
  385. bc.DestinationTableName = tableName;
  386. bc.BatchSize = table.Rows.Count;
  387. bc.WriteToServer(table);
  388. try { bc.Close(); } catch { }
  389. }
  390. catch (Exception ex)
  391. {
  392. try { bc.Close(); } catch { }
  393. throw ex;
  394. }
  395. }
  396. /// <summary>创建数据库,返回错误信息。</summary>
  397. /// <param name="name">数据库名称。</param>
  398. /// <param name="logMaxSizeMB">日志文件的最大 MB 数,指定非正数将不限制增长。</param>
  399. /// <returns>成功时候返回 NULL 值,失败时返回错误信息。</returns>
  400. public virtual string CreateStore(string name, int logMaxSizeMB = 1024)
  401. {
  402. var store = name.Escape().ToTrim();
  403. if (store.IsEmpty()) return "创建失败:未指定数据库名称。";
  404. if (ConnectionString.IsEmpty()) return "创建失败:未指定数据库连接方式。";
  405. using (var source = new SqlClient(ConnectionString))
  406. {
  407. var connect = source.Connect();
  408. if (connect.NotEmpty()) return "创建失败:" + connect;
  409. var schema = source.Cell("select default_schema_name from sys.database_principals where type = 'S' and name=user_name()");
  410. if (schema.IsEmpty()) return "创建失败:无法获取默认模式名称。";
  411. var refPath = source.Cell(@"select f.physical_name path from sys.filegroups g left join sys.database_files f on f.data_space_id = g.data_space_id where g.name = 'PRIMARY' and g.type = 'FG' and g.is_default = 1 and g.filegroup_guid is null");
  412. if (refPath.IsEmpty()) return "创建失败:无法获取文件路径。";
  413. var win = refPath.Substring(1, 2) == ":\\";
  414. var nix = refPath.StartsWith("/");
  415. if (!win && !nix) return "创建失败:暂不支持该服务器。";
  416. var mdfPath = store + ".mdf";
  417. var ldfPath = store + ".ldf";
  418. if (win)
  419. {
  420. var refSplit = refPath.Split('\\');
  421. var dir = string.Join("\\", refSplit, 0, refSplit.Length - 1);
  422. mdfPath = dir + "\\" + mdfPath;
  423. ldfPath = dir + "\\" + ldfPath;
  424. }
  425. if (nix)
  426. {
  427. var refSplit = refPath.Split('/');
  428. var dir = string.Join("/", refSplit, 0, refSplit.Length - 1);
  429. mdfPath = dir + "/" + mdfPath;
  430. ldfPath = dir + "/" + ldfPath;
  431. }
  432. // 创建库。
  433. var maxLog = logMaxSizeMB > 0 ? $"{logMaxSizeMB}MB" : "UNLIMITED";
  434. var sql1 = $@"
  435. CREATE DATABASE [{store}]
  436. ON PRIMARY
  437. (
  438. NAME = N'{store}',
  439. FILENAME = N'{mdfPath}',
  440. SIZE = 4MB,
  441. MAXSIZE = UNLIMITED,
  442. FILEGROWTH = 4MB
  443. )
  444. LOG ON
  445. (
  446. NAME = N'{store}_log',
  447. FILENAME = N'{ldfPath}',
  448. SIZE = 1MB,
  449. MAXSIZE = {maxLog},
  450. FILEGROWTH = 1MB
  451. )
  452. COLLATE Chinese_PRC_CI_AS
  453. ";
  454. var create = source.Execute(sql1, null, false);
  455. if (!create.Success) return TextUtility.Merge("创建失败:", create.Message);
  456. // 设置兼容性级别。
  457. var sql2 = $"alter database [{store}] set compatibility_level = 100";
  458. source.Execute(sql2, null, false);
  459. // 设置恢复默认为“简单”
  460. var sql3 = $"alter database [{store}] set recovery simple";
  461. source.Execute(sql3, null, false);
  462. return null;
  463. }
  464. }
  465. static string XType(int xtype)
  466. {
  467. switch (xtype)
  468. {
  469. case 34: return "image";
  470. case 35: return "text";
  471. case 36: return "uniqueidentifier";
  472. case 48: return "tinyint";
  473. case 52: return "smallint";
  474. case 56: return "int";
  475. case 58: return "smalldatetime";
  476. case 59: return "real";
  477. case 60: return "money";
  478. case 61: return "datetime";
  479. case 62: return "float";
  480. case 98: return "sql_variant";
  481. case 99: return "ntext";
  482. case 104: return "bit";
  483. case 106: return "decimal";
  484. case 108: return "numeric";
  485. case 122: return "smallmoney";
  486. case 127: return "bigint";
  487. case 165: return "varbinary";
  488. case 167: return "varchar";
  489. case 173: return "binary";
  490. case 175: return "char";
  491. case 189: return "timestamp";
  492. case 231: return "nvarchar";
  493. case 239: return "nchar";
  494. case 241: return "xml";
  495. }
  496. return null;
  497. }
  498. #if NET20 || NET40
  499. /// <summary>枚举本地网络中的 SQL Server 实例的信息。</summary>
  500. public static Source[] EnumerateSources()
  501. {
  502. var list = new List<Source>();
  503. // 表中列名:ServerName、InstanceName、IsClustered、Version。
  504. using (var table = SqlDataSourceEnumerator.Instance.GetDataSources())
  505. {
  506. var query = new Query(table);
  507. var rows = query.Rows;
  508. list.Capacity = rows;
  509. for (int i = 0; i < rows; i++)
  510. {
  511. var item = new Source();
  512. item.ServerName = query.Text(i, "ServerName");
  513. list.Add(item);
  514. }
  515. }
  516. return list.ToArray();
  517. }
  518. /// <summary>SQL Server 实例的信息。</summary>
  519. public sealed class Source
  520. {
  521. /// <summary></summary>
  522. public string ServerName { get; set; }
  523. /// <summary></summary>
  524. public string InstanceName { get; set; }
  525. /// <summary></summary>
  526. public string IsClustered { get; set; }
  527. /// <summary></summary>
  528. public string Version { get; set; }
  529. }
  530. #endif
  531. /// <summary>创建参数。</summary>
  532. /// <exception cref="ArgumentNullException"></exception>
  533. /// <exception cref="InvalidOperationException"></exception>
  534. static SqlParameter Parameter(Parameter parameter)
  535. {
  536. if (parameter == null) throw new InvalidOperationException("参数无效。");
  537. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  538. }
  539. /// <summary>创建参数。</summary>
  540. public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
  541. {
  542. var vname = TextUtility.Trim(name);
  543. if (TextUtility.IsBlank(vname)) return null;
  544. var vtype = SqlDbType.BigInt;
  545. switch (type)
  546. {
  547. case ColumnType.Boolean:
  548. vtype = SqlDbType.Bit;
  549. break;
  550. case ColumnType.Bytes:
  551. vtype = SqlDbType.Image;
  552. break;
  553. case ColumnType.Integer:
  554. vtype = SqlDbType.BigInt;
  555. break;
  556. case ColumnType.Float:
  557. vtype = SqlDbType.Float;
  558. break;
  559. case ColumnType.DateTime:
  560. vtype = SqlDbType.DateTime;
  561. break;
  562. case ColumnType.VarChar:
  563. case ColumnType.VarChar191:
  564. case ColumnType.VarCharMax:
  565. vtype = SqlDbType.VarChar;
  566. break;
  567. case ColumnType.NVarChar:
  568. case ColumnType.NVarChar191:
  569. case ColumnType.NVarCharMax:
  570. vtype = SqlDbType.NVarChar;
  571. break;
  572. case ColumnType.Text:
  573. vtype = SqlDbType.Text;
  574. break;
  575. case ColumnType.NText:
  576. vtype = SqlDbType.NText;
  577. break;
  578. default:
  579. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  580. }
  581. var vsize = size;
  582. switch (type)
  583. {
  584. case ColumnType.VarChar:
  585. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  586. break;
  587. case ColumnType.NVarChar:
  588. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  589. break;
  590. case ColumnType.VarChar191:
  591. case ColumnType.NVarChar191:
  592. vsize = NumberUtility.Restrict(vsize, 0, 191);
  593. break;
  594. default:
  595. vsize = 0;
  596. break;
  597. }
  598. var vvalue = value ?? DBNull.Value;
  599. if (vvalue is string && vvalue != null && vsize > 0)
  600. {
  601. vvalue = TextUtility.Left((string)vvalue, vsize);
  602. }
  603. var parameter = new SqlParameter();
  604. parameter.ParameterName = vname;
  605. parameter.SqlDbType = vtype;
  606. parameter.Value = vvalue;
  607. if (vsize > 0) parameter.Size = vsize;
  608. return parameter;
  609. }
  610. /// <summary>创建参数。</summary>
  611. public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
  612. {
  613. if (value is string && value != null && size > 0)
  614. {
  615. value = TextUtility.Left((string)value, (int)size);
  616. }
  617. var p = new SqlParameter();
  618. p.ParameterName = name ?? "";
  619. p.SqlDbType = type;
  620. p.Size = size;
  621. p.Value = value ?? DBNull.Value;
  622. return p;
  623. }
  624. /// <summary>创建参数。</summary>
  625. public static SqlParameter Parameter(string name, SqlDbType type, object value)
  626. {
  627. var p = new SqlParameter();
  628. p.ParameterName = name ?? "";
  629. p.SqlDbType = type;
  630. p.Value = value ?? DBNull.Value;
  631. return p;
  632. }
  633. static string Declaration(ColumnAttribute column)
  634. {
  635. var type = TextUtility.Empty;
  636. var vcolumn = column;
  637. var length = Math.Max(0, vcolumn.Length);
  638. switch (vcolumn.Type)
  639. {
  640. case ColumnType.Boolean:
  641. type = "bit";
  642. break;
  643. case ColumnType.Integer:
  644. type = "bigint";
  645. break;
  646. case ColumnType.Float:
  647. type = "float";
  648. break;
  649. case ColumnType.Bytes:
  650. type = "image";
  651. break;
  652. case ColumnType.DateTime:
  653. type = "datetime";
  654. break;
  655. case ColumnType.VarChar:
  656. type = $"varchar({Math.Min(8000, length)})";
  657. break;
  658. case ColumnType.VarChar191:
  659. type = "varchar(191)";
  660. break;
  661. case ColumnType.VarCharMax:
  662. type = "varchar(max)";
  663. break;
  664. case ColumnType.Text:
  665. type = "text";
  666. break;
  667. case ColumnType.NVarChar:
  668. type = $"nvarchar({Math.Min(4000, length)})";
  669. break;
  670. case ColumnType.NVarChar191:
  671. type = "nvarchar(191)";
  672. break;
  673. case ColumnType.NVarCharMax:
  674. type = "nvarchar(max)";
  675. break;
  676. case ColumnType.NText:
  677. type = "ntext";
  678. break;
  679. default:
  680. return TextUtility.Empty;
  681. }
  682. return $"[{vcolumn.Field}] {type}";
  683. }
  684. #endregion
  685. }
  686. }