public static DataTable RunCommand()

in Source/Actions/Microsoft.Deployment.Common/Helpers/SqlUtility.cs [222:309]


        public static DataTable RunCommand(string connectionString, string rawScript, SqlCommandType commandType, SqlParameter[] parameters = null, bool enableTransaction = true)
        {
            DataTable table = null;

            if (string.IsNullOrWhiteSpace(rawScript))
                return null;

            for (var retries = 0; retries < MAX_RETRIES; retries++)
            {
                SqlTransaction transaction = null;
                var cn = new SqlConnection(connectionString);

                try
                {
                    cn.Open();
                    if (enableTransaction)
                    {
                        transaction = cn.BeginTransaction(IsolationLevel.ReadCommitted);
                    }
                    using (var command = cn.CreateCommand())
                    {
                        command.Transaction = transaction;
                        command.CommandText = rawScript;
                        command.CommandType = CommandType.Text;
                        command.CommandTimeout = 0;

                        if (parameters != null)
                            command.Parameters.AddRange(parameters);

                        switch (commandType)
                        {
                            case SqlCommandType.ExecuteWithData:
                                {
                                    table = new DataTable();
                                    var adapter = new SqlDataAdapter(command);
                                    adapter.Fill(table);
                                    break;
                                }
                            case SqlCommandType.ExecuteStoredProc:
                                {
                                    command.CommandType = CommandType.StoredProcedure;
                                    table = new DataTable();
                                    var adapter = new SqlDataAdapter(command);
                                    adapter.Fill(table);
                                    break;
                                }
                            case SqlCommandType.ExecuteWithoutData:
                                {
                                    command.ExecuteNonQuery();
                                    break;
                                }
                        }
                    }
                    if (enableTransaction)
                    {
                        transaction.Commit();
                    }
                    break;
                }
                catch (Exception)
                {
                    if (cn.State != ConnectionState.Open)
                    {
                        // The transaction must have been rolledback with the client being disconnected
                        try
                        {
                            if (enableTransaction)
                            {
                                transaction?.Rollback();
                            }
                        }
                        catch (Exception)
                        {
                        }

                        continue;
                    }

                    throw;
                }
                finally
                {
                    cn.Close();
                }
            }

            return table;
        }