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.

113 lines
4.1 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace SqlSugar
  6. {
  7. public class SqlServerProvider : AdoProvider
  8. {
  9. public SqlServerProvider() { }
  10. public override IDbConnection Connection
  11. {
  12. get
  13. {
  14. if (base._DbConnection == null)
  15. {
  16. try
  17. {
  18. base._DbConnection = new SqlConnection(base.Context.CurrentConnectionConfig.ConnectionString);
  19. }
  20. catch (Exception ex)
  21. {
  22. Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
  23. }
  24. }
  25. return base._DbConnection;
  26. }
  27. set
  28. {
  29. base._DbConnection = value;
  30. }
  31. }
  32. /// <summary>
  33. /// Only SqlServer
  34. /// </summary>
  35. /// <param name="transactionName"></param>
  36. public override void BeginTran(string transactionName)
  37. {
  38. CheckConnection();
  39. base.Transaction = ((SqlConnection)this.Connection).BeginTransaction(transactionName);
  40. }
  41. /// <summary>
  42. /// Only SqlServer
  43. /// </summary>
  44. /// <param name="iso"></param>
  45. /// <param name="transactionName"></param>
  46. public override void BeginTran(IsolationLevel iso, string transactionName)
  47. {
  48. CheckConnection();
  49. base.Transaction = ((SqlConnection)this.Connection).BeginTransaction(iso, transactionName);
  50. }
  51. public override IDataAdapter GetAdapter()
  52. {
  53. return new SqlDataAdapter();
  54. }
  55. public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
  56. {
  57. var sqlCommand = new SqlCommand(sql, (SqlConnection)this.Connection)
  58. {
  59. CommandType = this.CommandType,
  60. CommandTimeout = this.CommandTimeOut
  61. };
  62. if (this.Transaction != null)
  63. {
  64. sqlCommand.Transaction = (SqlTransaction)this.Transaction;
  65. }
  66. if (parameters.HasValue())
  67. {
  68. var ipars = ToIDbDataParameter(parameters);
  69. sqlCommand.Parameters.AddRange((SqlParameter[])ipars);
  70. }
  71. CheckConnection();
  72. return sqlCommand;
  73. }
  74. public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
  75. {
  76. ((SqlDataAdapter)dataAdapter).SelectCommand = (SqlCommand)command;
  77. }
  78. /// <summary>
  79. /// if mysql return MySqlParameter[] pars
  80. /// if sqlerver return SqlParameter[] pars ...
  81. /// </summary>
  82. /// <param name="parameters"></param>
  83. /// <returns></returns>
  84. public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
  85. {
  86. if (parameters == null || parameters.Length == 0) return null;
  87. var result = new SqlParameter[parameters.Length];
  88. var index = 0;
  89. foreach (var parameter in parameters)
  90. {
  91. if (parameter.Value == null) parameter.Value = DBNull.Value;
  92. var sqlParameter = new SqlParameter
  93. {
  94. ParameterName = parameter.ParameterName,
  95. UdtTypeName = parameter.UdtTypeName,
  96. Size = parameter.Size,
  97. Value = parameter.Value,
  98. DbType = parameter.DbType,
  99. Direction = parameter.Direction
  100. };
  101. result[index] = sqlParameter;
  102. if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
  103. {
  104. if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
  105. this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
  106. this.OutputParameters.Add(sqlParameter);
  107. }
  108. ++index;
  109. }
  110. return result;
  111. }
  112. }
  113. }