如何创建DataAccessLayer?

时间:2011-01-17 07:34:19

标签: c#

我正在.Net中创建数据库应用程序。我正在使用DataAccessLayer与数据库通信.net对象,但我不确定这个类是否正确可以任何人交叉检查并纠正任何错误

namespace IDataaccess
{
    #region Collection Class

    public class SPParamCollection : List<SPParams>
    {

    }
    public class SPParamReturnCollection : List<SPParams>
    {

    }
    #endregion
    #region struct
    public struct SPParams
    {
        public string Name
        {
            get;
            set;
        }
        public object Value
        {
            get;
            set;
        }
        public ParameterDirection ParamDirection
        {
            get;
            set;
        }
        public SqlDbType Type
        {
            get;
            set;
        }
        public int Size
        {
            get;
            set;
        }
        public string TypeName
        {
            get;
            set;
        }
        // public string datatype;
    }
    #endregion
    /// <summary>
    /// Interface DataAccess Layer implimentation New version 
     /// </summary>

    public interface IDataAccess
    {
        DataTable getDataUsingSP(string spName);
        DataTable getDataUsingSP(string spName, SPParamCollection spParamCollection);
        DataSet getDataSetUsingSP(string spName);
        DataSet getDataSetUsingSP(string spName, SPParamCollection spParamCollection);
        SqlDataReader getDataReaderUsingSP(string spName);
        SqlDataReader getDataReaderUsingSP(string spName, SPParamCollection spParamCollection);
        int executeSP(string spName);
        int executeSP(string spName, SPParamCollection spParamCollection, bool addExtraParmas);
        int executeSP(string spName, SPParamCollection spParamCollection);
        DataTable getDataUsingSqlQuery(string strSqlQuery);
        int executeSqlQuery(string strSqlQuery);
        SPParamReturnCollection executeSPReturnParam(string spName, SPParamReturnCollection spParamReturnCollection);
        SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection);
        SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection, bool addExtraParmas);
        int executeSPReturnParam(string spName, SPParamCollection spParamCollection, ref SPParamReturnCollection spParamReturnCollection);
        object getScalarUsingSP(string spName);
        object getScalarUsingSP(string spName, SPParamCollection spParamCollection);
    }
}

using IDataaccess;
namespace Dataaccess
{
    /// <summary>
    /// Class DataAccess Layer implimentation New version 
    /// </summary>

    public class DataAccess : IDataaccess.IDataAccess
    {
        #region Public variables
        static string Strcon;
        DataSet dts = new DataSet();

        public DataAccess()
        {
            Strcon = sReadConnectionString();
        }
        private string sReadConnectionString()

        {
            try
            {
                //dts.ReadXml("C:\\cnn.config");
                //Strcon = dts.Tables[0].Rows[0][0].ToString();

                //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                //Strcon = config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;

                // Add an Application Setting.




               //Strcon = "Data Source=192.168.50.103;Initial Catalog=erpDB;User ID=ipixerp1;Password=NogoXVc3";
             Strcon =  System.Configuration.ConfigurationManager.AppSettings["connection"];
                //Strcon = System.Configuration.ConfigurationSettings.AppSettings[0].ToString();
            }

            catch (Exception)
            {
            }
            return Strcon;
        }

        public SqlConnection connection;
        public SqlCommand cmd;
        public SqlDataAdapter adpt;
        public DataTable dt;
        public int intresult;
        public SqlDataReader sqdr;
        #endregion
        #region Public Methods
        public DataTable getDataUsingSP(string spName)
        {
            return getDataUsingSP(spName, null);
        }
        public DataTable getDataUsingSP(string spName, SPParamCollection spParamCollection)
        {
            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        adpt.Fill(dt);
                        return (dt);
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }

        public DataSet getDataSetUsingSP(string spName)
        {
            return getDataSetUsingSP(spName, null);
        }
        public DataSet getDataSetUsingSP(string spName, SPParamCollection spParamCollection)
        {
            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        {
                            param = -1;
                        }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adpt.Fill(ds);
                        return ds;
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public SqlDataReader getDataReaderUsingSP(string spName)
        {
            return getDataReaderUsingSP(spName, null);
        }
        public SqlDataReader getDataReaderUsingSP(string spName, SPParamCollection spParamCollection)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        sqdr = cmd.ExecuteReader();

                        return (sqdr);
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public int executeSP(string spName)
        {
            return executeSP(spName, null);
        }
        public int executeSP(string spName, SPParamCollection spParamCollection, bool addExtraParmas)
        {
            try
            {

                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            SqlParameter par = new SqlParameter(spParamCollection[count].Name, spParamCollection[count].Value);
                            if (addExtraParmas)
                            {
                                par.TypeName = spParamCollection[count].TypeName;
                                par.SqlDbType = spParamCollection[count].Type;
                            }
                            cmd.Parameters.Add(par);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 60;
                        return (cmd.ExecuteNonQuery());
                    }
                }
            }

            finally
            {
                connection.Close();
            }

        }
        public int executeSP(string spName, SPParamCollection spParamCollection)
        {

            return executeSP(spName, spParamCollection, false);

        }

        public DataTable getDataUsingSqlQuery(string strSqlQuery)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                    connection.Open();
                {
                    using (cmd = new SqlCommand(strSqlQuery, connection))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandTimeout = 60;
                        adpt = new SqlDataAdapter(cmd);
                        dt = new DataTable();
                        adpt.Fill(dt);
                        return (dt);
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public int executeSqlQuery(string strSqlQuery)
        {

            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(strSqlQuery, connection))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandTimeout = 60;
                        intresult = cmd.ExecuteNonQuery();
                        return (intresult);
                    }
                }
            }
            finally
            {
                connection.Close();
            }

        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamReturnCollection spParamReturnCollection)
        {
            return executeSPReturnParam(spName, null, spParamReturnCollection);
        }
        public int executeSPReturnParam()
        {
            return 0;
        }

        public int executeSPReturnParam(string spName, SPParamCollection spParamCollection, ref SPParamReturnCollection spParamReturnCollection)
        {
            try
            {
                SPParamReturnCollection spParamReturned = new SPParamReturnCollection();
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        foreach (SPParams paramReturn in spParamReturnCollection)
                        {
                            SqlParameter _parmReturn = new SqlParameter(paramReturn.Name, paramReturn.Size);
                            _parmReturn.Direction = paramReturn.ParamDirection;
                            if (paramReturn.Size > 0) _parmReturn.Size = paramReturn.Size;
                            else _parmReturn.Size = 32;
                            _parmReturn.SqlDbType = paramReturn.Type;
                            cmd.Parameters.Add(_parmReturn);
                        }
                        cmd.CommandTimeout = 60;
                        intresult = cmd.ExecuteNonQuery();
                        connection.Close();

                        //for (int i = 0; i < spParamReturnCollection.Count; i++)
                        //{
                        //    spParamReturned.Add(new SPParams
                        //    {
                        //        Name = spParamReturnCollection[i].Name,
                        //        Value = cmd.Parameters[spParamReturnCollection[i].Name].Value
                        //    });

                        //}
                    }
                }
                return intresult;
            }
            finally
            {
                connection.Close();

            }
        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection)
        {
            return executeSPReturnParam(spName, spParamCollection, spParamReturnCollection, false);
        }
        public SPParamReturnCollection executeSPReturnParam(string spName, SPParamCollection spParamCollection, SPParamReturnCollection spParamReturnCollection, bool addExtraParmas)
        {
            try
            {
                SPParamReturnCollection spParamReturned = new SPParamReturnCollection();
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            //cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                            SqlParameter par = new SqlParameter(spParamCollection[count].Name, spParamCollection[count].Value);
                            if (addExtraParmas)
                            {
                                par.TypeName = spParamCollection[count].TypeName;
                                par.SqlDbType = spParamCollection[count].Type;
                            }
                            cmd.Parameters.Add(par);
                         }
                        cmd.CommandType = CommandType.StoredProcedure;
                        foreach (SPParams paramReturn in spParamReturnCollection)
                        {
                            SqlParameter _parmReturn = new SqlParameter(paramReturn.Name, paramReturn.Value);
                            _parmReturn.Direction = paramReturn.ParamDirection;
                            if (paramReturn.Size > 0) _parmReturn.Size = paramReturn.Size;
                            else _parmReturn.Size = 32;
                            _parmReturn.SqlDbType = paramReturn.Type;
                            cmd.Parameters.Add(_parmReturn);
                        }
                        cmd.CommandTimeout = 60;
                        cmd.ExecuteNonQuery();
                        connection.Close();

                        for (int i = 0; i < spParamReturnCollection.Count; i++)
                        {
                            spParamReturned.Add(new SPParams
                            {
                                Name = spParamReturnCollection[i].Name,
                                Value = cmd.Parameters[spParamReturnCollection[i].Name].Value

                            });

                        }
                    }
                }
                return spParamReturned;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                connection.Close();
            }
        }
        public object getScalarUsingSP(string spName)
        {
            return getScalarUsingSP(spName, null);
        }
        public object getScalarUsingSP(string spName, SPParamCollection spParamCollection)
        {
            try
            {
                using (connection = new SqlConnection(Strcon))
                {
                    connection.Open();
                    using (cmd = new SqlCommand(spName, connection))
                    {
                        int count, param = 0;
                        if (spParamCollection == null)
                        { param = -1; }
                        else
                        {
                            param = spParamCollection.Count;
                        }
                        for (count = 0; count < param; count++)
                        {
                            cmd.Parameters.AddWithValue(spParamCollection[count].Name, spParamCollection[count].Value);
                            cmd.CommandTimeout = 60;
                        }
                        cmd.CommandType = CommandType.StoredProcedure;
                        return cmd.ExecuteScalar();
                    }
                }
            }
            finally
            {
                connection.Close();
                cmd.Dispose();
            }
        }
        #endregion
    }
}

3 个答案:

答案 0 :(得分:1)

我可以看到您当前的方法并不关心事务环境,现在没有人关心如何创建DAL,当EFnhibernate,......可用时,我建议检查实体框架并将其用于启动检查code first。它可用于2008年和2010年。

答案 1 :(得分:1)

如果您有机会尝试使用可用工具从代码映射到数据库。 NHibernate是一个很受欢迎的MS Entity framework 4.0
你在这里公开的代码在这个页面中要求很复杂,我怀疑你会得到一个具体的答案。

答案 2 :(得分:0)

如果我是代码审查这个,我会拒绝它。我的第一个评论是:这不是数据访问层 - 您可以将其称为数据独立层(实际上您很少需要其中一个)。

数据访问层旨在从调用它的层抽象数据存储库。公开getScalarUsingSP()getDataUsingSP()之类的函数根本不是抽象数据库,它只是创建了一些更适合你的调用方式的包装函数。

通过接口公开内容,您处于正确的轨道,但您需要更改公开的功能。您需要getDataUsingSP()GetSelectedAccounts()进行调用,而不是SaveCustomer()getDataUsingSP()将是私有的,并且会从“友好”的公开函数中调用。