ASP.NET 数据库连接
我的 ASP.NET 项目的 web.config
文件中有正确的条目。如何确保连接已成功建立?我想从数据库中提取图像,然后将其显示在我的 aspx
页面上。需要注意的一些事项:我使用的是 Visual Studio 2010、SQL Server 2008、.NET 4.0
这是我的 web.config
文件中的相关部分,
<databases>
<add key="MyConnection" name="MyName" value="server=servername\SQL2008;uid=myuid;pwd=mypwd;database=databaseName;"/>
<add key="DataAccessClass" value="DataAccessSql"/>
</databases>
我的项目中还没有任何 app.config
文件。令人惊讶的是,我的 web.config
中没有 部分。我需要明确添加一项吗?
I have the correct entries in the web.config
file in my ASP.NET project. How do I make sure that the connection has been established successfully? I want to pull an image form my database and then display it on my aspx
page. Some things to note: I am using Visual Studio 2010, SQL server 2008, .NET 4.0
Here is the relevant part from my web.config
file
<databases>
<add key="MyConnection" name="MyName" value="server=servername\SQL2008;uid=myuid;pwd=mypwd;database=databaseName;"/>
<add key="DataAccessClass" value="DataAccessSql"/>
</databases>
I do not have any app.config
file in my project yet. Surprisingly there is not section in my web.config
. Do I need to add one explicitly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Web.config 文件只是存储配置设置。为了建立与数据库的连接,您需要编写一些代码。
ASP.NET - 数据库连接
创建与 SQL Server 的连接
Web.config file is just a store for configuration settings. In order to establish a connection to the database you need to write some code.
ASP.NET - Database Connection
Creating Connections to SQL Server
web.config 文件中输入的连接参数只是参数。没有建立实际的连接。事实上,可以在 web.config 中设置多个连接参数,并可以在运行时进行实际连接。
要实际发生连接,您需要定义
例如,此处 SQLDataSource 设置为连接到 ConnectionStrings.MyNorthwind
或在第二个示例中,它们显式创建 SqlConnection。这里的连接字符串将从 web.config 中获取。
这是在 web.config 中找到的连接字符串的示例
The connection parameters entered in the web.config file are just parameters. No actual connections are made. In fact, multiple connection parameters can be made in the web.config and the actual connection can be made at runtime.
To actually make a connection happen, you need to define
For instance, here the SQLDataSource is set to connect to ConnectionStrings.MyNorthwind
or in this second example, where they explicitely create an SqlConnection. The connectionstring here would be taken from the web.config.
Here is an example of a connection string to be found in your web.config