Ejemplo n.º 1
0
        public void ExecuteReader(string query, CommandType commandType = CommandType.Text, List <SqlParameter> parameters = null)
        {
            SqlConnection connection = null;

            try
            {
                using (connection = new SqlConnection(GetConnectionString(ServerName, DatabaseName)))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        if (Timeout != null)
                        {
                            command.CommandTimeout = (int)Timeout;
                        }
                        connection.InfoMessage += OnInfoMessageGenerated;
                        connection.Open();
                        command.StatementCompleted += OnStatementCompleted;
                        command.CommandType         = commandType;
                        if (parameters != null)
                        {
                            command.Parameters.AddRange(parameters.ToArray());
                        }
                        SqlDataReader reader = command.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                SqlRow row = new SqlRow();
                                row.GetReaderRow(reader);
                                Rows.Add(row);
                            }
                        }
                    }
                }
            }
            catch (SqlException se)
            {
                ProcessSqlError(se);
                throw;
            }
            catch (Exception e)
            {
                MessageLogging.WriteLine(string.Format("Exception {0}", e.Message));
            }
        }
Ejemplo n.º 2
0
        public void ExecuteCatchPrint(string query)
        {
            SqlConnection connection = null;

            try
            {
                using (connection = new SqlConnection(GetConnectionString(ServerName, DatabaseName)))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        if (Timeout != null)
                        {
                            command.CommandTimeout = (int)Timeout;
                        }
                        connection.InfoMessage += OnInfoMessageGenerated;
                        connection.Open();
                        command.StatementCompleted += OnStatementCompleted;
                        catchPrint = true;
                        command.ExecuteNonQuery();
                        SqlRow row = new SqlRow();
                        row.GetPrintValue(caughtPrint);
                        Rows.Add(row);
                    }
                }
            }
            catch (SqlException se)
            {
                ProcessSqlError(se);
                throw;
            }
            catch (Exception e)
            {
                MessageLogging.WriteLine(string.Format("Exception {0}", e.Message));
                throw;
            }
            finally
            {
                catchPrint  = false;
                caughtPrint = "";
            }
        }
Ejemplo n.º 3
0
        public void ExecuteScalar(string query)
        {
            SqlConnection connection = null;

            try
            {
                using (connection = new SqlConnection(GetConnectionString(ServerName, DatabaseName)))
                {
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        if (Timeout != null)
                        {
                            command.CommandTimeout = (int)Timeout;
                        }
                        connection.InfoMessage += OnInfoMessageGenerated;
                        connection.Open();
                        command.StatementCompleted += OnStatementCompleted;
                        Object value = command.ExecuteScalar();
                        SqlRow row   = new SqlRow();
                        row.GetScalarValue(value);
                        Rows.Add(row);
                        MessageLogging.WriteLine(string.Format("DataTypeName: {0}, Value: {1}",
                                                               row.ColumnOrdinal[0].DataTypeName, row.ColumnOrdinal[0].ToString()));
                    }
                }
            }
            catch (SqlException se)
            {
                ProcessSqlError(se);
                throw;
            }
            catch (Exception e)
            {
                MessageLogging.WriteLine(string.Format("Exception {0}", e.Message));
                throw;
            }
        }