如何改进此数据库连接类? (ADO.NET)

时间:2017-10-17 10:59:16

标签: c# model-view-controller ado.net

我正在尝试创建数据库访问层。我正在寻找一些关于最佳实践的课程/建议的改进。如果有人能够向我指出有关如何进行此项工作/需要考虑的事项的文档,将会很有帮助。我已经看过使用实体框架,但它似乎不适用,但是,我真的应该转向EF吗? ADO.NET是一种过时的方式吗?

    public static IDbCommand GetCommandObject(string Connstring)
    {
        IDbConnection conn = new SqlConnection(Connstring);
        return new SqlCommand { Connection = (SqlConnection)conn };
    }

    public static void AddParameter(ref IDbCommand cmd, string Name, object value, DbType ParamType)
    {
        IDbDataParameter Param = cmd.CreateParameter();
        Param.DbType = ParamType;
        Param.ParameterName = (Name.StartsWith("@")) ? "" : "@"; //@ character for MS SQL database
        Param.Value = value;

        cmd.Parameters.Add(Param);
    }

    public static Int32 ExecuteNonQuery(string SQL, IDbCommand cmd = null)
    {
        Boolean CommitTrans = true;
        Boolean CloseConn = true;
        SqlTransaction Trans = null;

        try
        {
            //IF Required - create command object if required
            if (cmd == null) { cmd = DB.GetCommandObject(""); }

            //Add the commandtext
            cmd.CommandText = SQL;

            if (cmd.Connection == null) { throw new Exception("No connection set"); }

            //IF REQUIRED - open the connection
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            else
            {
                CloseConn = false;
            }

            if (cmd.Transaction != null)
            {
                //We have already been passed a Transaction so dont close it
                CommitTrans = false;
            }
            else
            {
                //Create and open a new transaction
                Trans = (SqlTransaction)cmd.Connection.BeginTransaction();
                cmd.Transaction = Trans;
            }

            Int32 rtn = cmd.ExecuteNonQuery();

            if (CommitTrans == true) { Trans.Commit(); }

            return rtn;
        }
        catch (Exception e)
        {
            if (CommitTrans == true) { Trans.Rollback(); }

            throw new Exception();
        }
        finally
        {
            if (CloseConn == true)
            {
                cmd.Connection.Close();
                cmd = null;
            }

        }

    }

    public static object ExecuteScalar(string SQL, IDbCommand cmd, Boolean NeedsTransaction = true)
    {
        Boolean CommitTrans = true;
        Boolean CloseConn = true;
        SqlTransaction Trans = null;

        try
        {
            //IF Required - create command object if required
            if (cmd == null) { cmd = DB.GetCommandObject(""); }

            //Add the commandtext
            cmd.CommandText = SQL;

            //IF REQUIRED - create default Connection to CourseBuilder DB
            if (cmd.Connection == null) { throw new Exception("No Connection Object"); }

            //IF REQUIRED - open the connection
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            else
            {
                CloseConn = false;
            }

            if (NeedsTransaction == true)
            {
                if (cmd.Transaction != null)
                {
                    //We have already been passed a Transaction so dont close it
                    CommitTrans = false;
                }
                else
                {
                    //Create and open a new transaction
                    Trans = (SqlTransaction)cmd.Connection.BeginTransaction();
                    cmd.Transaction = Trans;
                }
            }

            Object rtn = cmd.ExecuteScalar();

            if (NeedsTransaction == true && CommitTrans == true) { Trans.Commit(); }

            return rtn;
        }
        catch
        {
            if (NeedsTransaction == true && Trans != null) { Trans.Rollback(); }
            throw new Exception();
        }
        finally
        {
            if (CloseConn == true) { cmd.Connection.Close(); cmd = null; }
        }



    }

    public static DataRow GetDataRow(string SQL, IDbCommand cmd = null, Boolean ErrorOnEmpty = true)
    {
        var dt = FillDatatable(SQL, ref cmd);
        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0];
        }
        else
        {
            if (ErrorOnEmpty == true) { throw new Exception(nameof(GetDataRow) + " returned no rows."); }
            return null;
        }
    }

    public static DataTable FillDatatable(string SQL, ref IDbCommand cmd)
    {

        string newline = System.Environment.NewLine;
        var DT = new DataTable();
        Boolean LeaveConOpen = false;

        try
        {
            //Add the commandtext
            cmd.CommandText = SQL;

            //IF REQUIRED - create default Connection to CourseBuilder DB
            if (cmd?.Connection == null) { throw new Exception("No Connection Object"); }

            if (cmd.Connection.State != ConnectionState.Open)
            {
                cmd.Connection.Open();
                LeaveConOpen = false;
            }
            else
            {
                LeaveConOpen = true;
            }

            var DA = new SqlDataAdapter((SqlCommand)cmd);
            DA.Fill(DT);
        }
        catch (Exception ex)
        {
            var sbErr = new StringBuilder();

            sbErr.AppendLine("Parameters (type defaults to varchar(max)):" + newline);
            foreach (SqlParameter p in cmd.Parameters)
            {
                string s = "";
                sbErr.AppendLine("declare " + p.ParameterName + " varchar(max) = '" + (p.Value == DBNull.Value ? "Null" : p.Value + "'; ") + newline);
            }

            sbErr.AppendLine(newline + SQL + newline);

            throw new Exception("Failed to FillDataTable:" + newline + newline + sbErr.ToString(), ex);

        }
        finally
        {
            if (LeaveConOpen == false) { cmd.Connection.Close(); }

        }

        return DT;

    }

    public static T CheckNull<T>(T value, T DefaultValue)
    {
        if (value == null || value is System.DBNull)
        {
            return DefaultValue;

        }
        else
        {
            return value;
        }
    }

1 个答案:

答案 0 :(得分:0)

创建DAL时要记住几件事情

  • DAL应该能够满足多个数据库(oracle,sql,mysql等..)
  • 您应该至少拥有每个的DB,Connection,Command和Reader实现。
  • 不要担心连接池
  • 请注意事务范围,尤其是在尝试保存嵌套对象时。 (例如:通过储蓄公司,您将在一次交易中保存公司和公司。员工和员工。)

替代方案是使用像Dapper这样的东西。

enter image description here