Problems defining the port for remote connection in SQL Server - c#

I use the following connection string to establish a remote connection in SQL Server:
"Data Source=" + _databaseIP + "," + _databasePort + ";Network
Library=DBMSSOCN; Initial Catalog=SI_CONFIG; uid=sa;pwd=Password1!"
where the _databaseIP and _databasePort are input by the user.
However, although the IP is fixed on the remote machine, the port eventually changes.
To discover the actual port where the server is listening I look for the ERRORLOG file ('c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Log\ERRORLOG') and search for the line 'Server is listening on [ 'any' 49167' which tells me the port, however, this seems to change randomly after some days or weeks.
I wanted the port to be fixed, or to use another way to connect through the server, in a way which I did not demand the port anymore from the user. Any suggestion is appreciated. Thanks.

It sounds like your SQL Server is configured to use a dynamic port. From the link:
Named instances of the Database Engine and SQL Server Compact are
configured for dynamic ports. This means they select an available port
when the SQL Server service is started. When you are connecting to a
named instance through a firewall, configure the Database Engine to
listen on a specific port, so that the appropriate port can be opened
in the firewall.
To configure your SQL instance to use a specific port instead, open up the Sql Server Configuration Manager and navigate to Sql Server Network Configuration -> Protocols for (instance name). Right-click TCP/IP, go to Properties -> IP Addresses tab, and clear all the entries for TCP Dynamic Ports. Set the TCP Port value for each IP address on which you want to make connections available. SQL will need to be restarted after you save your changes.

Related

Why my visual studio 2015 SQL Server doesn't connect with Azure SQL Database?

I have a SQL Server running on Microsoft Azure. I want to connect it to My Project in Visual Studio 2015 Community. I have added my IP address in firewall on server. I have also made my port 1433 open for sql. But when I try to connect to database this error shows up. Error Message in SQL Server
It appears that this is a two step process and you either missed the VM configuration or the Azure configuration. Based on what you said I believe it is the Azure configuration that may have been missed. This blog post appears to highlight the necessary things that need to be done to connect.
https://blogs.msdn.microsoft.com/ggaurav/2014/01/08/connect-to-sql-database-on-azure-iaas-from-ssms/
Below is an excerpt:
The two settings which you have to make sure you are checking are :
Endpoint is configured for the port on which SQL server is listening ( generally 1433) on the VM machine.
Ports are opened on the VM machine. Even though as per the documentation Cloud adaptor takes care of opening the firewall ports,
it doesn’t work for the normal connections which you are trying to
make. Just type in ” wf.msc” and create a rule for both outgoing and
incoming for TCP port 1433.
As soon as you are done with these two, you would be able to connect
to the SQL server on the VM machine.
According to your description, I guess your SQL database is a Azure service, not install SQL on Azure VM.
If I understand it correctly, we should check SQL database firewall settings and confirm the name of your database.
The error code means, this error could occur because either the
firewall on the server has refused the connection or the server is not
configured to accept remote connections.
We can via Azure Portal to check the firewall settings:
Make sure the Allow access to Azure Services is ON,
And make sure your client IP address have added to the Client IP address list.
Also we should confirm the name of your database, make sure we are connect to the right SQL database.
I have also made my port 1433 open for sql. But when I try to connect to database this error shows up.
You also need to enable 1433 port in Network Security Group which related to your VM. Steps below are for your reference.
Find the Network Security Group name which related to your VM in Netowrk interfaces panel.
Open this NSG, click [Add] button in the Inbound security rules panel
Add a rule as following.
After that, we could access the database hosted in VM using following server name format.
Server name format.
[IP address/dns name],1433
For example,
13.81.50.123,1433

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;

Unable to connect in SQL Management Studio

My application is throwing error from a client machine of not able to connect to the database. So as a test I use SQL Management Studio 2008 on that same client machine to try and connect to the database and I found out that if I use IP\InstanceName then I can connect but if I use ServerComputerName\InstanceName then I can't connect. I have verified the following:
The server has the same gateway and subnet mask as the client.
I can ping the server using either its IP address or its computer name.
No Firewall on either server or client machine.
Verify Server SQL browser service is running.
Verify Server is set to allow remote connection.
Verify Server TCP/IP is enabled via SQL SErver Configration Manager.
I can connect from a different client machine using ServerComputerName\InstanceName
Both server and client are using static IP setup with no DNS server under IPV4 properties.
Tracert from client to server shows only one hop, that is directly to the server machine IP.
I can remote access the client machine from the server machine or viceversa using host name.
I then go to the server machine and connect using LNKLAB8\Xmark (LNKLAB8 is the host name and Xmark is my instance name) and I connected successfully. However, when I run the command SELECT ##SERVERNAME, the result I got back is LNKLABARIAL8\XMARK
This baffled me, because if I right click on the database properties as per the screenshot below, it shows LNKLAB8/Xmark .. am I going crazy or something weird going on here? after taking at the steps I've taken, do you have any suggestion of how I can get the client machine to connect to the database using the server host name instead of the server IP address?
I've kind of hit the wall on this issue, I'm unsure if I am facing a network related issue or it is a database setting issue.
Edit: When I tried to connect using host name to the server DB I would get the following:
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)
Try add servers IP and Name to c:\WINDOWS\system32\drivers\etc\hosts
1) Make sure you've enabled TCP/IP (Configuration manager), and restarted MSSQL
<= it sounds like you've done this.
2) Make sure you can "ping" by hostname and by IP
<= It sounds like you've done this, too
3) Make sure MSSQL Browser is running
4) If you're using Windows authentication (vs. SQL Mixed), then make sure you can authenticate to the remote server:
net use \\remote-server /user:<name> <password>
5) Failing all else, you might want to consider reinstalling MSSQL-related components on either/both client or server (for example, MDAC)
Links:
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/4d2ce34a-508f-4a1d-8828-8806ccc12f77/sql-server-2005-cant-connect-remotely-using-host-name?forum=sqlsetupandupgrade
http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx
https://serverfault.com/questions/384700/error-connection-to-sql-server-using-hostname-instance-or-fqdn-instance-ip
'Hope that helps!
I noticed this:
8: Both server and client are using static IP setup with no DNS server under IPV4 properties.
How is name resolution happening, then? WINS? NetBIOS? Sql Server may not like that. Try checking that you have a working DNS server with the appropriate entry for your Sql Server system, and things may work better.

SQL Server c# connection

So Im running a Sql Server 2012 in a virtual machine on my computer and I want to be able to connect on it with the SqlConnection on Visual Studio, but I have no idea what to put on the servername parameter.
I dont know if I have to put the vm ipaddress or the machine address "WIN-NIJ53etc\SQLEXPRESS"
UPDATED:
FOR MIGRATING BETWEEN SQL CE AND SQL SERVER: Please take a look at this instruction here.
You should follow these steps:
1- Set the virtual machine to have a physical IP address.
For configuring network on Microsoft Virtual PC: http://support.microsoft.com/kb/833134
2- Be sure that you can see the VM with a static IP address. (ping is a good idea for checking that, but don't forget to turn off the firewall on the target machine, I mean the machine you wanna ping. There is no need for the server to see the client, so check that your client (in this case your VM) can ping the server IP address like "ping 192.168.10.1 -t")
3- Use Configuration Manager (All Programs\Microsoft SQL Server\Configuration Tools) and set SQL port and visibility over network.
Reference: http://msdn.microsoft.com/en-us/library/ms189083(v=sql.105).aspx
4- Restart the SQL server.
5- Be sure that "SQL Server Browser" service is started. (incase you wanna use the instance name to access the SQL server)
6- Config the firewall on the host machine (machine that has SQL engine) and add the port you specified in the Configuration Manager to TCP Inbound rules.
Take a look at here: http://www.tavislovell.com/post/How-to-configure-Windows-Firewall-to-allow-access-to-SQL-Server.aspx
BUT be advised that you should set the port specified in the Configuration Manager (default is 1433 but sometimes it's dynamic and you should set it again)
7- After you set the port and network visibility, you should change the "Server" value in the connection string to this format "IPAddress, PORT" like "192.168.10.50, 1433" (When you specify the port, there is no need to set the instance name like this "192.168.10.50\SQLEXPRESS")
Hope it helps
Ahh the joys of SQL Server Installation setup.
Make sure you have:
Network communication between the VM and the client computer (ping will work)
The SQL Server Service configured to start automatically or that it is started
The service configured to use TCP (if you don't want to use Named Pipes)
Either the SQL Server Browser enabled and started or that a static port is configured (1433 or similar)
The firewall rules configured to allow 1433 (or the port you have chosen) and 1434 (if you are using browser)
The connection string using the proper format Computer Name\Instance (to use browser) or Computer Name,port (if you are using a static port
There is a good tutorial on MSDN that may prove helpful. With specific regard to the Server Name/IP question, it doesn't matter so long as the client computer can resolve it. If you are testing, "IP,Port" is the most reliable (E.g. 10.10.10.100,1433) as it does not depend on NetBIOS, DNS nor the SQL Browser service.

SQL Server's port is blocked

Developed a Windows application that remotely accesses an SQL Server online. However, the client's ISP blocks port 1433. What are the other options to connect to SQL Server from the windows app?
SQL Server should have a network utility where you can configure the port to be used. Contact your client's ISP to see which ports are open (they may even be able to open up 1433 for you) then configure the server to use that port.
In my install, you select: "Start > All programs > Microsoft SQL Server > Server Network utility", select "Enabled Protocols" then "TCP/IP" and "Properties".
From there you can set the default port to a more accessible one.
You could change the port number that SQL Server runs under, to use a port that is actually open at the ISP.
Since 1433 is the default port for SQL Server, it is a common target for hackers. Some ISPs pick an arbitrary port number to obfuscate this. You may need to find what port number that is from them.
Another option is to use a WCF Web Service hosted in IIS and have your windows app call this service.

Categories

Resources