在DLL中设置SqlConnection [英] Set SqlConnection in a DLL

查看:89
本文介绍了在DLL中设置SqlConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从3个班级创建了一个混蛋.为此,我使用SqlConnection处理sql数据库.目前,我正在对ConnectionString进行硬编码,并在dataAccess类中创建sqlConnection.但是在创建DLL之后,用户需要根据需要设置连接.设计此内容的最佳方法是什么.

I create a assebly from 3 classes. For that one I''m using SqlConnection to deal with the sql database. Currently I''m hardcoding the ConnectionString and creating the sqlConnection inside the dataAccess class. But after creating the DLL user need to set the connection as he wants. What is the best way to design this thing.

推荐答案

如果您尝试将其用作通用控件,我会将连接字符串传递给您的代码,如下所示:初始化的一部分:通过构造函数(首选)或通过静态实例.这样,取决于应用程序如何以及在何处存储它,而不是DLL.由于该应用程序比DLL更了解它们将在其中运行的环境,所以确实有一定意义!
If you are trying to use this as a general purpose control, I would pass the connection string to your code as part of the initilization: either via the constructor (preferred) or via a static instance. That way it is up to the application how and where he stores it, rather than the DLL. Since the App is more "aware" of the environment they will be running in than the DLL, that does make some sense!


您可以使用Configuration对象的Settings对象,也可以使用自己设计解决方案.

尝试在解决方案资源管理器中的对象下展开属性"树视图项目,您将看到Settings.settings.您可以在其中添加连接字符串,并在代码中进行检索.另一种解决方案是将新文件添加到您的项目(应用程序配置),然后在ConnectionStrings部分中添加connectionstring.
You can use the Settings object, of the Configuration object, or design a solution yourself.

Try expanding the ''Properties'' treeview item under your ptoject in the solution explorer, you''ll see Settings.settings. You can add the connectionstring there and retrieve is in code. The other solution is to add a new file to your project (Application Configuration) and add the connectionstring in the ConnectionStrings section.
<?xml version="1.0"?>
<configuration>
 <connectionStrings>
        <clear />
        <add name="Name" connectionString="ConnectionStringX" />
 </connectionStrings>
</configuration>



您必须引用System.Configuration库才能调用ConfigurationManager.ConnectionStrings["Name"].ConnectionString,它将返回连接字符串(在本例中为 ConnectionStringX )

玩得开心!

爱德华



You must reference the System.Configuration library in order to call the ConfigurationManager.ConnectionStrings["Name"].ConnectionString which will return the connectionstring (in this case ConnectionStringX)

Have Fun!

Eduard


这篇关于在DLL中设置SqlConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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