IIS / SQL Server connection error - c#

I am doing a sample code and I got stucked in a really strange behavior, I am trying to connect to a SQL Server using this connection string
"Data Source=" + ServerName + "." + DomainName + ";Initial Catalog="+DBName+";Integrated Security = false;Persist Security Info=false;User ID=sa;Password=" & Mypassword & ";Connection Timeout=90"
Apparently I am not using Windows Integrated Authentication or (SSPI), when I use this on the web application I get this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Just to note I created a .Net application that uses the same connection string and is running from the computer where the IIS resides and it works fine.
Here are the steps I have done:
I checked the SQL client configuration I opened it and enabled both of the protocol tcp and Named pipes.
I did impersonation on the IIS and used windows authentication and made sure that the IIS is working with the same credentials as mine.
I used Wireshark to see wut is the difference between the two connections and I couldn't understand the output however it seems that from the IIS it always tries to use windows authentication although I have clearly made the string such that it doesn't use windows authentication basically wut happens is that some negotiation in the beginning of the connection that seems the same from both application "not_defined_in_RFC4178#please_ignore" and then the IIS goes always to NTLMSSP noting the my user doesn't have access to the database except through user name and password so the negotiation fails with IIS while it succeeds normally from the application.
My question is how may I fix this, I kind of assume that there is some configuration that mandates that any SQL client from IIS uses windows authentication but I can't know where or how to disable it
I note I am not using web.config configuration for the SQL Server connection string and this issue happens with IIS7.

Here are the first things I would check:
Verify the Application pool user has rights to access network resources
Since you stated it works when you are logged into the machine, try changing the application pool user to the user with which you log into the machine.
Check if you have impersonation enabled in your web.config. If you do, make sure that user can access network resources.
Try changing the connection string to use an IP address (as a test). If that fixes the issue, maybe it is some sort of DNS problem.
Make sure you include a ,1433 at the end of the IP Address
Set Network Library=DBMSSOCN in your connection string. (See this article on ConnectionStrings.com for more info.)

If you have typed exact connection string as above, please check changing & around Password in connection string to +.
The connection string written above is not valid string at all since string can be concatenate another string using + not using & in C# as I know.

Aside from what the others have indicated, the only other time I recall the error message specifically with "Named Pipes" reference was based on how SQL-Server is setup.
To confirm, I went into my version of SQL Server Configuration Manager. Then, looked at the
"SQL Native Client 11.0 Configuration" (just in case, did for both 32-bit and 64-bit) and opened to show "Client Protocols". Within that it shows options for Shared Memory, TCP/IP and Named Pipes. Make sure your "Named Pipes" protocol is enabled.

You mention that SQL Server is on another machine. You should make sure the SQL Server Browser service on that machine running.
You will find it in the Windows Services management console (as well as other locations).
In addition, you will have to make sure UDP port 1434 is open in the firewall.

Check this first:
Make sure that you sql server accept remote connections
Sql Server Instance ->Properities->Connections->Allow remore connections.
following this step in second:
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Check the values of ServerName, DomainName, DBName & MyPassword.
Are they correct ??
How they populate?

Let us check into the steps to resolve this error.
1) SQL Server should be up and running.
2) Enable TCP/IP in SQL Server Configuration
3) Open Port in Windows Firewall
4) Enable Remote Connection
5) Enable SQL Server Browser Service
6) Create exception of sqlbrowser.exe in Firewall
7) Recreate Alias
Source here
Hope it helps

may be the SQL server was not recognized by client system
because the error occurred due to sql server network not available and also it may be some error for network failure

Related

Login to SQL server on an application server with single user

I am making an application in C# that is supposed to work As CLIENT/SERVER architecture the application accesses to the server only to connect to the database. When I started developing the application I found that I had to make a connection string to access the database and then I made the following $"Server = {server}; Database = {dataBase}; Trusted_Connection = True;" which is working now and went to do a test on a machine where the local windows user does not have access to the SQL Server and hence I noticed that only
user at the time of creating the SQL instance in the Mixed Mode option
is one that have access to the SQL server if you are logged in to windows and are using the application. With little experience in developing such applications, I decided to come here to find help in how we can overcome this kind of challenges in desktop applications for .NET. Thanks in advance!
Windows SQL server uses the currently logged in windows user [At the server] to authenticate a connection request. For remote computers, this may not be effective. Rather, it's better to use the username and password authentication method for remote hosts. Kindly make sure that your server is set to mixed mode authentication and follow the following steps.
Create a new user who uses username and password to login. Visit [https://www.supremainc.com/en/node/618] as suggested by Aqib
From the windows firewall, allow incoming connections to access SQL server from outside computers by setting inbound rules in your firewall to allow connections through SQL server port. You may need to check the port your SQL server is listening to though the default port is 1433.
Visit [https://docs.syskit.com/spdockit/v7/faq/troubleshooting-sql-server-connection/inbound-traffic/] for instructions on how to setup the firewall
Make sure that the remote computer is connected to the server. You can do a simple ping to the server. You can use ip-address or server-name / url for checking the connection. [https://iihelp.iinet.net.au/How_to_run_a_ping_test]
Make sure your SQL server is started and run your application on the remote computer.
Thank you
If you want to use a SQL user, you first need to create a SQL user with the appropriate permissions (usually datareader and datawriter) on the database
How to create a SQL user on a database
Then instead of using Trusted Connection, you need to explicitely define the username and password in the connection string like this:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;
You can use SQL connection strings website as a reference for the various options

Failed to connect to azure sql database with connection string [duplicate]

I'm very frustrated. I have a website running on Visual Web Developer 2008 Express with my local database, everything works great. I also have the same web site running on a production server. Everything was working great but tonight I did a "reset" on production.
I deleted a couple of table, re-created them and inserted data. Everything was ok at this time.
I deleted ALL the files via the FTP.
I used the module called "Copy website" in visual studio and copy the site to the website via FTP.
When I log on my website, here is the error I got:
Server Error in '/' Application.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: 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. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Nothing has changed related to SQL connection, this is OLD code that I always used.
My website is completely paralysed because of this and I feel sick inside because I feel there is nothing I can do.
Can anyone help me please?
Your connection string was probably overriden when you copied your new website version on the server. Please check the connection string in web.config and see if it is valid.
If you are connecting from Windows Machine A to Windows Machine B (server with SQL Server installed) and are getting this error, you need to do the following:
On Machine B:
Turn on the Windows service called "SQL Server Browser" and start the service
In Windows Firewall:
enable incoming port UDP 1434 (in case SQL Server Management Studio on machine A is connecting or a program on machine A is connecting)
enable incoming port TCP 1433 (in case there is a telnet connection)
In SQL Server Configuration Manager:
enable TCP/IP protocol for port 1433
I've resolved the issue. It was due to the SQL browser service.
Solution to such problem is one among below -
Check the spelling of the SQL Server instance name that is specified in the connection string.
Use the SQL Server Surface Area Configuration tool to enable SQL Server to accept remote connections over the TCP or named pipes protocols. For more information about the SQL Server Surface Area Configuration Tool, see Surface Area Configuration for Services and Connections.
Make sure that you have configured the firewall on the server instance of SQL Server to open ports for SQL Server and the SQL Server Browser port (UDP 1434).
Make sure that the SQL Server Browser service is started on the server.
link - http://www.microsoft.com/products/ee/transform.aspx?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=-1
check forthe followings :
Make sure your database engine is configured to accept remote connections
• Start > All Programs > SQL Server 2005 > Configuration Tools > SQL Server Surface Area Configuration
• Click on Surface Area Configuration for Services and Connections
• Select the instance that is having a problem > Database Engine > Remote Connections
• Enable local and remote connections
• Restart instance
Check the SQL Server service account
• If you are not using a domain account as a service account (for example if you are using NETWORK SERVICE), you may want to switch this first before proceeding
If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings in your ASweb P.NET application
• Usually the format needed to specify the database server is machinename\instancename
• Check your connection string as well
the cause is that SQL SERVER is stopped from services.msc
a solution for this problem could be starting SQL SERVER from services.msc
I recently had this problem and it ended up being a port issue. My production SQL Server was set up at to be port 1427 instead 1433.
Just change the connection string to be
...data source=MySQLServerName,1427;initial catalog=MyDBName...
Hope this helps anyone who might be seeing this same issue.
I had the same problem with SQL Server 2008 R2 and when I checked "SQL Server Configuration Manager" My SQL Server instance had Stopped. Right Clicking and Starting the Instance solved the issue.
we have to enable TCP/IP property in sql server configuration manager
If your server was working and suddenly started erroring out, your server/instance stopped and connection settings were changed somehow.
For SQL Server 2008 here is how you can fit this:
Goto Start > All Programs > SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services.
And here you'll see all the instances and their state.
The state of the instance you were trying to connect can be stopped here.
Double click on the instance and then click on connect.
It will connect and now go back and run your application, you will be able to connect with no error.
This solution assumes the error is not being caused by something wrong in your connection string.
In my case it was a very silly error. I was using a library to read the connection string out of a config file, and I forgot to double back slash.
For example I had:
localhost\sqlexpress which was read as localhostsqlexpress
when I should rather have had
localhost\\sqlexpress note the \
I had same problem regarding that i.e A network-related or instance-specific error occurred while establishing a connection to SQL Server.
I was using SQL Server 2005 (.\sqlexpress)` and worked fine but suddenly services stopped and gave me error.
I solved it like this,
Start -> Search Box - > Sql Configuration Manager -> SQL Server 2005 Services >and just Change Your SQL Server (SQLEXPRESS) State to Running by right clicking that service Sate.
I solved this issue by running the following command in an elevated command prompt as specified in this post.
net start mssqlserver
I had the same problem but found that it was because the password for my Service Account for the Database Engine had expired. The solution was to login to that account, fix this, then set the account so password never expires.
My remote sql server was expecting connection at a different port and not on the default 1443.
The solution was to add a "," after your sql server IP and then add your port like so 129.0.0.1,2995 (Check the " , " after server IP)
Please find the image for reference below.
I had the same error, and it turned out .NET created a database on localhost which I wasn't aware of. When I tried to get the site live it didn't create the database, so the code was pointing to a database that was non existent.
Hopefully this might help some other people out trying to troubleshoot.

Need help in SQL Server Remote Access

how to connect two laptops to share a database using windows form application? i mean i've installed mssql server 2008 in my laptop, i need to make windows form application so my friend can modify the database from his laptop, it's not in LAN , we have to use Internet as medium to connect, it'd be helpful if anyone can show me some directions like how connection strings should be. thanks :)
You should consider looking into this. If you search for Error 40 or Error 26 SQL Server. You will get lot of links for connecting SQL Server remotely both on LAN and WAN.
Take a look at this link as well. Screenshots are added here
There are multiple steps involved
Enable default login (sa) - not a good practice
Enable remote connection on sql server
Enable SqlBrowser service - either through Sql Server configuration manager or type services.msc on Run(Ctrl + R)
Add SqlBrowser.exe to firewall exception
Open TCP port 1433 and add it to firewall
Enable TCP, NamedPipe in Sql Server Configuration manager.
Check your public IP using any site like Seemyip.com
Give this IP to your friend along with sql server instance name
On remote machine - enter IP\ServerName with credentials for sa.
OP you just need a dynamic DNS Record (for free at dyndns.org) and then you can create a port forwarding on your Router/Firewall to your SQL server.
That way your SQL Server will be accessible over the internet. Just update your connection string with the dynamic DNS entry that you registered.
This link would be helpful to set the configuration.The link tells how to manipulate connection string according to scenarios.
Here is your:
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;

Can't open a connection to SQL Server 2005 from a pocketpc (windows mobile 6)

I'm trying to connect from a pocket pc app (windows mobile 6) to a SQL Server 2005, but no matter how hard I try it didn't work. I checked every possible configuration for enabling tcp on SQL Server, I tested many connection strings, but still have the problem when I attempt to open the connection, I'm sure it's not a network issue because I can ping on my pocket pc from the server machine without any problem and the firewall on my server machine is disabled : here is one of the connection strings I used :
Data Source=10.168.0.160,1433;Initial Catalog=pos;Trusted_connection=yes;user id=myuserid;password=mypassword
where pos is the name of my database
Thank you for your help
Well, you cannot have both a trusted connection and specify an explicit user name and password at the same time - it's either or.
EITHER you connect to your server with the trusted connection, e.g. your Windows credentials - then your connection string looks something like this:
Data Source=10.168.0.160,1433;Initial Catalog=pos;Integrated Security=SSPI;
OR alternatively, you use an explicit user name and password - but in that case, you CANNOT also use trusted connection / integrated security at the same time!
In this case, your connection string would be something like:
Data Source=10.168.0.160,1433;Initial Catalog=pos;user id=myuserid;password=mypassword
Check out the ConnectionStrings.com for loads of samples of valid connection strings for SQL Server
You can't use Integrated Windows Authentication from a mobile device because the device doesn't have any concept of a logged in user. You also have to make sure the server has remote connections turned on.
Here's a simple example of how to get it working.

Initial connection to SQL Server Connection Is Slow. Why?

I have run into a situation with a C# application installed at two sites where the initial connection to SQL Server is extremely slow. I wrote a test application to verify where the slowdown occurs and it is on the first SQLConnection.Open statement. It was taking about 41 seconds to establish a connection to the server via named pipes. We thought it might be a DNS issue but its just as slow using a TCP/IP connection. After the initial connection is made the connection is pooled and the application responds normally. Both the workstation and the server are decent machines running Windows 7 Pro, Core 2 Duo 3.16 Ghz with 4 gig of Ram. I did find the following article on a microsoft forum:
http://social.msdn.microsoft.com/Forums/en/windowscompatibility/thread/f295994c-5812-4e46-8ac9-f05471d4dd54
Turning off the LLMNR protocol did cut the initial connection time in about half to 21 seconds. However, this is still a long time to get an initial connection to SQL Server. The only things slightly different from our norm is that DNS in this case is done thru a router and not an actual server. This has occurred at only two places so far, others run with no issues. Any help would be appreciated.
Thank You,
Dennis
In front of the server on the connection string, add np:
This becomes Server=np:server\instance and forces the Named Pipes instead of the default of TCP.
I could of probably changed the priority to use Named Pipes before TCP... but I did not want to mess with that on the server.
I saw similar problem but not sure it's the same as yours. In my case, it's not just the C# program being slow to make the SQL connection. It's any tools connecting to SQL server also experiencing the slowness. Also, once the initial connection made to the SQL server, any subsequent connections are fine for a period of time.
The reason was that SQL server was using a number of managed assemblies. It's trying to verify the cerfiticates assigned to the assemblies. It's connecting the crl.microsoft.com. My SQL server didn't have the internet connection. So, it waits for the timeout.
The solution was to make my SQL server to have internet access or disable the CRL checking. You can go to the SQL server machine. Select Tools > Internet Options > Advanced. Check if the publisher's certificate revocation under the secuity node is checked or not. If it's checked, uncheck it.
I tried specifying the connection string with integrated security = false (meaning the user id and password are in the connection string) and encrypt = false (just be be 100% sure SSL encryption is not being used). These specifications did not seem to help and I could not get a connection explicitly using the TCP/IP network library (NetworkLibrary = "dbmssocn"). This could have to do with the server firewall and the port not being open. I switched back to named pipes and put the named pipe network library specification in the connection string this time (NetworkLibrary = "dbnmpntw"). After this change, the connection was made instantaneously.
Establishing an integrated security connection to SQL Server using the IP address (instead of a host name), will prevent Kerberos authentication being used. In this scenario check the connection between the SQL Server and the domain controller.
If you connect using the host name (not the IP Address) Kerbos is in play, in this case you need to check the client machine's connection to the domain controller.
We had the same problem, and it turned out that our remotely-hosted Active Directory server was to blame.
We created a site-local Active Directory server to replicate the remotely hosted AD master and then all of our slow SQL Server integrated security authentication performance issues went away.
I hope that helps.
Yeah when you're using integrated security, Active Directory can be the one to blame, also the overall network since it all depends on it. Another thing I could think of is the edition of SQL Server you're using.
Also, when SQL Server is not being used by long periods of time, it behaves similar to IIS, putting the worker process to sleep, so when you contact the server again, depending on the machine (we can see that these have desktop machines configurations), it will take some time while the worker process gets back to life and is ready to work.
You did check the obvious I take it? That UDP port 1434 is open on the firewall and the browser service is running .... would take about 40 sec to authenticate otherwise.
there are other methods of creating connections to SQL databases. Try finding a tutorial which uses the
sqlconnection myCon = new sqlconnection(details);
myCon.Open()
instead of creating an object to instance the connection.
I don't have a concrete answer but have you tried running SQL Profiler to see what is going on from SQL's point of view?
Also have you tried connecting to SQL using the same credentials as your connection?
On the other hand it may all be much lower level but I always do the easy to check stuff first.
Good Luck.
It sounds like either name resolution is taking a while or authentication is taking a while. After the initial resolution or authentication's occured, then details of endpoints are cached by the server so it doesn't need to perform the lookups again until caches expire.
As an experiment, try ping'ing the server from the client box -- if this takes a long time to resolve the hostname, then you've found the culprit: hostname lookups (DNS or NBNS). Another alternative is to use the host IP address rather than name. So if you've got a SQL Server named instance of bob on server sql2005-01, and this server has an IP of 192.168.200.12, then try connecting to 192.168.200.12\bob instead of sql2005-01\bob.
Authentication's more awkward to resolve, but you might be able to test it with runas on the SQL server box (e.g., runas /user:domain\user cmd to see if you can open up a command prompt as the user you're attempting to authenticate as.
It could easily be a problem with either the connection or the authentication, it is normal that the first connection takes longer as ADO.NET has connection pool to avoid that long connection time.
There are many factors that can influence the speed:
- TCP/IP configuration
- Routers on the server-side
- etc.
Still if you are facing the issue, please see blow resolution:
Root Cause
The issue which we were seeing on Win7 VDIs could be due to the Network hardware device connected with the machine. If TCP/IP scaling is not supported by the network device then the performance will be slow.
Solution
Disable auto tuning level of the TCP. Please follow below steps:
1) Open command Prompt with admin right (Run as Admin)
2) Type “netsh interface tcp set global autotuninglevel=disabled”
3) After running above command restart the machine.
For other information on this command, visit link “http://support.microsoft.com/kb/935400”
I had the same problem. After vigorous research on google and stackoverflow, i changed the client computer's hosts file (In windows,located at C:\Windows\System32\drivers\etc).I entered my host's ip address and server name in this file and Viola!. Things became superfast!
Like everybody in stackoverflow said, it was the computer looking up the servername's address in the DNS service and getting a timeout.
The steps i did follows.Give it a try if nothing else works.
HOW TO ADD AN ENTRY IN HOSTS FILE
1.Open the command prompt and ping your server,where the remote database is located. To do this enter the following command:
ping servername
Here my remote computer name was Juno. So i should ping like this.
ping Juno
This command will ping my server and returns ip address like this.
Pinging Juno [192.168.0.3] with 32 bytes of data:
As you can see the ip address of the server is between the brackets.
Copy the ip address.
2.Now Open the Hosts file with elevated Notepad(Run as administrator).
At the end of the hosts file,there will be some lines like these:
#localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
At the very bottom (here,after the localhost),type # and enter the ip address of the server we just obtained preceeded by server name.
So, the hosts file should look like this.
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
# 169.254.63.1 Juno
Now save the hosts file and restart the Client PC. (For me,it worked instantly without restart.)
Annd there you go!
For more info about editing your hosts file,click here
In my case the answer was:
Try everything with no result.
Remote Desktop to the (non-prod) SQL Server to double check settings.
Close down Visual Studio you left running after doing a quick SSIS job.
Go somewhere quiet and kick yourself.
Open Start -> Microsoft SQL Server 20XX ->SQL Server Configuration Manager
SQL Native Client 11.0 Configuration (32bit) -> Client Protocols -> (Ensure all items are marked as "Enabled")
SQL Server Network Configuration -> Protocols for MSSQLSERVER -> (Ensure only Shared Memory item is marked as "Enabled", others should be "Disabled")
SQL Native Client 11.0 Configuration -> Client Protocols -> (Ensure all items are marked as "Enabled")
Reboot computer
Ensure SQL Services are running and attempt to connect to local MSSQL
Connection should be much more rapid

Categories

Resources