用asp.net访问数据库

问题描述:

am使用vs2008用sqlserver开发asp.net应用程序.当我从vs2008运行该应用程序时,它运行良好.但是将其发布到我的本地iis文件夹(即C:\ inetpub \ wwwroot)之后,它无法访问数据库.

我应该怎么办?我必须更改我的连接字符串吗?这是一个正在使用的计算机:

am developing an asp.net application with sqlserver using vs2008. When i run the application from vs2008, it works fine. But after publishing it to my local iis folder (i.e. C:\inetpub\wwwroot), it could not access the database.

What should i do? Do i have to change my connection string?? This is the one am using:

SqlConnection conn = new SqlConnection("Data Source=salisu-pc\\sqlexpress;Initial Catalog=NNPC;Integrated Security=True;Pooling=True");



[OP的答案移至问题]
谢谢.生成此异常:

System.Data.SqlClient.SqlException:无法打开登录请求的数据库"NNPC".然后登录失败.
用户"IIS APPPOOL \ DefaultAppPool"登录失败



[OP''s Answer moved to question]
thanks. The generating this exception:

System.Data.SqlClient.SqlException: Cannot open database "NNPC" requested by login. Then login failed.
Login failed for user "IIS APPPOOL\DefaultAppPool"

基于连接字符串,我的第一个猜测是您的工作站正在阻止与SQL Server的通信.例如,如果使用的是SQL 2005,则使用外围设备配置从PC外部启用TCP/IP通信.还要检查Windows中的防火墙是否阻止了通信

一个不错的检查清单要经过:如何将SQL Server 2005配置为允许远程连接 [
Based on the connection string my first guess would be that your workstation is blocking the communication with the SQL Server. If this is for example SQL 2005, use the Surface Area COnfiguration to enable TCP/IP communication from outside your PC. Also check that the firewall in Windows doesn''t block the communication

One good checklist to go through: How to configure SQL Server 2005 to allow remote connections[^]

Also check that your ASP.NET service is run under an account that has an access to the SQL Server.


问题是由您使用的集成安全性设置引起的.当您在VS下运行应用程序时,该应用程序将在您的帐户下运行.我想您是管理员,因此您可以访问数据库.但是在服务器上,它在与应用程序池关联的帐户下运行.默认是网络服务".您可以将数据库上的访问权限授予该服务帐户,但最好创建一个专用帐户,该帐户在iis下运行您的应用程序,并在sql服务器下注册,并被授予适当的权限(不超过必要).
更新:由于该服务位于另一台计算机上,因此您无法授予该服务的权限.如果您在域中或使用SQL身份验证,请创建专用的域帐户.
The problem is generated by the integrated security setting you use. When you run your application under VS, it runs under your account. I suppose you are admin, thus you have access to the database. But on the server it runs under an account associated with the app pool. The defauls is "network services" You can grant access on the database to the service account, but better you create a dedicated account, that runs your app under iis, and is registered under the sql server, and granted proper rights (not more than necessary).
Update: you cannot grant rights to the service, since it is on a different machine. Create dedicated domain account if you are in a domain or use SQL authentication.