Ejemplo n.º 1
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;
            }
        }