C# 根本不是正确的处理方法。使用后,您需要立即关闭并处理连接。阅读about并开始使用USING语句。此外,您的查询有一个参数,但您的命令没有。 protected virtual async Task<bool?> IsProcedureDepl

C# 根本不是正确的处理方法。使用后,您需要立即关闭并处理连接。阅读about并开始使用USING语句。此外,您的查询有一个参数,但您的命令没有。 protected virtual async Task<bool?> IsProcedureDepl,c#,sql,.net,sql-server,ado.net,C#,Sql,.net,Sql Server,Ado.net,根本不是正确的处理方法。使用后,您需要立即关闭并处理连接。阅读about并开始使用USING语句。此外,您的查询有一个参数,但您的命令没有。 protected virtual async Task<bool?> IsProcedureDeployed(string storedProcedureName) { try { SqlCommand sqlCommand = new SqlCo

根本不是正确的处理方法。使用后,您需要立即关闭并处理连接。阅读about并开始使用USING语句。此外,您的查询有一个参数,但您的命令没有。
protected virtual async Task<bool?> IsProcedureDeployed(string storedProcedureName)
        {
            try
            {
                SqlCommand sqlCommand = new SqlCommand
                {
                    CommandText = "select count(*) from sysobjects where type = 'P' and name = @storedProcedureName",
                    CommandType = CommandType.Text,
                    CommandTimeout = this.CommandTimeout
                };

                await this.EnsureConnectionOpened();
                int count = (int)(await sqlCommand.ExecuteScalarAsync());

                return count > 0;
            }
            catch (Exception exception)
            {
                this.SqlConnection.Close();
                ExceptionDispatchInfo.Capture(exception).Throw();
                return null;
            }
        }
protected async Task EnsureConnectionOpened()
        {
            if (SqlConnection.State == ConnectionState.Closed && SqlTransaction == null)
            {
                await SqlConnection.OpenAsync();
            }
        }
protected virtual async Task<bool?> IsProcedureDeployed(string storedProcedureName)
        {
            try
            {
                SqlCommand sqlCommand = new SqlCommand("select count(*) from sysobjects where type = 'P' and name = @storedProcedureName", this.SqlConnection)
                {
                    CommandType = CommandType.Text,
                    CommandTimeout = this.CommandTimeout
                };

                SqlParameter sqlParameter = new SqlParameter
                {
                    ParameterName = "@storedProcedureName",
                    Value = storedProcedureName
                };

                sqlCommand.Parameters.Add(sqlParameter);

                await this.EnsureConnectionOpened();
                int count = (int)(await sqlCommand.ExecuteScalarAsync());

                return count > 0;
            }
            catch (Exception exception)
            {
                this.SqlConnection.Close();
                ExceptionDispatchInfo.Capture(exception).Throw();
                return null;
            }
        }
protected virtual async Task<bool?> IsProcedureDeployed(string storedProcedureName)
{
    bool IsDeployed = false;
    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString))
    {
        conn.Open();
        using (var cmd = new SqlCommand("select count(*) from sysobjects where type = 'P' and name = @storedProcedureName", conn))
        {
            cmd.Parameters.Add("@storedProcedureName", SqlDbType.NVarChar, 128).Value = storedProcedureName; //using nvarchar(128) because object names use sysname which is a synonym for nvarchar(128)
            var result = await cmd.ExecuteScalarAsync();
            bool.TryParse(result.ToString(), out IsDeployed);
        }
    }
    return IsDeployed;
}