2

我编写了 C# 代码,它返回一个DataTable. 所有参数,即Connection, sQuery具有适当的值。

我仍然收到一个错误:

在名称中必须指定过程的所有者,这样可以提高性能

我用谷歌搜索但一无所获。

这是我的代码:

public static DataTable getATM(ref SqlConnection Connection)
{
            DataTable dtReturn;
            string sQuery = "";
            try
            {
                sQuery = "Select ATM From ATM Where Bank=1";
                using (SqlStoredProcedure sspObj = new SqlStoredProcedure(sQuery, Connection, CommandType.Text))
                {
                    dtReturn = sspObj.ExecuteDataTable();

                    sspObj.Dispose();
                }
            }
            catch (Exception xObj)
            {
                dtReturn = new DataTable();
            }

            return dtReturn;
}

SqlStoredProcedure.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Diagnostics;

namespace ReflectionIT.Common.Data.SqlClient {

    public class SqlStoredProcedure : IDisposable {
        public static readonly TraceSource TraceSource = new TraceSource("SqlStoredProcedure");
        private static int _eventId = 0;

        private const string _returnValue = "ReturnValue";
        private SqlCommand _command;
        private bool _connectionOpened = false;

        public SqlStoredProcedure(string name, SqlConnection connection, CommandType comtype)
            : this(name, connection, null, comtype) {
        }

        public SqlStoredProcedure(string name, SqlConnection connection, SqlTransaction transaction, CommandType comtype) {
            if (name.IndexOf('.') == -1) {
                throw new ArithmeticException("In the name the owner of a procedure must be specified, this improves performance");
            }
            _command = new SqlCommand(name, connection, transaction);
            _command.CommandTimeout = 0;
            _command.CommandType = comtype;
            AddReturnValue();
        }

        public void Dispose() {
            if (_command != null) {
                _command.Dispose();
                _command = null;
            }
        }

        virtual public string Name {
            get { return _command.CommandText; }
            set { _command.CommandText = value; }
        }

               virtual public int Timeout {
            get { return _command.CommandTimeout; }
            set { _command.CommandTimeout = value; }
        }
        virtual public SqlCommand Command {
            get { return _command; }
        }

        virtual public SqlConnection Connection {
            get { return _command.Connection; }
            set { _command.Connection = value; }
        }

        virtual public SqlTransaction Transaction {
            get { return _command.Transaction; }
            set { _command.Transaction = value; }
        }

        virtual public SqlParameterCollection Parameters {
            get { return _command.Parameters; }
        }

        virtual public int ReturnValue {
            get { return (int)_command.Parameters[_returnValue].Value; }
        }

        virtual public SqlParameter AddParameter(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction) {
            SqlParameter p;

            if (size > 0) {
                p = new SqlParameter(parameterName, dbType, size);
            } else {
                // size is automacally detected using dbType
                p = new SqlParameter(parameterName, dbType);
            }

            p.Direction = direction;

            Parameters.Add(p);
            return p;
        }

        virtual public SqlParameter AddParameterWithValue(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction,
            object value) {

            SqlParameter p = this.AddParameter(parameterName, dbType, size, direction);

            if (value == null) {
                value = DBNull.Value;
            }

            p.Value = value;

            return p;
        }

        virtual public SqlParameter AddParameterWithStringValue(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction,
            string value,
            bool emptyIsDBNull) {

            SqlParameter p = this.AddParameter(parameterName, dbType, size, direction);

            if (value == null) {
                p.Value = DBNull.Value;
            } else {
                value = value.TrimEnd(' ');
                if (emptyIsDBNull && value.Length == 0) {
                    p.Value = DBNull.Value;
                } else {
                    p.Value = value;
                }
            }

            return p;
        }

        virtual protected SqlParameter AddReturnValue() {
            SqlParameter p = Parameters.Add(
                new SqlParameter(_returnValue,
                    SqlDbType.Int,
                /* int size */ 4,
                    ParameterDirection.ReturnValue,
                /* bool isNullable */ false,
                /* byte precision */ 0,
                /* byte scale */ 0,
                /* string srcColumn */ string.Empty,
                    DataRowVersion.Default,
                /* value */ null));

            return p;
        }

        virtual public int ExecuteNonQuery() {
            int rowsAffected = -1;

            try {
                Prepare("ExecuteNonQuery");

                rowsAffected = _command.ExecuteNonQuery();

                TraceResult("RowsAffected = " + rowsAffected.ToString());
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return rowsAffected;
        }

        virtual public SqlDataReader ExecuteReader() {
            SqlDataReader reader;
            try {
                Prepare("ExecuteReader");

                reader = _command.ExecuteReader();

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public SqlDataReader ExecuteReader(CommandBehavior behavior) {
            SqlDataReader reader;
            try {
                Prepare("ExecuteReader");

                reader = _command.ExecuteReader(behavior);

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public object ExecuteScalar() {
            object val = null;

            try {
                Prepare("ExecuteScalar");

                val = _command.ExecuteScalar();

                TraceResult("Scalar Value = " + Convert.ToString(val));
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return val;
        }

        virtual public XmlReader ExecuteXmlReader() {
            XmlReader reader;

            try {
                Prepare("ExecuteXmlReader");

                reader = _command.ExecuteXmlReader();

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public DataSet ExecuteDataSet() {
            DataSet dataset = new DataSet();

            this.ExecuteDataSet(dataset);

            return dataset;
        }

        virtual public DataSet ExecuteDataSet(DataSet dataSet) {
            try {
                Prepare("ExecuteDataSet");

                SqlDataAdapter a = new SqlDataAdapter(this.Command);
                a.Fill(dataSet);

                TraceResult("# Tables in DataSet = " + dataSet.Tables.Count);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return dataSet;
        }

        virtual public DataTable ExecuteDataTable() {
            DataTable dt = null;
            try {
                Prepare("ExecuteDataTable");

                SqlDataAdapter a = new SqlDataAdapter(this.Command);
                dt = new DataTable();
                a.Fill(dt);

                TraceResult("# Rows in DataTable = " + dt.Rows.Count);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }

            return dt;
        }

        protected Exception TranslateException(SqlException ex) {
            Exception dalException = null;

            SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Error, _eventId, "{0} throwed exception: {1}", this.Name, ex.ToString());

            foreach (SqlError error in ex.Errors) {
                if (error.Number >= 50000) {
                    dalException = new DalException(error.Message, ex);
                }
            }

            if (dalException == null) {
                switch (ex.Number) {
                    case 17:                    
                    case 4060:                    
                    case 18456:              
                        dalException = new DalLoginException(ex.Message, ex);
                        break;
                    case 547:                      
                        dalException = new DalForeignKeyException(ex.Message, ex);
                        break;
                    case 1205:                  
                        dalException = new DalDeadLockException(ex.Message, ex);
                        break;
                    case 2627:
                    case 2601:                        
                        dalException = new DalUniqueConstraintException(ex.Message, ex);
                        break;
                    default:                  
                        dalException = new DalException(ex.Message, ex);
                        break;
                }
            }         
            return dalException;
        }

        protected void Prepare(string executeType) {
            _eventId++;
            if (_eventId > ushort.MaxValue) {
                _eventId = 0;
            }

            SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Information, _eventId, "{0}: {1}", executeType, this.Name);

            TraceParameters(true);

            if (_command.Connection.State != ConnectionState.Open) {
                _command.Connection.Open();
                _connectionOpened = true;
            }
        }

        private void TraceParameters(bool input) {
            if (SqlStoredProcedure.TraceSource.Switch.ShouldTrace(TraceEventType.Verbose) && this.Parameters.Count > 0) {
                foreach (SqlParameter p in this.Parameters) {
                    bool isInput = p.Direction != ParameterDirection.ReturnValue && p.Direction != ParameterDirection.Output;
                    bool isOutput = p.Direction != ParameterDirection.Input;
                    if ((input && isInput) || (!input && isOutput)) {
                        SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Verbose, _eventId, "SqlParamter: Name = {0}, Value = '{1}', Type = {2}, Size = {3}", p.ParameterName, p.Value, p.DbType, p.Size);
                    }
                }
            }
        }

        protected void CloseOpenedConnection() {
            if ((_command.Connection.State == ConnectionState.Open) & _connectionOpened)
                _command.Connection.Close();
        }

        protected void TraceResult(string result) {
            if (result != null) {
                SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Verbose, _eventId, "Result: {0}", result);
            }

            TraceParameters(false);
        }

    }
}
4

1 回答 1

1

SqlStoredProcedure()正在检查您的存储过程名称中是否有一个点,如果没有抛出该异常,请在您的存储过程名称前加上“dbo”。或者不管它是什么,例如:

dbo.myProcedureName

从某种意义上说,它提高了性能,因为 SQL 不必首先搜索所有用户来查找 proc。

于 2012-10-06T10:19:54.163 回答