Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以从C#加速此存储过程的处理?_C#_Sql Server_Stored Procedures_Database Performance - Fatal编程技术网

是否可以从C#加速此存储过程的处理?

是否可以从C#加速此存储过程的处理?,c#,sql-server,stored-procedures,database-performance,C#,Sql Server,Stored Procedures,Database Performance,我从C#调用SQL Server存储过程。只要我将SqlCommandCommandTimeout设置得足够高,它就可以工作——返回数据——但比绝对零度时的molasses慢 在如何调用存储过程方面,我能做些什么来加快进程?我是这样做的: DataTable dtFillRateResults = SqlDBHelper.ExecuteSQLReturnDataTable(FILL_RATE_BY_DISTRIBUTOR_BY_CUSTOMER_STORED_PROC, CommandType.

我从C#调用SQL Server存储过程。只要我将
SqlCommand
CommandTimeout
设置得足够高,它就可以工作——返回数据——但比绝对零度时的molasses慢

在如何调用存储过程方面,我能做些什么来加快进程?我是这样做的:

DataTable dtFillRateResults = SqlDBHelper.ExecuteSQLReturnDataTable(FILL_RATE_BY_DISTRIBUTOR_BY_CUSTOMER_STORED_PROC, CommandType.StoredProcedure,
            new SqlParameter()
            {
                ParameterName = "@Unit",
                SqlDbType = SqlDbType.VarChar,
                Value = _unit
            },
            new SqlParameter()
            {
                ParameterName = "@Member",
                SqlDbType = SqlDbType.VarChar,
                Value = _memberId
            },
            new SqlParameter()
            {
                ParameterName = "@BegDate",
                SqlDbType = SqlDbType.DateTime,
                Value = Convert.ToDateTime(_dateBegin)
            },
            new SqlParameter()
            {
                ParameterName = "@EndDate",
                SqlDbType = SqlDbType.DateTime,
                Value = Convert.ToDateTime(_dateEnd)
            }
);
上述调用
SqlDBHelper.ExecuteSQLReturnDataTable()
,即:

public static DataTable ExecuteSQLReturnDataTable(string sql, CommandType cmdType, params SqlParameter[] parameters)
{
    using (DataSet ds = new DataSet())
    using (SqlConnection connStr = new SqlConnection(FillRateRptConstsAndUtils.CPSConnStr))
    using (SqlCommand cmd = new SqlCommand(sql, connStr))
    {
        cmd.CommandType = cmdType;
        cmd.CommandTimeout = EXTENDED_TIMEOUT;

        foreach (var item in parameters)
        {
            cmd.Parameters.Add(item);
        }

        try
        {
            cmd.Connection.Open();
            new SqlDataAdapter(cmd).Fill(ds);
        }
        catch (SqlException sqlex)
        {
            for (int i = 0; i < sqlex.Errors.Count; i++)
            {
                var sqlexDetail = String.Format("From ExecuteSQLReturnDataTable(), SQL Exception #{0}{1}Source: {2}{1}Number: {3}{1}State: {4}{1}Class: {5}{1}Server: {6}{1}Message: {7}{1}Procedure: {8}{1}LineNumber: {9}",
                    i + 1, // Some users would get the fantods if they saw #0
                    Environment.NewLine,
                    sqlex.Errors[i].Source,
                    sqlex.Errors[i].Number,
                    sqlex.Errors[i].State,
                    sqlex.Errors[i].Class,
                    sqlex.Errors[i].Server,
                    sqlex.Errors[i].Message,
                    sqlex.Errors[i].Procedure,
                    sqlex.Errors[i].LineNumber);
                MessageBox.Show(sqlexDetail);
            }
        }
        catch (Exception ex)
        {
            String exDetail = String.Format(FillRateRptConstsAndUtils.ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
            MessageBox.Show(exDetail);
        }

        return ds.Tables[0];
    }
}
公共静态数据表ExecuteSQLReturnDataTable(字符串sql,CommandType cmdType,参数SqlParameter[]参数)
{
使用(数据集ds=新数据集())
使用(SqlConnection connStr=newsqlconnection(fillrateptconstsandutils.cpsconstr))
使用(SqlCommand cmd=newsqlcommand(sql,connStr))
{
cmd.CommandType=cmdType;
cmd.CommandTimeout=扩展超时;
foreach(参数中的变量项)
{
cmd.Parameters.Add(项目);
}
尝试
{
cmd.Connection.Open();
新的SqlDataAdapter(cmd).Fill(ds);
}
捕获(SqlException sqlex)
{
对于(int i=0;i
您确定问题不在存储过程本身吗?您可能需要显示SP的代码,我看不到C代码中任何会导致问题的突出部分,这一行
new-SqlDataAdapter(cmd.Fill(ds)
看起来很奇怪,不是因为它错了,也不是问题,而是因为它很突出。如果它很慢,那就是数据库的错误。您需要优化存储过程(如果可能的话)。如果上游速度慢,您就无法通过在下游工作来加快存储过程。您需要优化SP.@B.ClayShannon,我会直接进入数据库(我知道如何在oracle和SQL中实现这一点)并在那里运行该过程。不在连接板中。您可以使用相同的参数从编辑器中调用它,它将告诉您原始过程运行所需的时间。这将指向您需要优化的地方。如果您没有优化存储过程的访问权限,请告诉DBA。如果您拥有SQL Management Studio和运行存储过程的访问权限,请执行该操作并查看执行所需的时间。在我看来,问题在于SQL—您需要对其进行优化。您确定问题不在于存储过程本身吗?您可能需要显示SP的代码,我看不到C代码中任何会导致问题的突出部分,这一行
new-SqlDataAdapter(cmd.Fill(ds)
看起来很奇怪,不是因为它错了,也不是问题,而是因为它很突出。如果它很慢,那就是数据库的错误。您需要优化存储过程(如果可能的话)。如果上游速度慢,您就无法通过在下游工作来加快存储过程。您需要优化SP.@B.ClayShannon,我会直接进入数据库(我知道如何在oracle和SQL中实现这一点)并在那里运行该过程。不在连接板中。您可以使用相同的参数从编辑器中调用它,它将告诉您原始过程运行所需的时间。这将指向您需要优化的地方。如果您没有优化存储过程的访问权限,请告诉DBA。如果您拥有SQL Management Studio和运行存储过程的访问权限,请执行该操作并查看执行所需的时间。在我看来,问题在于SQL—您需要对其进行优化。