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.

701 lines
27 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
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
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
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
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. foreach (var column in structure.Columns)
  96. {
  97. // 检查 Independent 特性。
  98. if (structure.Independent && column.Independent) continue;
  99. // 去重。
  100. var lower = column.Field.ToLower();
  101. if (columns.Contains(lower)) continue;
  102. var type = Declaration(column);
  103. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  104. var sql = TextUtility.Merge("alter table [", table, "] add ", type, "; ");
  105. var execute = Execute(sql, null, false);
  106. if (execute.Success == false) return execute.Message;
  107. }
  108. return TextUtility.Empty;
  109. }
  110. else
  111. {
  112. var sqlcolumns = new List<string>();
  113. foreach (var column in structure.Columns)
  114. {
  115. // 检查 Independent 特性。
  116. if (structure.Independent && column.Independent) continue;
  117. var type = Declaration(column);
  118. if (!column.Independent)
  119. {
  120. if (column.PrimaryKey) type = type + " primary key";
  121. if (column.Incremental) type += " identity";
  122. }
  123. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  124. sqlcolumns.Add(type);
  125. }
  126. if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表:没有定义任何字段。";
  127. var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  128. var execute = Execute(sql, null, false);
  129. if (execute.Success) return TextUtility.Empty;
  130. return execute.Message;
  131. }
  132. }
  133. /// <summary>插入记录。返回错误信息。</summary>
  134. public override string Insert(object record, string table = null, bool adjust = true)
  135. {
  136. if (record == null) return "参数无效。";
  137. if (adjust) FixProperties(record);
  138. var structure = TableStructure.Parse(record.GetType());
  139. if (structure == null) return "无法解析记录模型。";
  140. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  141. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  142. // 排除字段。
  143. var excluded = new List<string>();
  144. foreach (var ca in structure.Columns)
  145. {
  146. // 排除不使用 ORM 的属性。
  147. if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
  148. }
  149. var ps = structure.CreateParameters(record, Parameter, excluded);
  150. var psc = ps.Length;
  151. if (psc < 1) return "数据模型不包含字段。";
  152. var names = new List<string>(psc);
  153. var values = new List<string>(psc);
  154. foreach (var column in ps)
  155. {
  156. names.Add($"[{column.ParameterName}]");
  157. values.Add("@" + column.ParameterName);
  158. }
  159. var sb = new StringBuilder();
  160. sb.Append("insert into ", table, "(", string.Join(", ", names.ToArray()), ") ");
  161. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  162. var sql = sb.ToString();
  163. var execute = Execute(sql, ps, false);
  164. if (execute.Success) return TextUtility.Empty;
  165. return execute.Message;
  166. }
  167. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  168. /// <remarks>不更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  169. public override string Update(IRecord record, string table = null, bool adjust = true)
  170. {
  171. if (record == null) return "参数无效。";
  172. if (adjust)
  173. {
  174. FixProperties(record);
  175. SetUpdated(record);
  176. }
  177. var structure = TableStructure.Parse(record.GetType());
  178. if (structure == null) return "无法解析记录模型。";
  179. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  180. if (string.IsNullOrEmpty(table)) table = structure.TableName;
  181. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  182. // 排除字段。
  183. var excluded = new List<string>();
  184. if (structure.Key != null) excluded.Add(structure.Key.Field);
  185. foreach (var ca in structure.Columns)
  186. {
  187. // 排除不使用 ORM 的属性、自增属性和主键属性。
  188. if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
  189. }
  190. var ps = structure.CreateParameters(record, Parameter, excluded);
  191. var psc = ps.Length;
  192. if (psc < 1) return "数据模型不包含字段。";
  193. var items = new List<string>();
  194. foreach (var p in ps)
  195. {
  196. var pn = p.ParameterName;
  197. items.Add($"[{pn}] = @{pn}");
  198. }
  199. var key = record.Key.SafeKey();
  200. var sql = $"update {table} set {string.Join(", ", items.ToArray())} where [{structure.Key.Field}]='{key}'";
  201. var execute = Execute(sql, ps, false);
  202. if (execute.Success) return TextUtility.Empty;
  203. return execute.Message;
  204. }
  205. /// <summary></summary>
  206. public override string ConnectionString => _connstr;
  207. /// <summary></summary>
  208. protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new SqlDataAdapter((SqlCommand)command);
  209. /// <summary></summary>
  210. protected override IDbConnection GetConnection()
  211. {
  212. if (_conn == null) _conn = new SqlConnection(_connstr);
  213. return _conn;
  214. }
  215. /// <summary></summary>
  216. protected override IDataParameter CreateParameter() => new SqlParameter();
  217. /// <summary></summary>
  218. protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
  219. {
  220. if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
  221. else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
  222. }
  223. /// <summary></summary>
  224. protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
  225. {
  226. if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
  227. else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
  228. }
  229. /// <summary></summary>
  230. protected override string List(string tableName, string flagField, long flagValue)
  231. {
  232. if (flagValue == 0) return $"select * from [{tableName}]";
  233. else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
  234. }
  235. #endregion
  236. #region special
  237. /// <summary>清空与指定连接关联的连接池。</summary>
  238. public static void ClearPool(IDbConnection connection)
  239. {
  240. var special = connection as SqlConnection;
  241. if (special != null)
  242. {
  243. try
  244. {
  245. SqlConnection.ClearPool(special);
  246. }
  247. catch { }
  248. }
  249. }
  250. /// <summary>清空与指定连接关联的连接池。</summary>
  251. public static void ClearPool(IDbAdo instance)
  252. {
  253. var connection = instance?.Connection;
  254. if (connection != null) ClearPool(connection);
  255. }
  256. /// <summary>清空连接池。</summary>
  257. public static void ClearAllPools()
  258. {
  259. try
  260. {
  261. SqlConnection.ClearAllPools();
  262. }
  263. catch { }
  264. }
  265. /// <summary>获取列信息。</summary>
  266. public ColumnInfo[] ColumnsInfo(string tableName)
  267. {
  268. if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
  269. var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
  270. using (var query = Query(sql))
  271. {
  272. var ab = new ArrayBuilder<ColumnInfo>();
  273. for (var i = 0; i < query.Rows; i++)
  274. {
  275. var info = new ColumnInfo();
  276. info.Name = query.Text(i, "name");
  277. info.Type = XType(query.Int32(i, "xtype"));
  278. info.Length = query.Int32(i, "length");
  279. ab.Add(info);
  280. }
  281. return ab.Export();
  282. }
  283. }
  284. /// <summary>批量插入,必须在 DataTable 中指定表名,或指定 tableName 参数。</summary>
  285. /// <exception cref="ArgumentNullException"></exception>
  286. /// <exception cref="ArgumentException"></exception>
  287. /// <exception cref="Exception"></exception>
  288. public void BulkCopy(DataTable table, string tableName = null)
  289. {
  290. // 检查 table 参数。
  291. if (table == null) throw new ArgumentNullException(nameof(table));
  292. if (table.Rows.Count < 1) return;
  293. // 检查 tableName 参数。
  294. if (tableName.IsEmpty()) tableName = table.TableName;
  295. if (tableName.IsEmpty()) throw new ArgumentException("未指定表名。");
  296. // 连接数据库。
  297. var connect = Connect();
  298. if (connect.NotEmpty()) throw new Exception(connect);
  299. // 准备参数。
  300. var options = SqlBulkCopyOptions.Default;
  301. var trans = Transaction as SqlTransaction;
  302. if (trans == null) options |= SqlBulkCopyOptions.UseInternalTransaction;
  303. // 批量插入。
  304. var bc = null as SqlBulkCopy;
  305. try
  306. {
  307. bc = new SqlBulkCopy((SqlConnection)Connection, options, trans);
  308. bc.DestinationTableName = tableName;
  309. bc.BatchSize = table.Rows.Count;
  310. bc.WriteToServer(table);
  311. try { bc.Close(); } catch { }
  312. }
  313. catch (Exception ex)
  314. {
  315. try { bc.Close(); } catch { }
  316. throw ex;
  317. }
  318. }
  319. /// <summary>创建数据库,返回错误信息。</summary>
  320. /// <param name="name">数据库名称。</param>
  321. /// <param name="logMaxSizeMB">日志文件的最大 MB 数,指定非正数将不限制增长。</param>
  322. /// <returns>成功时候返回 NULL 值,失败时返回错误信息。</returns>
  323. public string CreateStore(string name, int logMaxSizeMB = 1024)
  324. {
  325. var store = name.Escape().ToTrim();
  326. if (store.IsEmpty()) return "创建失败:未指定数据库名称。";
  327. if (ConnectionString.IsEmpty()) return "创建失败:未指定数据库连接方式。";
  328. using (var source = new SqlClient(ConnectionString))
  329. {
  330. var connect = source.Connect();
  331. if (connect.NotEmpty()) return "创建失败:" + connect;
  332. var schema = source.Cell("select default_schema_name from sys.database_principals where type = 'S' and name=user_name()");
  333. if (schema.IsEmpty()) return "创建失败:无法获取默认模式名称。";
  334. 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");
  335. if (refPath.IsEmpty()) return "创建失败:无法获取文件路径。";
  336. var win = refPath.Substring(1, 2) == ":\\";
  337. var nix = refPath.StartsWith("/");
  338. if (!win && !nix) return "创建失败:暂不支持该服务器。";
  339. var mdfPath = store + ".mdf";
  340. var ldfPath = store + ".ldf";
  341. if (win)
  342. {
  343. var refSplit = refPath.Split('\\');
  344. var dir = string.Join("\\", refSplit, 0, refSplit.Length - 1);
  345. mdfPath = dir + "\\" + mdfPath;
  346. ldfPath = dir + "\\" + ldfPath;
  347. }
  348. if (nix)
  349. {
  350. var refSplit = refPath.Split('/');
  351. var dir = string.Join("/", refSplit, 0, refSplit.Length - 1);
  352. mdfPath = dir + "/" + mdfPath;
  353. ldfPath = dir + "/" + ldfPath;
  354. }
  355. // 创建库。
  356. var maxLog = logMaxSizeMB > 0 ? $"{logMaxSizeMB}MB" : "UNLIMITED";
  357. var sql1 = $@"
  358. CREATE DATABASE [{store}]
  359. ON PRIMARY
  360. (
  361. NAME = N'{store}',
  362. FILENAME = N'{mdfPath}',
  363. SIZE = 4MB,
  364. MAXSIZE = UNLIMITED,
  365. FILEGROWTH = 4MB
  366. )
  367. LOG ON
  368. (
  369. NAME = N'{store}_log',
  370. FILENAME = N'{ldfPath}',
  371. SIZE = 1MB,
  372. MAXSIZE = {maxLog},
  373. FILEGROWTH = 1MB
  374. )
  375. COLLATE Chinese_PRC_CI_AS
  376. ";
  377. var create = source.Execute(sql1, null, false);
  378. if (!create.Success) return TextUtility.Merge("创建失败:", create.Message);
  379. // 设置兼容性级别。
  380. var sql2 = $"alter database [{store}] set compatibility_level = 100";
  381. source.Execute(sql2, null, false);
  382. // 设置恢复默认为“简单”
  383. var sql3 = $"alter database [{store}] set recovery simple";
  384. source.Execute(sql3, null, false);
  385. return null;
  386. }
  387. }
  388. static string XType(int xtype)
  389. {
  390. switch (xtype)
  391. {
  392. case 34: return "image";
  393. case 35: return "text";
  394. case 36: return "uniqueidentifier";
  395. case 48: return "tinyint";
  396. case 52: return "smallint";
  397. case 56: return "int";
  398. case 58: return "smalldatetime";
  399. case 59: return "real";
  400. case 60: return "money";
  401. case 61: return "datetime";
  402. case 62: return "float";
  403. case 98: return "sql_variant";
  404. case 99: return "ntext";
  405. case 104: return "bit";
  406. case 106: return "decimal";
  407. case 108: return "numeric";
  408. case 122: return "smallmoney";
  409. case 127: return "bigint";
  410. case 165: return "varbinary";
  411. case 167: return "varchar";
  412. case 173: return "binary";
  413. case 175: return "char";
  414. case 189: return "timestamp";
  415. case 231: return "nvarchar";
  416. case 239: return "nchar";
  417. case 241: return "xml";
  418. }
  419. return null;
  420. }
  421. #if NET20 || NET40
  422. /// <summary>枚举本地网络中的 SQL Server 实例的信息。</summary>
  423. public static Source[] EnumerateSources()
  424. {
  425. var list = new List<Source>();
  426. // 表中列名:ServerName、InstanceName、IsClustered、Version。
  427. using (var table = SqlDataSourceEnumerator.Instance.GetDataSources())
  428. {
  429. var query = new Query(table);
  430. var rows = query.Rows;
  431. list.Capacity = rows;
  432. for (int i = 0; i < rows; i++)
  433. {
  434. var item = new Source();
  435. item.ServerName = query.Text(i, "ServerName");
  436. list.Add(item);
  437. }
  438. }
  439. return list.ToArray();
  440. }
  441. /// <summary>SQL Server 实例的信息。</summary>
  442. public sealed class Source
  443. {
  444. /// <summary></summary>
  445. public string ServerName { get; set; }
  446. /// <summary></summary>
  447. public string InstanceName { get; set; }
  448. /// <summary></summary>
  449. public string IsClustered { get; set; }
  450. /// <summary></summary>
  451. public string Version { get; set; }
  452. }
  453. #endif
  454. /// <summary>创建参数。</summary>
  455. /// <exception cref="ArgumentNullException"></exception>
  456. /// <exception cref="InvalidOperationException"></exception>
  457. static SqlParameter Parameter(Parameter parameter)
  458. {
  459. if (parameter == null) throw new InvalidOperationException("参数无效。");
  460. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  461. }
  462. /// <summary>创建参数。</summary>
  463. public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
  464. {
  465. var vname = TextUtility.Trim(name);
  466. if (TextUtility.IsBlank(vname)) return null;
  467. var vtype = SqlDbType.BigInt;
  468. switch (type)
  469. {
  470. case ColumnType.Boolean:
  471. vtype = SqlDbType.Bit;
  472. break;
  473. case ColumnType.Bytes:
  474. vtype = SqlDbType.Image;
  475. break;
  476. case ColumnType.Integer:
  477. vtype = SqlDbType.BigInt;
  478. break;
  479. case ColumnType.Float:
  480. vtype = SqlDbType.Float;
  481. break;
  482. case ColumnType.DateTime:
  483. vtype = SqlDbType.DateTime;
  484. break;
  485. case ColumnType.VarChar:
  486. case ColumnType.VarChar191:
  487. case ColumnType.VarCharMax:
  488. vtype = SqlDbType.VarChar;
  489. break;
  490. case ColumnType.NVarChar:
  491. case ColumnType.NVarChar191:
  492. case ColumnType.NVarCharMax:
  493. vtype = SqlDbType.NVarChar;
  494. break;
  495. case ColumnType.Text:
  496. vtype = SqlDbType.Text;
  497. break;
  498. case ColumnType.NText:
  499. vtype = SqlDbType.NText;
  500. break;
  501. default:
  502. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  503. }
  504. var vsize = size;
  505. switch (type)
  506. {
  507. case ColumnType.VarChar:
  508. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  509. break;
  510. case ColumnType.NVarChar:
  511. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  512. break;
  513. case ColumnType.VarChar191:
  514. case ColumnType.NVarChar191:
  515. vsize = NumberUtility.Restrict(vsize, 0, 191);
  516. break;
  517. default:
  518. vsize = 0;
  519. break;
  520. }
  521. var vvalue = value ?? DBNull.Value;
  522. if (vvalue is string && vvalue != null && vsize > 0)
  523. {
  524. vvalue = TextUtility.Left((string)vvalue, vsize);
  525. }
  526. var parameter = new SqlParameter();
  527. parameter.ParameterName = vname;
  528. parameter.SqlDbType = vtype;
  529. parameter.Value = vvalue;
  530. if (vsize > 0) parameter.Size = vsize;
  531. return parameter;
  532. }
  533. /// <summary>创建参数。</summary>
  534. public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
  535. {
  536. if (value is string && value != null && size > 0)
  537. {
  538. value = TextUtility.Left((string)value, (int)size);
  539. }
  540. var p = new SqlParameter();
  541. p.ParameterName = name ?? "";
  542. p.SqlDbType = type;
  543. p.Size = size;
  544. p.Value = value ?? DBNull.Value;
  545. return p;
  546. }
  547. /// <summary>创建参数。</summary>
  548. public static SqlParameter Parameter(string name, SqlDbType type, object value)
  549. {
  550. var p = new SqlParameter();
  551. p.ParameterName = name ?? "";
  552. p.SqlDbType = type;
  553. p.Value = value ?? DBNull.Value;
  554. return p;
  555. }
  556. static string Declaration(ColumnAttribute column)
  557. {
  558. var type = TextUtility.Empty;
  559. var vcolumn = column;
  560. var length = Math.Max(0, vcolumn.Length);
  561. switch (vcolumn.Type)
  562. {
  563. case ColumnType.Boolean:
  564. type = "bit";
  565. break;
  566. case ColumnType.Integer:
  567. type = "bigint";
  568. break;
  569. case ColumnType.Float:
  570. type = "float";
  571. break;
  572. case ColumnType.Bytes:
  573. type = "image";
  574. break;
  575. case ColumnType.DateTime:
  576. type = "datetime";
  577. break;
  578. case ColumnType.VarChar:
  579. type = $"varchar({Math.Min(8000, length)})";
  580. break;
  581. case ColumnType.VarChar191:
  582. type = "varchar(191)";
  583. break;
  584. case ColumnType.VarCharMax:
  585. type = "varchar(max)";
  586. break;
  587. case ColumnType.Text:
  588. type = "text";
  589. break;
  590. case ColumnType.NVarChar:
  591. type = $"nvarchar({Math.Min(4000, length)})";
  592. break;
  593. case ColumnType.NVarChar191:
  594. type = "nvarchar(191)";
  595. break;
  596. case ColumnType.NVarCharMax:
  597. type = "nvarchar(max)";
  598. break;
  599. case ColumnType.NText:
  600. type = "ntext";
  601. break;
  602. default:
  603. return TextUtility.Empty;
  604. }
  605. return $"[{vcolumn.Field}] {type}";
  606. }
  607. #endregion
  608. }
  609. }