SqlWork.cs
9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebRepository
{
public partial class SqlWork : ISqlWork
{
private BaseDBContext _context;
private string CnString;
public SqlWork(BaseDBContext context)
{
_context = context;
}
public BaseDBContext GetDbContext()
{
return _context;
}
/// <summary>
/// 数据操作
/// </summary>
public int DataOperator(string Cmstring)
{
CnString = _context.Database.GetDbConnection().ConnectionString;
int Line = 0;
SqlCommand sqlCm = null;
SqlConnection sqlCn = null;
try
{
using (sqlCn = new SqlConnection(CnString))
{
sqlCn.Open();
sqlCm = new SqlCommand(Cmstring, sqlCn);
Line = sqlCm.ExecuteNonQuery();
sqlCn.Close();
}
}
catch (Exception ex)
{
//SEmessage = ex.Message.Replace(Environment.NewLine, "");
Line = -1;
throw new Exception(ex.Message.Replace(Environment.NewLine, ""));
}
finally
{
sqlCn.Dispose();
if (sqlCm != null)
sqlCm.Dispose();
}
return Line;
}
/// <summary>
/// 查询信息
/// </summary>
public DataSet SelectGet(string CmString)
{
CnString = _context.Database.GetDbConnection().ConnectionString;
DataSet ds = new System.Data.DataSet();
SqlConnection sqlCn = null;
SqlDataAdapter sqlDa = null;
try
{
using (sqlCn = new SqlConnection(CnString))
{
sqlCn.Open();
sqlDa = new SqlDataAdapter(CmString, sqlCn);
sqlDa.Fill(ds);
sqlCn.Close();
}
}
catch (Exception ex)
{
ds = null;
throw new Exception(ex.Message.Replace(Environment.NewLine, ""));
//App.ExFile.MessageLog(App.ExFile.Path_Emessage, "", ex.ToString());
}
finally
{
sqlCn.Dispose();
if (sqlDa != null)
sqlDa.Dispose();
}
return ds;
}
/// <summary>
/// 执行存储过程,返回影响的行数
/// </summary>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns></returns>
public int RunProcedure(string storedProcName, IDataParameter[] parameters)
{
CnString = _context.Database.GetDbConnection().ConnectionString;
using (SqlConnection connection = new SqlConnection(CnString))
{
int result;
connection.Open();
SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
int rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}
public int ProcOutput(SqlCommand cmd, string procName, SqlParameter[] paras)
{
int result;
cmd.CommandType = CommandType.StoredProcedure;//设置此类型是存储过程类型
cmd.CommandText = procName;//设置执行的存储过程//添加para参数
if (paras != null)
{
foreach (SqlParameter parameter in paras)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
result = (int)cmd.Parameters["ReturnValue"].Value;
return result;
}
public int ProcOutput(string procName, SqlParameter[] paras)
{
CnString = _context.Database.GetDbConnection().ConnectionString;
SqlConnection conn = new SqlConnection(CnString);//创建数据库连接对象
conn.Open();//打开数据库连接
//SqlCommand cmd = conn.CreateCommand();//创建并执行与此连接关联的T-Sql语句命令的对象,可以不用这段用下面两段
SqlCommand cmd = new SqlCommand
{
Connection = conn,
CommandType = CommandType.StoredProcedure,//设置此类型是存储过程类型
CommandText = procName//设置执行的存储过程//添加para参数
};
int result;
if (paras != null)
{
foreach (SqlParameter parameter in paras)
cmd.Parameters.Add(parameter);
}
cmd.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
result = (int)cmd.Parameters["ReturnValue"].Value;
conn.Close();//关闭连接
return result;
}
/// <summary>
/// 创建 SqlCommand 对象实例(用来返回一个整数值)
/// </summary>
/// <param name="connection">SQl连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand 对象实例</returns>
private SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
/// <summary>
/// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection)
{
CommandType = CommandType.StoredProcedure
};
foreach (SqlParameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以DBNull.Value.
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
return command;
}
/// <summary>
/// 调用存储过程Output参数方法
/// </summary>
/// <param name="procName">执行的存储过程语句</param>
/// <param name="paras">数据库的参数</param>
/// <param name="OutName">定义Output参数</param>
/// <returns>返回值</returns>
public object ProcOutput(string procName, SqlParameter[] paras, string OutName)
{
CnString = _context.Database.GetDbConnection().ConnectionString;
SqlConnection conn = new SqlConnection(CnString);//创建数据库连接对象
conn.Open();//打开数据库连接
//SqlCommand cmd = conn.CreateCommand();//创建并执行与此连接关联的T-Sql语句命令的对象,可以不用这段用下面两段
SqlCommand cmd = new SqlCommand
{
Connection = conn,
CommandType = CommandType.StoredProcedure,//设置此类型是存储过程类型
CommandText = procName//设置执行的存储过程//添加para参数
};
if (paras != null)
{
foreach (SqlParameter parameter in paras)
cmd.Parameters.Add(parameter);
}
int n = cmd.ExecuteNonQuery();//执行语句并返回受影响的行数
object o = cmd.Parameters[OutName].Value;//获取@CardID的值
conn.Close();//关闭连接
return o;
}
}
}