解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.IO;public string connectionString
    {
        get;
        private set;
    }public DBOperator(string connectionString)
    {
        this.connectionString = ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString;
    }
    public DataTable GetDataTable(SqlCommand sqlCommand)
    {
        ChangeNullToDBNullValue(sqlCommand);
        bool useDefaultConnection = false;
        if (sqlCommand.Connection == null)
        {
            useDefaultConnection = true;
            sqlCommand.Connection = new SqlConnection(this.connectionString);
        }
        else
        {
            useDefaultConnection = false;
            if (sqlCommand.Connection.State != ConnectionState.Closed)
            {
                throw new ArgumentException("SqlCommand's connection state must be closed.");
            }
        }    sqlCommand.Connection.Open();
        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
        DataTable dt = new DataTable();
        da.SelectCommand = sqlCommand;
        try
        {
            da.Fill(dt);
        }
        catch (Exception ex)
        {
            DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
        }
        sqlCommand.Connection.Close();
        return dt;
    }public ExecuteResult ExecuteNonQuery(SqlCommand sqlCommand)
    {
        ChangeNullToDBNullValue(sqlCommand);
        bool useDefaultConnection = false;
        if (sqlCommand.Connection == null)
        {
            useDefaultConnection = true;
            sqlCommand.Connection = new SqlConnection(this.connectionString);
        }
        else
        {
            useDefaultConnection = false;
            if (sqlCommand.Connection.State != ConnectionState.Closed)
            {
                throw new ArgumentException("SqlCommand's connection state must be closed.");
            }
        }
        sqlCommand.Connection.Open();
        sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
        try
        {
            int rows = sqlCommand.ExecuteNonQuery();
            sqlCommand.Transaction.Commit();
            return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = rows };
        }
        catch (SqlException ex)
        {
            sqlCommand.Transaction.Rollback();
            DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
            return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
        }
    }public ExecuteResult ExecuteScalar(SqlCommand sqlCommand)
    {
        ChangeNullToDBNullValue(sqlCommand);
        bool useDefaultConnection = false;
        if (sqlCommand.Connection == null)
        {
            useDefaultConnection = true;
            sqlCommand.Connection = new SqlConnection(this.connectionString);
        }
        else
        {
            useDefaultConnection = false;
            if (sqlCommand.Connection.State != ConnectionState.Closed)
            {
                throw new ArgumentException("SqlCommand's connection state must be closed.");
            }
        }
        sqlCommand.Connection.Open();
        sqlCommand.Transaction = sqlCommand.Connection.BeginTransaction();
        try
        {
            object returnValue = sqlCommand.ExecuteScalar();
            sqlCommand.Transaction.Commit();
            return new ExecuteResult() { ActionStatus = ActionStatusType.Success, ReturnValue = returnValue };
        }
        catch (SqlException ex)
        {
            sqlCommand.Transaction.Rollback();
            DBOperatorLogsWritter.WriteDBErrorLog(ex, sqlCommand);
            return new ExecuteResult() { ActionStatus = ActionStatusType.Fail, Message = ex.Message };
        }
    }
    private void ChangeNullToDBNullValue(SqlCommand command)
    {
        foreach (SqlParameter para in command.Parameters)
        {
            if (para.Value == null)
            {
                para.Value = DBNull.Value;
            }
            else
            {
                if (para.Value is string)
                {
                    if (string.IsNullOrWhiteSpace(para.Value.ToString()))
                    {
                        para.Value = string.Empty;
                    }
                }
            }
        }
    }