gpt4 book ai didi

c# - 可以调用存储过程进行调试吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:58 25 4
gpt4 key购买 nike

在我的 ASP.NET 网站上使用 Visual Studio 2010 时,我们有存储过程调用的代码:

SqlDataAccess sqlDataAccess = new SqlDataAccess();

SqlParameter[] parameters =
{
new SqlParameter("@R", rptType.Replace("'", "''")),
new SqlParameter("@M", hybrDct["mod"].ToString().Replace("'", "''")),
new SqlParameter("@C", hybrDct["CG"].ToString().Replace("'", "''")),
new SqlParameter("@Ts$", hybrDct["TFields"].ToString().Replace("'", "''")),
};

sqlDataAccess.ProcName = "MyStoredProc";
sqlDataAccess.Parameters = parameters;

是否可以出于调试目的打印出执行,而不是找出每个单独的 SqlParameter 并单独输入它?

谢谢。

最佳答案

我使用类似下面的东西 - 一切都经过这个并被记录下来 - 你明白了。

/// <summary>
/// Executes a stored procedure with no return.
/// </summary>
/// <returns>The number of records affected by stored proc.</returns>
public static int ExecuteStoredProc(string storedProcName, params SqlParameter[] parameters)
{
StringBuilder callDefinition = new StringBuilder();
callDefinition.Append(string.Format("ExecuteStoredProc: {0} (", storedProcName));
for (int i = 0; i < parameters.Count(); i++)
{
callDefinition.Append(string.Format("{0}={1}", parameters[i].ParameterName, parameters[i].Value));
if (i < parameters.Count - 1)
{
callDefinition.Append(",");
}
}
callDefinition.Append(")";
log.Debug(callDefinition.ToString());
using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionProfile.ConnectionName))
{
using (SqlCommand command = new SqlCommand(storedProcName, ctx.Connection))
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandTimeout = 1000;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
//log your param here
}

return command.ExecuteNonQuery();
}
}
}

/// <summary>
/// Executes a query and returns a dataset
/// </summary>
public static DataSet ExecuteQueryReturnDataSet(string query, params SqlParameter[] parameters)
{
try
{
//implement the parameter logging here as in the above code sample as well

log.Debug("Executing ExecuteQueryReturnDataSet() calling query " + query);
using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionProfile.ConnectionName))
{
using (SqlCommand command = new SqlCommand(query, ctx.Connection))
{
command.CommandType = System.Data.CommandType.Text;
command.CommandTimeout = 1000;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
return dataSet;
}
}
}
}
catch (Exception ex)
{
log.Error(ex);
throw;
}
}

关于c# - 可以调用存储过程进行调试吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com