2023年6月21日发(作者:)
使⽤C#操作存储过程,执⾏sql语句通⽤类如何使⽤C# 操作存储过程,执⾏sql语句?闲话不多说,直接上代码: /// /// Sql通⽤类 /// public class SqlHelper { ⾸先配置连接字符串 public static string connStr = tionStrings["ConnString"].ConnectionString;//ConnString表⽰webconfig中的连接字符串
执⾏存储过程不设置超时时间 /// /// 调⽤存储过程 /// /// 连接字符串 /// 存储过程名称 /// 存储过程执⾏状态 /// 执⾏存储过程状态描述 /// 存储过程输⼊参数 /// public static DataSet Sql_GetStoredProcedureFunction(string connStr, string storedProcedureName, out bool ResponseBool, outstring ResponseMsg, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(storedProcedureName, sqlConn)) { dType = Procedure; if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,存储过程:【{storedProcedureName}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
当存储过程执⾏时间太长时,存储过程的默认超时时间是30s,需要设置存储过程执⾏超时时间 /// /// 调⽤存储过程 (⾃定义超时时间) /// /// 连接字符串 /// 存储过程名称 /// 执⾏存储过程请求超时时间(单位:s) /// 存储过程执⾏状态 /// 执⾏存储过程状态描述 /// 存储过程输⼊参数 /// public static DataSet Sql_GetStoredProcedureFunction(string connStr, string storedProcedureName, int commandOutTime, out boolResponseBool, out string ResponseMsg, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(storedProcedureName, sqlConn)) { dType = Procedure; dTimeout = commandOutTime; if (() > 0) { SqlParameter[] sqlParameters = new SqlParameter[()]; for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,存储过程:【{storedProcedureName}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
执⾏sql语句,进⾏增删改操作 /// /// 增删改数据 /// /// 数据库连接字符串 /// 执⾏的sql语句 /// 输⼊参数 /// public static int SQLExecuteData(string sqlConnStr, string sql, params ParameterKeyValuesEntity[] paramsObject) { int count = 0; using (SqlConnection conn = new SqlConnection(sqlConnStr)) { (); SqlCommand cmd = new SqlCommand(sql, conn); //定义⼀个sql操作命令对象 if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } count = eNonQuery(); //执⾏语句 (); //关闭连接 cmd = null; e(); //释放对象 } return count; }
当数据库中表关系及其复杂,并且数据量特别多的时候(⼀般情况下⽤缓存解决问题),执⾏sql查询语句相当耗时,需要设置sql语句请求超时时间。 执⾏sql查询语句,设置sql查询语句超时时间 /// /// 执⾏SQL脚本 /// /// 连接字符串 /// SQL脚本 /// 执⾏状态 /// 状态描述 /// 执⾏sql语句请求超时时间(单位:s) /// 输⼊参数 /// public static DataSet Sql_GetStored(string connStr, string sqlScript, out bool ResponseBool, out string ResponseMsg, intcommandOutTime = 500, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(sqlScript, sqlConn)) { dType = ; dTimeout = commandOutTime; if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,SQL脚本:【{sqlScript}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
⼊参实体建类 /// /// 输⼊参数实体 参数名称(Key)/参数值(Value) /// public class ParameterKeyValuesEntity { /// /// 参数名称 /// public string Key { get; set; } /// /// 参数值 /// public object Value { get; set; } }
执⾏存储过程⽰例: public Result ⽅法名(string ⼊参1,string ⼊参2, string ⼊参3) { try {
//定义输出参数 Result result = new Result(); //存储过程名称 string procName = "存储过程名称"; #region -- 执⾏存储过程获取数据 //返回值状态 bool responseBool = true; //返回值状态描述 string responseMsg = ; //存储过程输⼊参数实体 ParameterKeyValuesEntity[] parameterKeyValue = new ParameterKeyValuesEntity[] { new ParameterKeyValuesEntity(){Key="@存储过程⼊参1",Value=赋值1}, new ParameterKeyValuesEntity(){Key="@存储过程⼊参2",Value=赋值2}, new ParameterKeyValuesEntity(){Key="@存储过程⼊参3",Value=赋值3},
}; //使⽤sql通⽤类的⽅法执⾏存储过程 DataSet ds = _GetStoredProcedureFunction(connStr, procName, out responseBool, out responseMsg,parameterKeyValue); if (!responseBool) { = "204"; = $"查询存储过程时出现异常,异常信息:{responseMsg}"; og($"业务异常:存储过程名:{procName}---异常信息:{responseMsg}");//项⽬中的异常⽇志 return result; } DataTable dt = [0];
if (dt != null && != null && > 0) { 获取存储过程执⾏后的数据,给实体类赋值 } #endregion = loopbackdata; string json = izeObject(); result = ResultSuccess(json, typeof(JObject)); return result; } catch (Exception e) { og($"业务异常:{e}"); return ResultError($"异常信息:{e}"); } }
.
2023年6月21日发(作者:)
使⽤C#操作存储过程,执⾏sql语句通⽤类如何使⽤C# 操作存储过程,执⾏sql语句?闲话不多说,直接上代码: /// /// Sql通⽤类 /// public class SqlHelper { ⾸先配置连接字符串 public static string connStr = tionStrings["ConnString"].ConnectionString;//ConnString表⽰webconfig中的连接字符串
执⾏存储过程不设置超时时间 /// /// 调⽤存储过程 /// /// 连接字符串 /// 存储过程名称 /// 存储过程执⾏状态 /// 执⾏存储过程状态描述 /// 存储过程输⼊参数 /// public static DataSet Sql_GetStoredProcedureFunction(string connStr, string storedProcedureName, out bool ResponseBool, outstring ResponseMsg, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(storedProcedureName, sqlConn)) { dType = Procedure; if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,存储过程:【{storedProcedureName}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
当存储过程执⾏时间太长时,存储过程的默认超时时间是30s,需要设置存储过程执⾏超时时间 /// /// 调⽤存储过程 (⾃定义超时时间) /// /// 连接字符串 /// 存储过程名称 /// 执⾏存储过程请求超时时间(单位:s) /// 存储过程执⾏状态 /// 执⾏存储过程状态描述 /// 存储过程输⼊参数 /// public static DataSet Sql_GetStoredProcedureFunction(string connStr, string storedProcedureName, int commandOutTime, out boolResponseBool, out string ResponseMsg, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(storedProcedureName, sqlConn)) { dType = Procedure; dTimeout = commandOutTime; if (() > 0) { SqlParameter[] sqlParameters = new SqlParameter[()]; for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,存储过程:【{storedProcedureName}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
执⾏sql语句,进⾏增删改操作 /// /// 增删改数据 /// /// 数据库连接字符串 /// 执⾏的sql语句 /// 输⼊参数 /// public static int SQLExecuteData(string sqlConnStr, string sql, params ParameterKeyValuesEntity[] paramsObject) { int count = 0; using (SqlConnection conn = new SqlConnection(sqlConnStr)) { (); SqlCommand cmd = new SqlCommand(sql, conn); //定义⼀个sql操作命令对象 if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } count = eNonQuery(); //执⾏语句 (); //关闭连接 cmd = null; e(); //释放对象 } return count; }
当数据库中表关系及其复杂,并且数据量特别多的时候(⼀般情况下⽤缓存解决问题),执⾏sql查询语句相当耗时,需要设置sql语句请求超时时间。 执⾏sql查询语句,设置sql查询语句超时时间 /// /// 执⾏SQL脚本 /// /// 连接字符串 /// SQL脚本 /// 执⾏状态 /// 状态描述 /// 执⾏sql语句请求超时时间(单位:s) /// 输⼊参数 /// public static DataSet Sql_GetStored(string connStr, string sqlScript, out bool ResponseBool, out string ResponseMsg, intcommandOutTime = 500, params ParameterKeyValuesEntity[] paramsObject) { DataSet ResponseDs = new DataSet(); ResponseBool = true; ResponseMsg = "获取成功!"; try { using (SqlConnection sqlConn = new SqlConnection(connStr)) { (); using (SqlCommand sqlCmd = new SqlCommand(sqlScript, sqlConn)) { dType = ; dTimeout = commandOutTime; if (() > 0) { for (int i = 0; i < (); i++) { SqlParameter sqlParameter = new SqlParameter(paramsObject[i].Key, paramsObject[i].Value); (sqlParameter); } } SqlDataAdapter sda = new SqlDataAdapter(sqlCmd); (ResponseDs); } } } catch (Exception e) { ResponseBool = false; ResponseMsg = $"查询存储过程时出现异常,SQL脚本:【{sqlScript}】n 异常原因:【{e}】n 异常详细信息:【{race}】!"; } return ResponseDs; }
⼊参实体建类 /// /// 输⼊参数实体 参数名称(Key)/参数值(Value) /// public class ParameterKeyValuesEntity { /// /// 参数名称 /// public string Key { get; set; } /// /// 参数值 /// public object Value { get; set; } }
执⾏存储过程⽰例: public Result ⽅法名(string ⼊参1,string ⼊参2, string ⼊参3) { try {
//定义输出参数 Result result = new Result(); //存储过程名称 string procName = "存储过程名称"; #region -- 执⾏存储过程获取数据 //返回值状态 bool responseBool = true; //返回值状态描述 string responseMsg = ; //存储过程输⼊参数实体 ParameterKeyValuesEntity[] parameterKeyValue = new ParameterKeyValuesEntity[] { new ParameterKeyValuesEntity(){Key="@存储过程⼊参1",Value=赋值1}, new ParameterKeyValuesEntity(){Key="@存储过程⼊参2",Value=赋值2}, new ParameterKeyValuesEntity(){Key="@存储过程⼊参3",Value=赋值3},
}; //使⽤sql通⽤类的⽅法执⾏存储过程 DataSet ds = _GetStoredProcedureFunction(connStr, procName, out responseBool, out responseMsg,parameterKeyValue); if (!responseBool) { = "204"; = $"查询存储过程时出现异常,异常信息:{responseMsg}"; og($"业务异常:存储过程名:{procName}---异常信息:{responseMsg}");//项⽬中的异常⽇志 return result; } DataTable dt = [0];
if (dt != null && != null && > 0) { 获取存储过程执⾏后的数据,给实体类赋值 } #endregion = loopbackdata; string json = izeObject(); result = ResultSuccess(json, typeof(JObject)); return result; } catch (Exception e) { og($"业务异常:{e}"); return ResultError($"异常信息:{e}"); } }
.
发布评论