在Windows窗体应用程序中保留连接字符串和连接对象的最佳做法是什么? [英] what is the best practice to keep connection string and connection object in a windows forms application?

查看:68
本文介绍了在Windows窗体应用程序中保留连接字符串和连接对象的最佳做法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net的初学者,也是我最后一年开发工资单系统的项目。现在我有一些关于ado.net sql连接对象的问题。

为了集中保持连接字符串,我使用了一个单独的类调用'db'。再向这个集中思维迈出了一步,我已经在这个'db'类中集中初始化了连接对象,如下所示。



Im a beginner to .net and for my final year project im developing a payroll system. Now I have some issues regarding ado.net sql connection object.
To keep the connection string centrally I have used a separate class call 'db'. Taking another step to this centralization thinking, Ive initialized the connection object also centrally in this 'db' class as follows.

class db
 {
        string connectionString = ("server=CHATHURANGA-PC\\SQLEXPRESS;" +
                                          "Trusted_Connection=yes;" +
                                          "database=Payroll; " +
                                          "connection timeout=30");
        public SqlConnection GetConn()
        {
            SqlConnection NewConn = new SqlConnection(connectionString);
            return NewConn;
        }
 }



现在我在我的应用程序中使用此连接对象如下...

我只是想要知道我将来是否会因为这种做法而面临问题,并且如果专家之一能够解释我这方面的最佳做法,也会感激。




Now Im using this connection object as follows in my application...
I just want to know whether I would face issues in future because of this practice and also appreciate if one of experts could explain me what is the best practice in this regard.

 class client
 {

        db NewDB = new db(); // db class is instantiated...
        SqlConnection newCon; // object referece newConn is created...


        //Method to insert new clients to 'client' table
 
        public void addNewClient(DateTime entDate, client NewClient)
        {
            try
            {
                newCon = NewDB.GetConn(); // connection object is assigned to newCon... but this is optional and I put this for the clarity

                string CommandString = "INSERT INTO client(Client_Name, C_Add, Contact_Person, C_Mob_No, C_Tel_No, Remarks, Ent_Date)" +
                                        " VALUES (@CName, @CAdd, @CPerson, @CMob, @CTel, @Remarks, @entDate)";
                                              
               
                SqlCommand SqlCom = new SqlCommand();
                SqlCom.CommandText = CommandString;
                SqlCom.Parameters.Add("@CName", SqlDbType.VarChar).Value = NewClient.CName;
                SqlCom.Parameters.Add("@CAdd", SqlDbType.VarChar).Value = NewClient.CAdd;
                SqlCom.Parameters.Add("@CPerson", SqlDbType.VarChar).Value = NewClient.CPerson;
                SqlCom.Parameters.Add("@CMob", SqlDbType.Char).Value = NewClient.CMob;
                SqlCom.Parameters.Add("@CTel", SqlDbType.Char).Value = NewClient.CTel;
                SqlCom.Parameters.Add("@Remarks", SqlDbType.VarChar).Value = NewClient.Remarks;
                SqlCom.Parameters.Add("@entDate", SqlDbType.Date).Value = entDate;
                SqlCom.Connection = newCon;
                newCon.Open();
                
                SqlCom.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }

            finally
            {
                newCon.Close(); // newCon object is global to entire class so can call its close method.
            }
        }
}

推荐答案

我知道你是一个新人,所以我也尽量不要混淆你许多。如果我这样做,请告诉我,好吗?



这不是一个坏主意 - 它是所谓的三层模型的简单形式,其中数据I / O与业务逻辑分开处理,并且两者都与用户界面分开(这些通常分别称为DL(或DAL),BL和PL,并且经常保存在单独的程序集中)。



你应该考虑几件事情:

1)尽量不要硬编码连接字符串:如果你知道如何使用设置文件,然后将其保存在那里更容易使用,因为当您移动到另一台PC时不必更改应用程序,或者只是将SQL服务器移动到另一台PC。

2)我建议将IDisposable接口添加到您的db类 - 它应该检查是否存在任何连接并且是打开的,并在对象被丢弃时关闭它们。
I know you are a newby, so I'll try not to confuse you too much. If I do, let me know, OK?

That's not a bad idea - it's a simple form of what is known as a Three-Tier Model, where the data I/O is handled separated from the business logic, and both are kept separate from the user interface (These are often known as a DL (or DAL), BL and PL respectively, and are frequently kept in separate assemblies).

There are couple of things you should consider:
1) try not to "hard code" connection strings: if you know how to use settings files, then keeping it in there is a lot easier to work with, as you don't have to change your application when you move to a different PC, or just move your SQL server to a different PC.
2) I would recommend adding the IDisposable interface to your db class - and it should check if any connections exist and are open, and close them when the object is disposed.


它取决于该应用程序。我主要编写控制台应用程序,因此能够在命令行上指定连接细节至关重要。

我肯定不会存储连接字符串;他们太具体了。我更喜欢存储允许应用程序创建连接字符串所需的部分(例如服务器名称和数据库名称)。



虽然我为你鼓掌使用参数,我想指出设置数据类型是浪费精力 - 设置值时将设置数据类型。



和在你的例子中,catch / throw是不必要的。使用语句尝试
It depends on the app. I write mostly console apps, so being able to specify the connection details on the command line is vital.
I would certainly not store a "connection string"; they're too concrete. I prefer to store the parts that are required to allow the app to create the connection string (the server name and database name for example) .

While I applaud you for using parameters, I'd like to point out that setting the datatype is a waste of effort -- the datatype will be set when you set the Value.

And the catch/throw is needless in your example. Try a using statement.


这篇关于在Windows窗体应用程序中保留连接字符串和连接对象的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆