Esempio n. 1
0
        public int ExecuteNonQuery(bool isNewConnection, bool consolePrintSql, string commandText, params IDbDataParameter[] parameters)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException("执行命令不能为空");
            }

            int result = 0;

            if (isNewConnection == true)
            {
                using (IDbConnection connection = new SqlConnection(_connectionString))
                {
                    connection.Open();
                    result = ExecuteNonQuery(connection, consolePrintSql, commandText, parameters);
                }
            }
            else
            {
                using (IConnector connector = SQLServerConnectionPools.GetConnector())
                {
                    result = ExecuteNonQuery(connector.GetConnection(), consolePrintSql, commandText, parameters);
                }
            }
            return(result);
        }
Esempio n. 2
0
        public List <T> ExecuteList <T>(bool consolePrintSql, List <PropertyInfo> propertyInfoList, string commandText, params IDbDataParameter[] parameters)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException("执行命令不能为空");
            }

            List <T> list = new List <T>();

            using (IConnector connector = SQLServerConnectionPools.GetConnector())
            {
                IDbCommand command = connector.GetConnection().CreateCommand();
                command.CommandText = commandText;
                foreach (IDbDataParameter parameter in parameters)
                {
                    command.Parameters.Add(parameter);
                }
                PrintSql(consolePrintSql, commandText);
                IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                list = InstanceFactory.CreateListAndDisposeReader <T>(reader, propertyInfoList);
            }
            return(list);
        }
Esempio n. 3
0
        public object ExecuteScalar(bool consolePrintSql, string commandText, params IDbDataParameter[] parameters)
        {
            if (string.IsNullOrWhiteSpace(commandText))
            {
                throw new ArgumentNullException("执行命令不能为空");
            }

            object result = null;

            using (IConnector connector = SQLServerConnectionPools.GetConnector())
            {
                using (IDbCommand command = connector.GetConnection().CreateCommand())
                {
                    command.CommandText = commandText;
                    foreach (IDbDataParameter parameter in parameters)
                    {
                        command.Parameters.Add(parameter);
                    }
                    PrintSql(consolePrintSql, commandText);
                    result = command.ExecuteScalar();
                }
            }
            return(result);
        }