Connection String for SQL Server Instance - c#

I have SQL server 2000, and it have 2 other sql instances as well. Main sever has no issues, hereafter I am talking about those two instances,
They share,
Same machine
Same IP
Same Instance Name
Same Port
I too wonder how it has done.
Using query analyzer Once connect to,
MyServer\Invent (it connects to first instance)
MyServer\Invent,1433 (it connects to second instance)
(Yeah, it happens. I am 100% sure on that because I have separate DBs inside them)
Now it comes to problem,
With C# SqlConnection, I am unable to connect to first one, but can connect to second.
1. Data Source=MyServer\Invent;Persist Security Info=True;Initial Catalog=TEST;uid=admin;pwd=123
2. Data Source=MyServer\Invent,1433;Persist Security Info=True;Initial Catalog=TEST;uid=admin;pwd=123
Once I used 1st Connection String it hangs for a while and gets following error message...
"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)"
Searched on net for this, many says installing such is possible. And one connection should use named pipes (tcp/445)
Any idea how to resolve this ?
Any possible Connection String format ?
Anyway of making Connection String for named pipes ?

I think your issue is that the one server is using a different port. So it's using TCP but a different port and you'll need to figure out what port is it using and then add that port to your connection string like you're doing with the other instance (1433).
you SQL Server configuration manager.
Navigate to the SQL Server Network Configuration Node and expand it.
Locate the instance in the children nodes
Now on the right hand side you'll see Protocols (Shared Memory, Named Pipes, TCP etc.)
Double Click on TCP.
In the dialog that pops up, switch to the IP Address tab.
Scroll all the way down to the "IP All" section
What does it say for TCP Port?

I think maybe your first instance is not actually listening on 1433, even though that is usually the default.
I believe it's possible to change the default port with the SQL Server Configuration utility. Check what the default port is, and assuming it's not 1433, try specifying it in the first connection string.
Here's some info about how to check which port your instance is listening on.
Edit: Ok, the first instance is listening on a named pipe. Maybe server=np:MyServer\Invent; will work, as per this page.

Related

.NET Core 1.1 application on Mac and SQL Server connection string

I'm having problems in connecting a .NET Core 1.1 application to a remote SQL Server, but only on my Mac because on Windows everything works well.
Said that it's not my application and I'm only trying to make it working, this is my connection string on appsettings.Development.json:
"ConnectionStrings": {
"MesDatabase": "Data Source=x.x.x.x\\BLABLA,49331; Initial Catalog=DBNAME; Uid=sa; Pwd=blablabla"
}
This is the error I get opening the connection:
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: TCP
Provider, error: 25 - Connection string is not valid)
I can't access the server, any idea? I'm almost sure that it's a stupid error on connection string but I cannot find it.
I also tried to create a .udl file on Windows, editing it with correct connection data and taking the resulting connection string opening it with notepad. Nothing, always "connection string is not valid" on Mac.
As Jeroen Monstert said:
It makes no sense to specify both an instance name and a port number.
Either specify the instance name, and then let the SQL Server Browser
service resolve the port for you, or specify the port, but then leave
out the instance name.
My problem was due to the fact that in the connection string I had both the instance name and the port, in combination with a "probably bug" on .NET Core 1.1 because behaviour was different between Mac and Windows. In my case SQL Server Browser was down on the server so I needed to specify the port, and for this reason I had to remove the instance name from the connection string.

Azure Hybrid Connection string

I have successfully created an Hybrid Connection between Azure and an on-premise resource. I see "Connected" in both the Azure Portal and Hybrid Connection Manager.
When I run my .NET code local in Visual Studio with the connection string "AppServer://MyPC:5162/AzureOeApi" everything runs fine but when I run it in Azure, I get a connection error (it can't see the on-prem end-point).
Do I need to change my connection string when running in Azure to force it to go through the Hybrid Connection or will it be handled automatically?
No need to change connection string.
We had similar issue - connection was "Connected", but no requests whould go through. You need to use fully qualified domain name in configuration of hybrid connection.
Please also read this question - similar question
I've been struggling with this problem for a whole day and finally I had a breakthrough: in my original connection string I was referring to [DB_SERVER]\[INSTANCE], having correctly configured the DB instance to listen to a given TCP port [PORT].
With this setup the Hybrid Connection was 'Connected' but I was getting the exception:
ExceptionMessage: The underlying provider failed on Open. -InnerExceptionMessage: 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)
It turns out that the connection string needs do explicitly refer to the port number (instead of the DB instance) using the syntax [DB_SERVER],[PORT] (mind the comma!).
Unfortunately Microsoft does not document this subtle detail....

web service - SQL connection string

I've built an .asmx web service which retrieves informations from a local sql server 2014 database.
Everything is working fine on localhost, but after publishing the web service to Azure i get the error:
An unhandled exception of type 'System.Web.Services.Protocols.SoapException'
occurred in System.Web.Services.dll
Additional information: Server was unable to process request. ---> 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)
SQL Server is configured to allow remote connections.
My connection string is like:
string con2 = #"Data Source=OfficePc\MSSQLSERVER2014;Initial Catalog=Database;Persist Security Info=True;User ID=Admin;Password=123456";
Is the error the result of something missing from the connection string, or am i missing some configurations changes?
As Paul mentioned in a comment under the question, your connection string is pointing to a local database resource (presumably on your dev machine). Even though you configured your local database server to support remote connections, the address OfficePc\MSSQLSERVER2014 isn't addressable, as that does not equate to a machine address (IP address).
Your app would need to connect to your database via an accessible IP address (which might require you to do some port-forwarding on your local network, or open ports on your firewall).
Alternatively, you can migrate your database to Azure (either with SQL Server in a VM or with the SQL Database service).
Keep in mind: If you are accessing a local (on-premises) database server from Azure, there will be latency added, as well as some outbound bandwidth costs.
It looks like SQL Server instance is not running or not accessible. Try connecting to the same database using SSMS and if you get the same error then the instance is not running.
Mostly the error occurs when the Database server was not found. Recheck if the server name (Data Source) is mentioned correctly. If you manually generated the connection string use .uld file to generate connection string.
To auto generate connection string using .udl file:
Create a sampe.txt file.
Rename it as sample.udl file.
Then double click on it, It will show you window entitled 'Data Link Properties'.
Configure the connection there.
Then Test the connection using test connection button.
Then open the file with notepad. It will show you the exact connection string.
For further reference check : MSDN

SQL Error : error: 40 - Could not open a connection to SQL Server

I Get this error message whenever I tried to up my aspx page.
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
whic is connected in this connection string
SqlConnection conn = new SqlConnection("Data Source=192.168.xxx.xxx;Initial Catalog=DBSample;User ID=dev;Password=pass;Integrated Security=SSPI;"))
The weird thing is that the server that I'm connecting has already hosting some aspx page. I don't knnow if there's missing in my connectiong string Thanks. and I know the server that I'm connecting to is already allowed remote connection since it's already hosted some aspx websites. :(
Thanks!
Do you need to add an instance name to your connection string? Do you have the SQLBrowser service running on the target machine, or do you have to specify a port for the instance?
You also get that very same error when the database doesn't exist at the location that you are trying to connect to. Have tried looking at the connection strings of the aspx pages that are successfully connecting?
edited: Specifying Integrated Security=SSPI means you will be using Windows authentication to login to the database. What user is your aspx page running as (check your app pool)? Does it have the rights to log in to the database? This could also explain why it works on one server but not another.
Are you trying to connect to a hosted SQL Server over TCP/IP?
The reason I ask is some firewalls block traffic over Port 1433.
If not then it is simply a case of validating the connection string details and ensuring the SQL Server Engine is actually running...
Have you EVER been able to connect to this database from the PC you are currently attempting this connection on?

SQL Server connection issue

I am using VSTS 2008 + .Net 3.5 + C# + SQL Server 2008 Enterprise on Windows Server 2003. I am using the following connection string, and labtest1 is the local machine name and I connect from local machine using ADO.Net. Then it always fail with connection error. But when I change in the connection string from "labtest1" to ".", connection has no issue with the same ADO.Net client code. Any ideas what is wrong?
Data Source=labtest1;Initial Catalog=CustomerDB;Trusted_Connection=true;Asynchronous Processing=true
Here is the detailed error message I got,
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Looks like a Sql Server configuration issue to me : have you tried to tune protocols in Sql Server Network configuration ? Named Pipes or TCP/IP should be enabled.
When you use "." or "(local)" it connects to the default instance on your PC, perhaps the SQL Server was installed with instances, in which case you have to specify the instance name in the connection string in the format "SERVER\INSTANCE_NAME".
In SQL Management studio execute this query to see your full server\instance name
select ##servername
I have seen this issue previously with ZoneAlarms blocking the connection (on the machine trying to connect to the SQL server). I would spend some time investigating this area around firewalls etc.
Hope this helps
Do you have the Named Pipes network protocol enabled in the network config? (In the SQL Server Configuration Manager - sql server 2005, that's what I have, might be different in 2008 - you should be able to verify this setting)
Try to ping by the computer name: ping labtest1. if it does not find the server, then try ping labtest1.mydomainname.com with the domain name. If this works, then you just need to add/fix the DSN aliases in the domain controller or just re-login to the machine.
One other thins - and may not be relevant - but you've not specified the security model, at least not in the string provided as a sample.
I would expect to see: Integrated Security=True (given that its the local machine) in the connection string.
The other thing that may be relevant is - as has already been mentioned - the protocols, I'd look to make sure that TCP/IP is enabled.

Categories