コード例 #1
0
ファイル: SQLServerHelper.cs プロジェクト: hexu6788/HSQL
        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);
        }
コード例 #2
0
ファイル: SQLServerHelper.cs プロジェクト: hexu6788/HSQL
        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);
        }
コード例 #3
0
ファイル: SQLServerHelper.cs プロジェクト: hexu6788/HSQL
        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);
        }