SQL Connection with public IP, named instance and port number - c#

I have had difficulty creating a connection string in c# that will connect to a remote SQL server using a public IP, named instance and a port number (other than 1433). Anyone know how to do that?

Try this, replacing 666 with the port number you want to use, 190.190.200.100 with the IP address you want, etc.:
Data Source=190.190.200.100\MyInstance,666;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

Using the servername tcp:<public IP>,<port>, as documented in SqlConnection.ConnectionString:
The name or network address of the
instance of SQL Server to which to
connect. The port number can be
specified after the server name:
server=tcp:servername, portnumber
When specifying a local instance,
always use (local). To force a
protocol, add one of the following
prefixes:
np:(local), tcp:(local), lpc:(local)
Data Source must use the TCP format or
the Named Pipes format.
TCP format is as follows:
tcp:<host name>\<instance name>
tcp:<host name>,<TCP/IP port number>
If you use the tcp:<host name>\<isntance name> the SQL Browser service connection is required (port 1433) therefore is better to use the later format, with explicit port name:
Data Source=tcp:1.2.3.4,1234;User Id=...; Password=...

This site has never failed me.
And i am gonna state the obvious here, but it is generally a bad idea to expose your sql server on the internet.. (unless you are using VPN)

connectionString="Database=pub;Server=192.168.1.1\INSTANCE,1746;Trusted_Connection=yes;"
Or you could use username/password instead of trusted connection.

Related

I want to create a shared database c# windows form application?

This application that i am trying to create, whose "instances/copies" will be installed on multiple PCs of the client and are able to access the same database from another PC of the same client that has SQL server running all the time through the same network(i.e: they have the same router and static IP of server etc).
I had two ways to do this:
To make applications use the same database through synchronization.
To have multiple localdbs that then share the changes a database server.
By following 1st way,
I tried setting up the Connection String like:
SqlConnection con = new SqlConnection("Data Source =[serverPcIP,Port];Integrated Security=True;Connect Timeout=30");
SqlConnection con = new SqlConnection("SERVER=[serverPcIP]; Port=[portno];Integrated Security=True;Connect Timeout=30");
In Server - Security Settings:
I have allowed "SQL Server and Windows Authentication mode"
In Sql Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLEXPRESS => TCP/IP = Enabled; under IP Addresses -> IP1 -> TCP Port = 1433 (port that i used in my connection-string)
In advanced Firewall Setting:
Have enabled the Incoming & Outgoing rules for port 1433.
The exception that i get:
Additional information: 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: 0 - The wait operation timed out.)
Can someone please tell me what configurations (either in connection-String or in SQL server itself) am i missing?
Your connection string lacks the instance name e.g. data source=<hostname or ip>\SQLEXPRESS. If your database listens on the default port 1433 you don't need to pass that with the connection string
If you setup a SQL server, you can choose between the default SQL Server instance or the named instance. For SQL Express the default is to install a named instance with the name SQLEXPRESS. If you don't use the default instance and install a named instance instead, you have to pass it with the connection string.
The default SQL Server instance ist just a named instance with the well known name MSSQLSERVER. So if you installed the database instance as the default instance you can connect with <host>[,port] or <host>\MSSQLSERVER[,port]
based on your screenshot, under IP1, the ip address is active but not enabled.
also, ip address that starts with 169.x.x.x normally would mean you have set up this network card to use DHCP, but the network card failed to have ip address from the DHCP server.
do you mind try to use static ip address?
Consider following steps:
Since you mention the port number using configuration manager, the service required restart. please restart the service if not done so.
Seems you are able to connect via SSMS, once service restarted verify SQL Error log via SSMS -> Managemnt -> SQL Server Logs, you must find a message as follows:
Server is listening on [ 'any' <ipv4> 1433].
Further details, follow these steps. Also, this..

ODBC Driver ignores port parameters for SQL Anywhere 10

I'm trying to connect to a SQL Anywhere 10 database using an ODBC connection in C#. This connects fine with all the default parameters, but whenever I try to specify a port or IP address, it looks like it's completely ignored
My connection string is:
Driver={SQL Anywhere 10};uid=username;pwd=password;
Which works when connecting to the first database listed. I want to connect to the second database running on the computer, which is on a different port. I have tried:
Driver={SQL Anywhere 10};uid=username;pwd=password;port=1234;
Driver={SQL Anywhere 10};uid=username;pwd=password;LINKs=tcpip(host=192.168.1.1:1234)
Driver={SQL Anywhere 10};uid=username;pwd=password;DataSource=localhost:1234;
None of these have changed the connection at all, or even failed to connect
What is the correct string to use to specify port?
Or what other method can I use other than a System.Data.ODBC.ODBCConnection to connect where I can specify a port?
It looks like you're just making up connection attributes to try in the string...
Try looking at documentation for the driver you're using --
Driver={SQL Anywhere 10};UID=<user name>;DatabaseName=<database name>;EngineName=<database engine name>;AutoStop=YES;Integrated=NO;EncryptedPassword=3ff6c3114e;Debug=NO;DisableMultiRowFetch=NO;CommLinks='TCPIP{HOST=<database server name>;PORT=<port number for remote connections>}';Compress=NO
Most of those attributes are optional, so here's a reduced string that may work for you --
Driver={SQL Anywhere 10};UID=username;PWD=password;CommLinks='TCPIP{HOST=localhost;PORT=1234}'

Connect to Azure Redis Cache via HTTP

I'm using "ServiceStack.Redis" to connect to Redis and it works correctly on my development machine.
Basically, I open the connection via this line:
client = new RedisClient(host);
Where host, on my development machine, is "localhost".
Now, I'd like to upload my application to Azure, so I created a cache in Azure and I'm trying to connect to it by passing the following connection string:
XXX.redis.cache.windows.net,ssl=false,password=YYY
The creation of the "RedisClient" seems to work but when I try to perform an operation (the first one to be executed being client.RemoveByPattern("...")), I get the following error:
Exception Details: System.Net.Sockets.SocketException: No such host is
known
Note that I allowed the cache to be connected to via HTTP, so normally, the port 6379 is unblocked and accessible.
All the example I found over Internet are using "ConnectionMultiplexer" but this class does not seem to be found in the NuGet package "ServiceStack.Redis".
What am I doing wrong?
I was having the same(similar?) issue connecting to Azure Redis with ServiceStack, in the end it was working out the correct syntax for the connection that worked for me. XXX.redis.cache.windows.net?ssl=true
Found some help here https://github.com/ServiceStack/ServiceStack.Redis, but to quote the connection strings section had examples;
"Redis Connection strings have been expanded to support the more versatile URI format which is now able to capture most of Redis Client settings in a single connection string (akin to DB Connection strings).
Redis Connection Strings supports multiple URI-like formats, from a simple hostname or IP Address and port pair to a fully-qualified URI with multiple options specified on the QueryString."
Some examples of supported formats:
localhost
127.0.0.1:6379
redis://localhost:6379
password#localhost:6379
clientid:password#localhost:6379
redis://clientid:password#localhost:6380?ssl=true&db=1
NOTE: I used the final example here but without the redis:// bit as I found this was not needed in Azure.

Data source for connection string

What connection string should I use if SSMS connects to it using simply machine name, without instance name?
I mean it connects using the following string: PCName
I used to connect using PCName/SQLExpress. I cannot set correct connection string in my app in order to connect app to database on this machine.
How can I check what data source I should use? I've checked in Sql Server configuration that server instance named as SQLEXPRESS.
So I tried data source as:
.\SQLEXPRESS
PCName\SQLEXPRESS
.
I'm trying to connect to a service-based database, located in my app folder. So I'm using the following connection string:
data source=PCName;attachdbfilename=|DataDirectory|\spareparts.mdf;integrated security=true;user instance=true;multipleactiveresultsets=true;App=EntityFramework;
If SSMS connects via PCName then your application should be able to use Data Source=PCName. However it depends on whether your application is on the same machine as SSMS or not. If on a different machine it might not be able to connect for a variety of reasons. We can't speculate what the problem might be if all you do to describe the issue is "It won't connect" - what does that mean? Do you get an error message? If so, what is it? Make sure:
SQL Browser service is started
TCP/IP is enabled
Add Network=DBMSSOCN; to the connection string
You've also tried the IP address in addition to PCName
Firewall isn't blocking the SQL Server port

I can't make a connection to SQL Server with my local IP in C#

I have one problem with C#
I connect Data Source=BARIS;Initial Catalog=SurucuOtomasyon;User ID=sa;Password=1234
But I don't connect database when i write mylocal ip to Data Source
Data Source=192.168.1.3;Initial Catalog=SurucuOtomasyon;User ID=sa;Password=1234
Thanks for helping
an answer
Open your SQL Configuration Manager and allow to TCP/IP connections.
Set dynamic port blank to disable it (i don't remember here well)
The IP gives the machine, you need a port, too:
192.168.1.3:1433 (the port 1433 is a standart)
another link about connectionstrings
connection string with DataSource=192.168.2.3 is enough. You have to change configurationn.
Right configuration will run your code. (i've tested it with a project, its all about configuration of your sql. See my comment below)

Categories

Resources