Ejemplo n.º 1
0
        public override void Execute()
        {
            string connectionString = m_configurationProvider.GetConfiguration(ConfigName.SqlConnectionString);

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                using (var sqlCommand = connection.CreateCommand())
                {
                    SqlCommandState sqlCommandState = new SqlCommandState(sqlCommand);

                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = GetStoredProcedureName();

                    var parameters = GetParameters();
                    sqlCommand.Parameters.AddRange(parameters);

                    var asyncResult = sqlCommand.BeginExecuteNonQuery();

                    if (!asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(m_sqlTimeoutInSeconds)))
                    {
                        throw new TimeoutException(String.Format("Timeout occured while waiting for {0} stored procedure", GetStoredProcedureName()));
                    }
                    int rowsAffected = sqlCommand.EndExecuteNonQuery(asyncResult);

                    CheckNonQueryReturnValue(rowsAffected);
                }
            }
        }
        public override void Execute()
        {
            string connectionString = m_configurationProvider.GetConfiguration(ConfigName.SqlConnectionString);

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (var sqlCommand = connection.CreateCommand())
                {
                    SqlCommandState sqlCommandState = new SqlCommandState(sqlCommand);

                    SqlParameter[] parameters = new SqlParameter[1];

                    sqlCommand.CommandType = CommandType.StoredProcedure;
                    sqlCommand.CommandText = GetStoredProcedureName();

                    parameters = GetParameters();
                    sqlCommand.Parameters.AddRange(parameters);

                    // Either this, parsing returned data in the callback...
                    //var asyncResult = sqlCommand.BeginExecuteReader(new AsyncCallback(HandleCallback), sqlCommandState);

                    // ...or this, parsing it here. (Which I think was the original intent.)
                    var asyncResult = sqlCommand.BeginExecuteReader(CommandBehavior.Default);

                    // Wait for async to finish. True if successful wait, false if timedout.
                    if (!asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(m_sqlTimeoutInSeconds)))
                    {
                        // TODO: Log error message and raise event somehow.
                        throw new TimeoutException(String.Format("Timeout occured while waiting for {0} stored procedure", GetStoredProcedureName()));
                    }
                    sqlCommandState.DataReader = sqlCommand.EndExecuteReader(asyncResult);
                    ParseData(sqlCommandState.DataReader);
                }
            }
        }
        private void HandleCallback(IAsyncResult result)
        {
            SqlCommandState state = result.AsyncState as SqlCommandState;

            try
            {
                state.DataReader = state.SqlCommand.EndExecuteReader(result);
                ParseData(state.DataReader);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Async call returned exception. See Inner Exception for details", ex);
            }
        }