开发者

Opening SQL Connection from IIS7?

I created an ASP.NET website with Visual Studio 2008 sp1 and in the code I use SQL connection to reach a data base in my SQL Server 2008. It all wiorks great but then I wanted to deploy the website in IIS7 (Windows 7) which was hell to discover how to d开发者_如何学Pythono it- (I added a virtual directory and converted it to an application and only then it didn't show me the error that I was getting before I copied it to inetpub\wwwroot and converted it to an application). After doing it I tried to run it from the IIS7 but it just keeps showing me an error that it gets in the command sqlconnection.open() which works great when I run the website from the Visual Studio. How do I resolve this?

I am using the following connection string: "Data SourceEDI-PC\SQLEXPRESS;Initial Catalog=SyncMaster;Integrated Security=True"

And my IIS7 is on the same computer as the SQL Server, so what exextly do I have to do to make it work?


Your connection string probably looks like this...

<add name="ConnectionString" 
     connectionString="Data Source=servername;
                      Initial Catalog=dbname;
                      Integrated Security=True"
     providerName="System.Data.SqlClient" />

Which means it is using the windows account IIS runs under, NETWORK SERVICE, to connect. When you run from Visual Studio it uses your windows account which has access to the SQL server.

Create a sql user to use as a service account and specify that info in the connection string. This way it will try to connect using the same account that you created in both Visual Studio and IIS.

ie.

<add name="ConnectionString" 
     connectionString="Data Source=servername;
                       Initial Catalog=dbname;
                       Persist Security Info=True;
                       User ID=sqluser;
                       Password=sqruserpassword"
     providerName="System.Data.SqlClient" />


When running in IIS7 (or any other IIS for that matter) the connection to sql server is made as the user that runs the application pool in iis. In IIS7 this is usually NETWORK SERVICE. So if you are using integrated security, you need to grant this account access to the database. If your SQL server instance is running on another machine, say SQLSERVER01, and your web app is running on your machine, YOURMACHINE, then the NETWORK SERVICE account on your machine will be called YOURMACHINE$ as seen by SQLSERVER01.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜