Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# n层应用中的连接控制_C#_N Tier Architecture - Fatal编程技术网

C# n层应用中的连接控制

C# n层应用中的连接控制,c#,n-tier-architecture,C#,N Tier Architecture,我是N层应用程序的新手,我试图通过制作自己的应用程序来了解N层应用程序的工作方式 我读了几篇关于它的文章,但没有什么比一堵文字墙更重要的了(也许我没有找到正确的地方) 虽然我有一个工具可以帮助生成DAL、BLL类,但它就像一个“大纲”。我没有与DAL,BLL合作的经验…所以我做不了多少 下面是由generation tool(如我所说)生成的代码,并由我自己稍加修改 达尔 BLL 我的问题是: 如果我们需要从数据源获取数据,我们必须遵循以下步骤:打开连接->读取数据->关闭读卡器->关闭连接。

我是N层应用程序的新手,我试图通过制作自己的应用程序来了解N层应用程序的工作方式

我读了几篇关于它的文章,但没有什么比一堵文字墙更重要的了(也许我没有找到正确的地方)

虽然我有一个工具可以帮助生成DAL、BLL类,但它就像一个“大纲”。我没有与DAL,BLL合作的经验…所以我做不了多少

下面是由generation tool(如我所说)生成的代码,并由我自己稍加修改

达尔

BLL

我的问题是:

  • 如果我们需要从数据源获取数据,我们必须遵循以下步骤:打开连接->读取数据->关闭读卡器->关闭连接。 我的问题是我不知道在BLL或DAL中,应该将紧密连接语句放在哪里?怎么做?细节越多越好

  • 你能花1分钟看看我的代码吗。够干净吗?或者它还有其他问题吗


  • 对不起,如果我的英语有任何错误。非常感谢。

    请始终在finally block not in catch中关闭您的连接,并在DAL中关闭您的连接。或者更好的做法是,使用
    using
    block(它实际上变成了try-catch finally block,但在阅读代码时更容易理解)。BLL不应打开/关闭与DAL的连接。这是DAL的责任。BLL应该对DAL一无所知,除了可用的方法和这些方法返回的对象。好的,我知道你的意思。但我还是要问你,具体怎么做?DAL打开连接并返回数据读取器。之后,BLL读取数据(DAL不知道BLL何时完成)。你能给我举个例子吗?我想我自己有一个答案。非常感谢。
    Namespace FIRSTAPP.DAL
    {
        class Teacher
        {
            clsOLEDB db = new clsOLEDB();
    
            public OleDbDataReader SelectAll()
            {
                try
                {
                    db.OpenConnection();
                    return db.Excute("SELECT * FROM TEACHER");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
    
            }        
        public OleDbDataReader SelectRow()
            {}
       }
    }
    
    namespace FIRSTAPP.BLL
    {
        class Teacher
        {
            public string Id { get; set; }
            public string FullName { get; set; }
            public DateTime DateOfBirth { get; set; }
    
            public List< Teacher > SelectAll()
            {
                DAL.Teacher dalTeacher = new DAL.Teacher();
                IDataReader dr = dalTeacher.SelectAll();
    
                List<Teacher> TeacherList = new List<Teacher>();
                while (dr.Read())
                {
                    Teacher myTeacher = new Teacher
                    {
                        Id = (string)dr["Id"],
                        FullName = (string)dr["FullName"],
                        DateOfBirth = (DateTime)dr["DateOfBirth "],
                    };
                    TeacherList.Add(myTeacher);
                }
                dr.Close();
    
                return TeacherList;
            }
    
    Public Teacher SelectRow(string TeacherId)
    {}
    }
    }
    
    namespace FIRSTAPP.DB
    {
        public class clsOLEDB
        {
            string mConnectionString = @"Provider = Microsoft.JET.OLEDB.4.0; Data Source = .\FirstAppDB.mdb";
            OleDbConnection mConnection;
            OleDbDataReader dataReader;
            OleDbCommand cmdCommand;
    
            public void OpenConnection()
            {
                mConnection = new OleDbConnection(mConnectionString);
                try
                {
                    mConnection.Open();
                }
                catch
                {
                    throw new Exception("Could not connect to DB");
                }
                finally
                {
                     if ( mConnection != null )
                           mConnection.Close();
                }
            }
    
            public OleDbDataReader Excute(string SQLText)
            { 
                cmdCommand = mConnection.CreateCommand();
                cmdCommand.CommandText = SQLText;
    
                try
                {
                    return cmdCommand.ExecuteReader();
                }
                catch
                {
                    throw new Exception("Has an error when executing");
                }
            }
    public void CloseConnection()
            {
                mConnection.Close();
            }
        }
    }