cannot access sql server after publishing site in iis7 - c#

I am created a website using visual studio 2010. On the time of the development of website I am able to access the database.. but after publishing the site using IIS7..i was unable to access the database..the exception occured during that time was "the connection is in the closed state".in IIS7 When I changed the application pool identity to localsystem, it worked.
Data base is installed in the same machine. and server is SQL SERVER 2008 R2

you can only access database with sql authentication after publishing it.

In IIS7 the application pool by default runs under the IIS AppPool\[Application Pool Name]
That means, if your app pool is called "MyWebSite", then you must give the following user permissions to use your database: IIS AppPool\MyWebSite
It won't be in any list, so you'll have to type it as above when adding it.
I suggest making it database owner if you're in doubt about which permissions to give it. If it works, you can always refine the permissions later

Related

ASP.NET SqlException when publishing in IIS Web Server

I've just start working on an ASP.NET Web Application in Visual Studio 2015 and SQL Server 2014. Somewhere, I need to connect to my local database and I use this:
ConnStr = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=my_db;Integrated Security=True";
It works fine when I run the application through VS in my browser. I can access the db and do whatever I want to do. But, when I publish my application in IIS Web Server, and then I open it in browser, it still works OK until I have to access the db. At that moment it throws SqlException:
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. .
Maybe it is a server name problem and I should use an IP and a Port instead of that name, but I'm not sure because I don't have experience on this.
You are correct: You should use server name and/or IP in your connection string.
Using something like "local" or "localhost" means that your code is not portable. Another option would be to store your connection strings in two separate config files - one for your local copy (for development and troubleshooting) and one for your server (for portability). I have two config files in my solutions:
Web.config
WebServer.config
Then, when I deploy to the server, I just delete Web.config and rename WebServer.config to Web.config. It's totally portable and you'll never have connection string troubles again!
Also noteworthy: you're not including credentials in your connection string, which means that you're using windows authentication when connecting to SQL server. When debugging through visual studio, the application will run as you - and if you have the needed permissions, it will work. However, when running in IIS, it won't be running as you (at least, it SHOULDN'T be) - so you could run into issues there, as well.
EDIT
This link might be useful for you: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
Your connection string should look like this:
Data Source=192.168.1.10,1433;Initial Catalog=YourDatabaseName;User ID=SqlUserName;Password=SqlPassword;Connection Timeout=60; Max Pool Size=3000;
(you can set max pool size and connection timeout to whatever you want - or omit them entirely.
Also, regarding your windows issues - you need to make sure that the windows account IIS is using has permissions to traverse your network and reach your SQL Server instance. That said, I suggest that you use a SQL account instead of windows authentication.
Since you are using integrated security in connection string you will have to modify the Identity of App pool under which your application is running.
The Identity will be your windows username and password.
To change the username and password you need to go the advance settings of the app pool and process model you can see identity where you can add your windows credentials
Hope this helps
There are two problems in you connection string:
"Integrated Security" means you are using the native windows system for authentication. Similar to opening SSMS on the database using your Windows password. IIS is now trying to connect to the database, and connection string is telling to use the process that IIS is running under.
You can create a non-windows user in SQL Server and put the credentials into the connection string. Or you can grant the IIS user DB privileges. Or you can a lot different things here, but theses are the easiest to get you moving.
THe second problem in the connection string is the data source. Is there SQL Server on you local machine? If so that's why it's not working. Try to run your app in VS but against the remote SQL Server. That should be your next step.
The problem was that I thought that SQL Server was installed automatically with VS or at least with SQL Server Management Studio. BUT NO. So, as far as I understand, till know I have not worked with a real SQL Server. When I checked SQL Server Configuration Manager there were nothing running at SQL Server Services and so I realized that I was missing something.
Then, I installed SQL Server Express and build my db there. Now it is working fine even when I publish it. The connection string is
Data Source=.\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass
It can also be:
Data Source=localhost\\SQLEXPRESS;Initial Catalog=my_db;User ID=username;Password=pass

Job cannot access database after installed as Windows service

I can successfully run my jobs (that need access to an Sql Server database) when I run the Quartz.NET server in dev environment (Visual studio). But when I install the project as a Windows service, I get a login failed error to the database in my job. I have also allowed my service to interact with the desktop.
What am I not setting correctly?
You should check the Windows account your service is running under. Most probably it hasn't got the privileges to connect to your SQL Server, or to access the database. Basically, you have two choices:
Configure your service to run under an account that has a corresponding login in your SQL Server instance.
Add the account as a login in your SQL Server instance.

SQL Server database is readonly error

I am trying to upgrade one of our web application from Windows Server 2003 to a new box with Windows Server 2012. The application allows us to create multiple instances of another application as virtual directories within a website. Its more like we create an instance of a web application on run time for every signup which is customer specific. Each instance consist of SQL server MDF file that it attaches to SQL server.
The problem I am having is that MDF file is from SQL 2008 and system fails to upgrade it to 2012. If I upgrade it manually then system complains that database is in readonly mode. I have already set application pool identity to Network Service account and I believe it should have sufficient rights to attach/read/write the database. I am unable to reproduce the problem on local system where I am using Windows 8 and SQL 2012. Any help would be appreciated.
Failed to update database "some.MDF" because the database is read-only.
Network service has not part in readonly database configuration.It could depend from login role.Have you check via ssms if login user has been bind to the database and if its has the right permission.
Take a look to the image below:
if not you need to setup right configuration for your customer and so on.
The only way to get it working was by allow Network Service full control permissions using NTFS folder permission dialog.

Unable to connect to SQL Server 2008 in .Net

I am working on a C# web service.
I have deployed a web service which is working perfectly on my local system but when I try to run the same service on my virtual dedicated server it is returning an error.
This is my connection string
Data Source = <serverinstance>\\SQLEXPRESS; Initial Catalog = DomainTable; User ID= <serverinstance>\\admin; Password = <Windows_Login_Password>
Before this I was using this connection string
Data Source=<serverinstance>\\SQLEXPRESS; Initial Catalog=DomainTable; Integrated Security=SSPI";
None of them is working for me. please help its really really important
Data Source=serverinstance\SQLEXPRESS;
-----------------------------------------^ only one slash here
Also make sure that the remote server can see whatever you've referenced as <serverinstance>. You may have this as localhost but your web server does not consider your machine to be localhost. It may also not be able to reach it by name, particularly if it is in a different domain.
If you are trying to use Windows Authentication, there is no way on earth you should be using:
User ID = serverinstance\admin; Password = Windows_Login_Password
Wow, never hard-code a Windows password anywhere. If you want to use SQL authentication, then set up . Otherwise you should be using this instead of User ID / Password:
Persist Security Info = true;
And ensuring that the IIS user has been granted access to your SQL Server.
Try just;
"data source=.\\SQLEXPRESS;Integrated Security=SSPI;initial catalog=DomainTable"
I believe the user name / password are generally only required for remote connections, and you must set them up for the specified DB. The above connection string should get you going on the running instance of sqlexpress on the local machine if you have not made login credentials required.
Edit
If you are using IIS to host, here is a chunk from a recent article I did for deploying and setting up App pools to work with SQL Express 2008. Even if you are not using IIS, you will still need to do something like this to get your hosting environment set up correctly with SQL Server.
Step 1 Create a DB Logon for the IIS USER
In the security section in MS SQL SERVER, need to right click users and your IIS APPPOOL(IIS APPPOOL\apppoolname, created in Section one, Step 2).Check out dp.'s answer here for more step by steps - Add IIS 7 AppPool Identities as SQL Server Logons
Step 2 Setting Permissions for the IIS USER on the db
Right click on the database name, and click properties. In permissions section add your APPPOOL user that you just created, be sure to give him the Execute \Select permissions needed .
Note: Website1 needs Execute, Select, Delete, Insert permissions as well. Website2 is also using this app pool which it shouldnt be because we dont not need to alow the user to have that kind of functionality from within the trimdynamics application. We could seperate the app pools so that our trim dynamics IIS USRS is only getting Execute and Select permissions.
If this does not help, it is hard to tell whats really going on without concrete knowledge of your hosting structure, as there are lot's of gotch-ya's when deploying from development to production environment :(

Can someone explain difference in security credentials when using Visual Studio development server VS. IIS

Can anyone answer this question?
I have just switched to using IIS on an application I am working on and I am now running into problems with my database. I am using sqlexpress and entity framework and now I have switched to using IIS my application is being refused permission to drop/create my db.
Visual Development server will be running under the context of the logged in user, which will probably have administrator access.
IIS will be running under the context of the AppPool which will be default probably be set to NetworkService. You need to grant appropriate access to the local Network Service account in SQL Server to give it permission to drop and recreate databases.
Your app pool is likely under NETWORKSERVICE.
Just give rights to your DB for this account.
Or change the app pool account.

Categories

Resources