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.

818 lines
30 KiB

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
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
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
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
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
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
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
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
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
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
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
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
4 years ago
4 years ago
  1. /* 2021.11.07 */
  2. using Apewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Text;
  8. using static Apewer.Source.SourceUtility;
  9. using System.Data.SqlClient;
  10. #if NETFRAMEWORK
  11. using System.Data.Sql;
  12. #endif
  13. namespace Apewer.Source
  14. {
  15. /// <summary></summary>
  16. [Serializable]
  17. public class SqlClient : IDbClient
  18. {
  19. #region 变量、构造函数
  20. private Timeout _timeout = null;
  21. private string _connectionstring = "";
  22. /// <summary>获取或设置日志记录。</summary>
  23. public Logger Logger { get; set; }
  24. /// <summary>超时设定。</summary>
  25. public Timeout Timeout { get => _timeout; }
  26. /// <summary>使用连接字符串创建数据库连接实例。</summary>
  27. public SqlClient(string connectionString, Timeout timeout = null)
  28. {
  29. _timeout = timeout ?? Timeout.Default;
  30. _connectionstring = connectionString ?? "";
  31. }
  32. /// <summary>使用连接凭据创建数据库连接实例。</summary>
  33. public SqlClient(string address, string store, string user, string pass, Timeout timeout = null)
  34. {
  35. if (timeout == null) timeout = Timeout.Default;
  36. var a = address ?? "";
  37. var s = store ?? "";
  38. var u = user ?? "";
  39. var p = pass ?? "";
  40. var cs = $"data source = {a}; initial catalog = {s}; ";
  41. if (string.IsNullOrEmpty(u)) cs += "integrated security = sspi; ";
  42. else
  43. {
  44. cs += $"user id = {u}; ";
  45. if (!string.IsNullOrEmpty(p)) cs += $"password = {p}; ";
  46. }
  47. cs += $"connection timeout = {timeout.Connect}; ";
  48. _timeout = timeout ?? Timeout.Default;
  49. _connectionstring = cs;
  50. }
  51. #endregion
  52. #region Ado - Connection
  53. private SqlConnection _db = null;
  54. /// <summary>连接字符串。</summary>
  55. public string ConnectionString { get => _connectionstring; }
  56. /// <summary>获取当前的 SqlConnection 对象。</summary>
  57. public IDbConnection Connection { get => _db; }
  58. /// <summary>数据库是否已经连接。</summary>
  59. public bool Online
  60. {
  61. get
  62. {
  63. if (_db == null) return false;
  64. return (_db.State == ConnectionState.Open);
  65. }
  66. }
  67. /// <summary>连接数据库,若未连接则尝试连接,获取连接成功的状态。</summary>
  68. public string Connect()
  69. {
  70. if (_db == null)
  71. {
  72. _db = new SqlConnection();
  73. _db.ConnectionString = _connectionstring;
  74. }
  75. else
  76. {
  77. if (_db.State == ConnectionState.Open) return null;
  78. }
  79. try
  80. {
  81. _db.Open();
  82. switch (_db.State)
  83. {
  84. case ConnectionState.Open: return null;
  85. default: return $"连接失败,当前处于 {_db.State} 状态。";
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.Error(nameof(SqlClient), "Connection", ex, _db.ConnectionString);
  91. Close();
  92. return ex.Message;
  93. }
  94. }
  95. /// <summary>关闭连接,并释放对象所占用的系统资源。</summary>
  96. public void Close()
  97. {
  98. if (_db != null)
  99. {
  100. if (_transaction != null)
  101. {
  102. if (_autocommit) Commit();
  103. else Rollback();
  104. }
  105. _db.Close();
  106. _db.Dispose();
  107. _db = null;
  108. }
  109. }
  110. /// <summary>关闭连接,释放对象所占用的系统资源,并清除连接信息。</summary>
  111. public void Dispose() => Close();
  112. #endregion
  113. #region Ado - Transaction
  114. private IDbTransaction _transaction = null;
  115. private bool _autocommit = false;
  116. /// <summary>启动事务。</summary>
  117. public string Begin(bool commit = true) => Begin(commit, null);
  118. /// <summary>启动事务。</summary>
  119. public string Begin(bool commit, Class<IsolationLevel> isolation)
  120. {
  121. var connect = Connect();
  122. if (connect.NotEmpty()) return connect;
  123. if (_transaction != null) return "存在已启动的事务,无法再次启动。";
  124. try
  125. {
  126. _transaction = isolation ? _db.BeginTransaction(isolation.Value) : _db.BeginTransaction();
  127. _autocommit = commit;
  128. return null;
  129. }
  130. catch (Exception ex)
  131. {
  132. Logger.Error(nameof(SqlClient), "Begin", ex.Message());
  133. return ex.Message();
  134. }
  135. }
  136. /// <summary>提交事务。</summary>
  137. public string Commit()
  138. {
  139. if (_transaction == null) return "事务不存在。";
  140. try
  141. {
  142. _transaction.Commit();
  143. RuntimeUtility.Dispose(_transaction);
  144. _transaction = null;
  145. return null;
  146. }
  147. catch (Exception ex)
  148. {
  149. RuntimeUtility.Dispose(_transaction);
  150. _transaction = null;
  151. Logger.Error(nameof(SqlClient), "Commit", ex.Message());
  152. return ex.Message();
  153. }
  154. }
  155. /// <summary>从挂起状态回滚事务。</summary>
  156. public string Rollback()
  157. {
  158. if (_transaction == null) return "事务不存在。";
  159. try
  160. {
  161. _transaction.Rollback();
  162. RuntimeUtility.Dispose(_transaction);
  163. _transaction = null;
  164. return null;
  165. }
  166. catch (Exception ex)
  167. {
  168. RuntimeUtility.Dispose(_transaction);
  169. _transaction = null;
  170. Logger.Error(nameof(SqlClient), "Rollback", ex.Message);
  171. return ex.Message();
  172. }
  173. }
  174. #endregion
  175. #region Ado - SQL
  176. /// <summary>查询。</summary>
  177. public IQuery Query(string sql) => Query(sql, null);
  178. /// <summary>查询。</summary>
  179. public IQuery Query(string sql, IEnumerable<IDataParameter> parameters)
  180. {
  181. if (TextUtility.IsBlank(sql)) return Example.InvalidQueryStatement;
  182. var connected = Connect();
  183. if (connected.NotEmpty()) return new Query(false, connected);
  184. try
  185. {
  186. using (var command = new SqlCommand())
  187. {
  188. command.Connection = _db;
  189. command.CommandTimeout = _timeout.Query;
  190. command.CommandText = sql;
  191. if (parameters != null)
  192. {
  193. foreach (var p in parameters)
  194. {
  195. if (p != null) command.Parameters.Add(p);
  196. }
  197. }
  198. using (var dataset = new DataSet())
  199. {
  200. using (var da = new SqlDataAdapter(command))
  201. {
  202. var name = "table_" + Guid.NewGuid().ToString("n");
  203. da.Fill(dataset, name);
  204. var table = dataset.Tables[name];
  205. return new Query(table);
  206. }
  207. }
  208. }
  209. }
  210. catch (Exception exception)
  211. {
  212. Logger.Error(nameof(SqlClient), "Query", exception, sql);
  213. return new Query(exception);
  214. }
  215. }
  216. /// <summary>执行。</summary>
  217. public IExecute Execute(string sql) => Execute(sql, null);
  218. /// <summary>执行单条 Transact-SQL 语句,并加入参数。</summary>
  219. public IExecute Execute(string sql, IEnumerable<IDataParameter> parameters)
  220. {
  221. if (TextUtility.IsBlank(sql)) return Example.InvalidExecuteStatement;
  222. var connected = Connect();
  223. if (connected.NotEmpty()) return new Execute(false, connected);
  224. var inTransaction = _transaction != null;
  225. if (!inTransaction) Begin();
  226. try
  227. {
  228. using (var command = new SqlCommand())
  229. {
  230. command.Connection = _db;
  231. command.Transaction = (SqlTransaction)_transaction;
  232. command.CommandTimeout = _timeout.Execute;
  233. command.CommandText = sql;
  234. if (parameters != null)
  235. {
  236. foreach (var parameter in parameters)
  237. {
  238. if (parameter != null) command.Parameters.Add(parameter);
  239. }
  240. }
  241. var rows = command.ExecuteNonQuery();
  242. if (!inTransaction) Commit(); // todo 此处应该检查事务提交产生的错误。
  243. return new Execute(true, rows);
  244. }
  245. }
  246. catch (Exception exception)
  247. {
  248. Logger.Error(nameof(SqlClient), "Execute", exception, sql);
  249. if (!inTransaction) Rollback();
  250. return new Execute(exception);
  251. }
  252. }
  253. #endregion
  254. #region ORM
  255. /// <summary>查询数据库中的所有表名。</summary>
  256. public string[] TableNames()
  257. {
  258. var list = new List<string>();
  259. if (Connect().IsEmpty())
  260. {
  261. var sql = "select [name] from [sysobjects] where [type] = 'u' order by [name]; ";
  262. var query = (Query)Query(sql);
  263. for (int r = 0; r < query.Rows; r++)
  264. {
  265. var cell = query.Text(r, 0);
  266. if (TextUtility.IsBlank(cell)) continue;
  267. list.Add(cell);
  268. }
  269. query.Dispose();
  270. }
  271. return list.ToArray();
  272. }
  273. /// <summary>查询数据库实例中的所有数据库名。</summary>
  274. public string[] StoreNames()
  275. {
  276. var list = new List<string>();
  277. if (Connect().IsEmpty())
  278. {
  279. var sql = "select [name] from [master]..[sysdatabases] order by [name]; ";
  280. var query = (Query)Query(sql);
  281. for (int r = 0; r < query.Rows; r++)
  282. {
  283. var cell = query.Text(r, 0);
  284. if (TextUtility.IsBlank(cell)) continue;
  285. if (cell == "master") continue;
  286. if (cell == "model") continue;
  287. if (cell == "msdb") continue;
  288. if (cell == "tempdb") continue;
  289. list.Add(cell);
  290. }
  291. query.Dispose();
  292. }
  293. return list.ToArray();
  294. }
  295. /// <summary>查询表中的所有列名。</summary>
  296. public string[] ColumnNames(string tableName)
  297. {
  298. var list = new List<string>();
  299. if (Connect().IsEmpty())
  300. {
  301. var table = TextUtility.AntiInject(tableName);
  302. var sql = TextUtility.Merge("select [name] from [syscolumns] where [id] = object_id('", table, "'); ");
  303. var query = (Query)Query(sql);
  304. for (int r = 0; r < query.Rows; r++)
  305. {
  306. var cell = query.Text(r, 0);
  307. if (TextUtility.IsBlank(cell)) continue;
  308. list.Add(cell);
  309. }
  310. query.Dispose();
  311. }
  312. return list.ToArray();
  313. }
  314. static string XType(int xtype)
  315. {
  316. switch (xtype)
  317. {
  318. case 34: return "image";
  319. case 35: return "text";
  320. case 36: return "uniqueidentifier";
  321. case 48: return "tinyint";
  322. case 52: return "smallint";
  323. case 56: return "int";
  324. case 58: return "smalldatetime";
  325. case 59: return "real";
  326. case 60: return "money";
  327. case 61: return "datetime";
  328. case 62: return "float";
  329. case 98: return "sql_variant";
  330. case 99: return "ntext";
  331. case 104: return "bit";
  332. case 106: return "decimal";
  333. case 108: return "numeric";
  334. case 122: return "smallmoney";
  335. case 127: return "bigint";
  336. case 165: return "varbinary";
  337. case 167: return "varchar";
  338. case 173: return "binary";
  339. case 175: return "char";
  340. case 189: return "timestamp";
  341. case 231: return "nvarchar";
  342. case 239: return "nchar";
  343. case 241: return "xml";
  344. }
  345. return null;
  346. }
  347. /// <summary>获取列信息。</summary>
  348. public ColumnInfo[] ColumnsInfo(string tableName)
  349. {
  350. if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
  351. var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
  352. using (var query = Query(sql))
  353. {
  354. var ab = new ArrayBuilder<ColumnInfo>();
  355. for (var i = 0; i < query.Rows; i++)
  356. {
  357. var info = new ColumnInfo();
  358. info.Name = query.Text(i, "name");
  359. info.Type = XType(query.Int32(i, "xtype"));
  360. info.Length = query.Int32(i, "length");
  361. ab.Add(info);
  362. }
  363. return ab.Export();
  364. }
  365. }
  366. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  367. public string Initialize<T>() where T : class, new() => Initialize(typeof(T));
  368. /// <summary>创建表,当表不存在时创建表,当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串,发生异常时返回异常信息。</summary>
  369. public string Initialize(Type model)
  370. {
  371. var structure = TableStructure.Parse(model);
  372. if (structure == null) return "无法解析记录模型。";
  373. // 连接数据库。
  374. var connect = Connect();
  375. if (connect.NotEmpty()) return connect;
  376. // 检查现存表。
  377. var exists = false;
  378. var tables = TableNames();
  379. if (tables.Length > 0)
  380. {
  381. var lower = structure.Name.ToLower();
  382. foreach (var table in tables)
  383. {
  384. if (TextUtility.IsBlank(table)) continue;
  385. if (table.ToLower() == lower)
  386. {
  387. exists = true;
  388. break;
  389. }
  390. }
  391. }
  392. if (exists)
  393. {
  394. // 获取已存在的列名。
  395. var columns = ColumnNames(structure.Name);
  396. if (columns.Length > 0)
  397. {
  398. var lower = new List<string>();
  399. foreach (var column in columns)
  400. {
  401. if (TextUtility.IsBlank(column)) continue;
  402. lower.Add(column.ToLower());
  403. }
  404. columns = lower.ToArray();
  405. }
  406. // 增加列。
  407. foreach (var column in structure.Columns)
  408. {
  409. // 检查 Independent 特性。
  410. if (structure.Independent && column.Independent) continue;
  411. // 去重。
  412. var lower = column.Field.ToLower();
  413. if (columns.Contains(lower)) continue;
  414. var type = Declaration(column);
  415. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  416. var sql = TextUtility.Merge("alter table [", structure.Name, "] add ", type, "; ");
  417. var execute = Execute(sql);
  418. if (execute.Success == false) return execute.Message;
  419. }
  420. return TextUtility.Empty;
  421. }
  422. else
  423. {
  424. var sqlcolumns = new List<string>();
  425. foreach (var column in structure.Columns)
  426. {
  427. // 检查 Independent 特性。
  428. if (structure.Independent && column.Independent) continue;
  429. var type = Declaration(column);
  430. if (!column.Independent && column.Property.Name == "Key") type = type + " primary key";
  431. if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
  432. sqlcolumns.Add(type);
  433. }
  434. var sql = TextUtility.Merge("create table [", structure.Name, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
  435. var execute = Execute(sql);
  436. if (execute.Success) return TextUtility.Empty;
  437. return execute.Message;
  438. }
  439. }
  440. /// <summary>插入记录。返回错误信息。</summary>
  441. public string Insert(object record, string table = null)
  442. {
  443. if (record == null) return "参数无效。";
  444. FixProperties(record);
  445. var structure = TableStructure.Parse(record.GetType());
  446. if (structure == null) return "无法解析记录模型。";
  447. if (string.IsNullOrEmpty(table)) table = structure.Name;
  448. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  449. var ps = structure.CreateParameters(record, Parameter);
  450. var psc = ps.Length;
  451. if (psc < 1) return "数据模型不包含字段。";
  452. var names = new List<string>(psc);
  453. var values = new List<string>(psc);
  454. foreach (var column in ps)
  455. {
  456. //names.Add(TextGenerator.Merge("[", column, "]"));
  457. names.Add(TextUtility.Merge(column));
  458. values.Add("@" + column);
  459. }
  460. var sb = new StringBuilder();
  461. sb.Append("insert into [", table, "](", string.Join(", ", names.ToArray()), ") ");
  462. sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
  463. var sql = sb.ToString();
  464. var execute = Execute(sql, ps);
  465. if (execute.Success) return TextUtility.Empty;
  466. return execute.Message;
  467. }
  468. /// <summary>更新记录,实体中的 Key 属性不被更新。返回错误信息。</summary>
  469. /// <remarks>无法更新带有 Independent 特性的模型(缺少 Key 属性)。</remarks>
  470. public string Update(IRecord record, string table = null)
  471. {
  472. if (record == null) return "参数无效。";
  473. FixProperties(record);
  474. SetUpdated(record);
  475. var structure = TableStructure.Parse(record.GetType());
  476. if (structure == null) return "无法解析记录模型。";
  477. if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
  478. if (string.IsNullOrEmpty(table)) table = structure.Name;
  479. if (string.IsNullOrEmpty(table)) return "表名称无效。";
  480. var ps = structure.CreateParameters(record, Parameter, "_key");
  481. var psc = ps.Length;
  482. if (psc < 1) return "数据模型不包含字段。";
  483. var items = new List<string>();
  484. foreach (var p in ps)
  485. {
  486. var pn = p.ParameterName;
  487. items.Add(TextUtility.Merge("[", pn, "] = @", pn));
  488. }
  489. var key = record.Key.SafeKey();
  490. var sql = TextUtility.Merge("update [", table, "] set ", string.Join(", ", items.ToArray()), " where [_key]='", key, "'; ");
  491. var execute = Execute(sql, ps);
  492. if (execute.Success) return TextUtility.Empty;
  493. return execute.Message;
  494. }
  495. /// <summary>获取按指定语句查询到的所有记录。</summary>
  496. public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters);
  497. /// <summary>获取按指定语句查询到的所有记录。</summary>
  498. public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new()
  499. {
  500. var query = Query(sql, parameters);
  501. if (!query.Success) return new Result<T[]>(query.Message);
  502. var records = query.Fill<T>();
  503. query.Dispose();
  504. var result = new Result<T[]>(records);
  505. return result;
  506. }
  507. /// <summary>获取记录。</summary>
  508. public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) =>
  509. {
  510. if (flag == 0) return $"select * from [{tn}]; ";
  511. return $"select * from [{tn}] where _flag={flag}; ";
  512. });
  513. /// <summary>获取记录。</summary>
  514. public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) =>
  515. {
  516. if (flag == 0) return $"select * from [{tn}]; ";
  517. return $"select * from [{tn}] where _flag={flag}; ";
  518. });
  519. /// <summary>获取具有指定 Key 的记录。</summary>
  520. public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) =>
  521. {
  522. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  523. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  524. });
  525. /// <summary>获取具有指定 Key 的记录。</summary>
  526. public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) =>
  527. {
  528. if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; ";
  529. return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; ";
  530. });
  531. /// <summary>查询有效的 Key 值。</summary>
  532. public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) =>
  533. {
  534. if (flag == 0) return $"select _key from [{tn}]; ";
  535. return $"select _key from [{tn}] where _flag={flag}; ";
  536. });
  537. /// <summary>查询有效的 Key 值。</summary>
  538. public Result<string[]> Keys<T>(long flag = 0) where T : class, IRecord, new() => Keys(typeof(T), flag);
  539. #endregion
  540. #region public static
  541. #if NET20 || NET40
  542. /// <summary>枚举本地网络中服务器的名称。</summary>
  543. public static SqlServerSource[] EnumerateServer()
  544. {
  545. var list = new List<SqlServerSource>();
  546. // 表中列名:ServerName、InstanceName、IsClustered、Version。
  547. using (var query = new Query(SqlDataSourceEnumerator.Instance.GetDataSources()))
  548. {
  549. for (int i = 0; i < query.Rows; i++)
  550. {
  551. var item = new SqlServerSource();
  552. item.ServerName = query.Text(i, "ServerName");
  553. list.Add(item);
  554. }
  555. }
  556. return list.ToArray();
  557. }
  558. #endif
  559. /// <summary>指定的连接凭据是否符合连接要求,默认指定 master 数据库。</summary>
  560. public static bool Proven(string address, string user, string pass) => Proven(address, "master", user, pass);
  561. /// <summary>指定的连接凭据是否符合连接要求。</summary>
  562. public static bool Proven(string address, string store, string user, string pass)
  563. {
  564. var a = string.IsNullOrEmpty(address);
  565. var s = string.IsNullOrEmpty(store);
  566. var u = string.IsNullOrEmpty(user);
  567. var p = string.IsNullOrEmpty(pass);
  568. if (a) return false;
  569. if (s) return false;
  570. if (u && !p) return false;
  571. return true;
  572. }
  573. /// <summary>创建参数。</summary>
  574. /// <exception cref="ArgumentNullException"></exception>
  575. /// <exception cref="InvalidOperationException"></exception>
  576. static SqlParameter Parameter(Parameter parameter)
  577. {
  578. if (parameter == null) throw new InvalidOperationException("参数无效。");
  579. return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
  580. }
  581. /// <summary>创建参数。</summary>
  582. public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
  583. {
  584. var vname = TextUtility.Trim(name);
  585. if (TextUtility.IsBlank(vname)) return null;
  586. var vtype = SqlDbType.BigInt;
  587. switch (type)
  588. {
  589. case ColumnType.Bytes:
  590. vtype = SqlDbType.Image;
  591. break;
  592. case ColumnType.Integer:
  593. vtype = SqlDbType.BigInt;
  594. break;
  595. case ColumnType.Float:
  596. vtype = SqlDbType.Float;
  597. break;
  598. case ColumnType.DateTime:
  599. vtype = SqlDbType.DateTime;
  600. break;
  601. case ColumnType.VarChar:
  602. case ColumnType.VarChar191:
  603. case ColumnType.VarCharMax:
  604. vtype = SqlDbType.VarChar;
  605. break;
  606. case ColumnType.NVarChar:
  607. case ColumnType.NVarChar191:
  608. case ColumnType.NVarCharMax:
  609. vtype = SqlDbType.NVarChar;
  610. break;
  611. case ColumnType.Text:
  612. vtype = SqlDbType.Text;
  613. break;
  614. case ColumnType.NText:
  615. vtype = SqlDbType.NText;
  616. break;
  617. default:
  618. throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
  619. }
  620. var vsize = size;
  621. switch (type)
  622. {
  623. case ColumnType.VarChar:
  624. vsize = NumberUtility.Restrict(vsize, 0, 8000);
  625. break;
  626. case ColumnType.NVarChar:
  627. vsize = NumberUtility.Restrict(vsize, 0, 4000);
  628. break;
  629. case ColumnType.VarChar191:
  630. case ColumnType.NVarChar191:
  631. vsize = NumberUtility.Restrict(vsize, 0, 191);
  632. break;
  633. default:
  634. vsize = 0;
  635. break;
  636. }
  637. var vvalue = value;
  638. if (vvalue is string && vvalue != null && vsize > 0)
  639. {
  640. vvalue = TextUtility.Left((string)vvalue, vsize);
  641. }
  642. var parameter = new SqlParameter();
  643. parameter.ParameterName = vname;
  644. parameter.SqlDbType = vtype;
  645. parameter.Value = vvalue;
  646. if (vsize > 0) parameter.Size = vsize;
  647. return parameter;
  648. }
  649. /// <summary>创建参数。</summary>
  650. public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
  651. {
  652. if (value is string && value != null && size > 0)
  653. {
  654. value = TextUtility.Left((string)value, (int)size);
  655. }
  656. var p = new SqlParameter();
  657. p.ParameterName = name ?? "";
  658. p.SqlDbType = type;
  659. p.Size = size;
  660. p.Value = value;
  661. return p;
  662. }
  663. /// <summary>创建参数。</summary>
  664. public static SqlParameter Parameter(string name, SqlDbType type, object value)
  665. {
  666. var p = new SqlParameter();
  667. p.ParameterName = name ?? "";
  668. p.SqlDbType = type;
  669. p.Value = value;
  670. return p;
  671. }
  672. static string Declaration(ColumnAttribute column)
  673. {
  674. var type = TextUtility.Empty;
  675. var vcolumn = column;
  676. var length = Math.Max(0, vcolumn.Length);
  677. switch (vcolumn.Type)
  678. {
  679. case ColumnType.Integer:
  680. type = "bigint";
  681. break;
  682. case ColumnType.Float:
  683. type = "float";
  684. break;
  685. case ColumnType.Bytes:
  686. type = "image";
  687. break;
  688. case ColumnType.DateTime:
  689. type = "datetime";
  690. break;
  691. case ColumnType.VarChar:
  692. type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
  693. break;
  694. case ColumnType.VarChar191:
  695. type = TextUtility.Merge("varchar(191)");
  696. break;
  697. case ColumnType.VarCharMax:
  698. type = TextUtility.Merge("varchar(max)");
  699. break;
  700. case ColumnType.Text:
  701. type = TextUtility.Merge("text");
  702. break;
  703. case ColumnType.NVarChar:
  704. type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
  705. break;
  706. case ColumnType.NVarChar191:
  707. type = TextUtility.Merge("nvarchar(191)");
  708. break;
  709. case ColumnType.NVarCharMax:
  710. type = TextUtility.Merge("nvarchar(max)");
  711. break;
  712. case ColumnType.NText:
  713. type = TextUtility.Merge("ntext");
  714. break;
  715. default:
  716. return TextUtility.Empty;
  717. }
  718. return TextUtility.Merge("[", vcolumn.Field, "] ", type);
  719. }
  720. #endregion
  721. }
  722. }