C# 我可以在类库项目中创建数据库连接吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/987031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I create a Database connection in a Class Library Project?
提问by MrM
I am setting up a .net project that is called to generate other webpages. Basically a true CODE BEHIND page. How do I go about making a connection on a Class Library File, when there is no webconfig file present/available?
我正在设置一个 .net 项目,该项目被调用以生成其他网页。基本上是一个真正的代码隐藏页面。当没有 webconfig 文件存在/可用时,如何在类库文件上建立连接?
采纳答案by theJerm
In this case, I would create a 'Base Page' that derives from System.Web.UI.Page. On this page, you would create a property called 'ConnectionString'. You will have all of your web pages you create inherit from this page.
在这种情况下,我将创建一个派生自 System.Web.UI.Page 的“基本页面”。在此页面上,您将创建一个名为“ConnectionString”的属性。您创建的所有网页都将从该页面继承。
Example:
例子:
public partial class BasePage : System.Web.UI.Page
{
protected string ConnectionString
{
get
{
return System.Configuration.ConfigurationSettings.AppSettings["MyConnectionString"];
}
}
}
And in your web.config
在你的 web.config 中
<appSettings>
<!-- Connection String -->
<add key="MyConnectionString" value="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"/>
</appSettings>
Now you can have a web.config file with a connection string that is easily readable. And you can store this 'Base Page' in your class library, no problem. As long as your web pages inheriting from it are using the web.config file with the connection string.
现在,您可以拥有一个带有易于阅读的连接字符串的 web.config 文件。您可以将这个“基页”存储在您的类库中,没问题。只要从它继承的网页使用带有连接字符串的 web.config 文件。
After that, it's a simple matter of connecting using that string. You can do this in a variety of ways, either in your web pages, or in separate classes (I recommend separate classes). In this case, if you had a separate class, you would need to pass in the ConnectionString property to your connecting functions:
之后,使用该字符串进行连接就很简单了。您可以通过多种方式执行此操作,无论是在您的网页中,还是在单独的类中(我推荐单独的类)。在这种情况下,如果您有一个单独的类,则需要将 ConnectionString 属性传递给您的连接函数:
public void ExecuteQuery(string connectionString, string sql)
{
using (System.Data.SqlClient.SqlConnection theConnection = new System.Data.SqlClient.SqlConnection(connectionString))
using (System.Data.SqlClient.SqlCommand theCommand = new System.Data.SqlClient.SqlCommand(sql, theConnection))
{
theConnection.Open();
theCommand.CommandType = CommandType.Text;
theCommand.ExecuteNonQuery();
}
}
or you can create a function that does not take a connection string parameter, and just reads from the web.config file. It sounds like you may want to put your connecting and data access code in a class library for good separation of data and content. Hope this helps!
或者您可以创建一个不接受连接字符串参数的函数,并且只从 web.config 文件中读取。听起来您可能希望将连接和数据访问代码放在类库中,以便很好地分离数据和内容。希望这可以帮助!
回答by Cody C
System.Data namespace. If using SQL Server, a SQLConnection object.
System.Data 命名空间。如果使用 SQL Server,则为 SQLConnection 对象。
I use the Application Data Blocks for data access b/c I think it is a time saver. That framework is great for SQL Server access.
我使用应用程序数据块进行数据访问 b/c 我认为它可以节省时间。该框架非常适合 SQL Server 访问。
回答by Paddy
Will your library be called from an ASP.net site? If so, you can still use the same methods for accessing the application settings. Otherwise, you may need some kind of app.config for whatever front end you're putting on this.
会从 ASP.net 站点调用您的库吗?如果是这样,您仍然可以使用相同的方法访问应用程序设置。否则,您可能需要某种 app.config 来用于您放置的任何前端。
回答by Wyatt Barnett
Also, you don't need a connecting in your configuration in most class libraries, unless you are using an ORM which needs to use it to pick up some info. The calling application's configuration will be used when you are running the thing, so it will pick up the configuration out of the web.config or whatever.
此外,您不需要在大多数类库的配置中进行连接,除非您使用的 ORM 需要使用它来获取一些信息。当您运行该事物时,将使用调用应用程序的配置,因此它将从 web.config 或其他内容中提取配置。
回答by Jared
There are several ways to solve this:
有几种方法可以解决这个问题:
1) If you're class library is really going to generate web pages, then it will probably be deployed on the server with the web.config and so can use properties of the web.config file. If you don't want to use the ConnectionString part, then you can just make an appSetting with the connection string.
1) 如果您的类库真的要生成网页,那么它可能会使用 web.config 部署在服务器上,因此可以使用 web.config 文件的属性。如果您不想使用 ConnectionString 部分,那么您可以使用连接字符串创建一个 appSetting。
The benefits of doing this are that it's easy to change or have different connection strings for different environments (testing, deployment, etc.). And if you decide to use the class library in another kind of app, you can use the app.config file with the same setting.
这样做的好处是可以轻松更改或针对不同环境(测试、部署等)使用不同的连接字符串。如果您决定在另一种应用程序中使用类库,您可以使用具有相同设置的 app.config 文件。
Depending on the type of database, you'll be using the System.Data and possibly some of the sub-namespaces to make this work.
根据数据库的类型,您将使用 System.Data 和可能的一些子命名空间来完成这项工作。
2) You can hard code the connection string into the code. Ugly, but workable in a pinch.
2) 您可以将连接字符串硬编码到代码中。丑陋,但在紧要关头可以使用。
3) You could use an entity framework to help you out and cut down on the work that you have to do.
3)您可以使用实体框架来帮助您并减少您必须做的工作。