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..
Related
I am currently implementing a UWP program for an existing database. I took this article as a basis.
https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases
When running, I get an exception:
System.Data.SqlClient.SqlException" в System.Data.SqlClient.dll
Exception:
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)
This seemed strange to me, since a WPF application with the same connection string works fine. Next, I decided to install a database instance on my computer and export some of the data to a local database. To my surprise, after replacing the connection string from:
Data Source=sqlserver\sqlexpress
to
Data Source=.\sqlexpress
UWP program read this from the database on my computer.
I also read this article to make sure that the remote database is configured correctly. https://www.lansweeper.com/knowledgebase/a-network-related-or-instance-specific-error-occurred/
As I said, WPF programs work with this database perfectly. What are the ways to check why the UWP doesn't want to communicate with the database?
to make it clear whether the remote server is available in principle, I apply the output of the TNC command:
tnc sqlserver -p 1433 -I detailed
ComputerName : sqlserver
RemoteAddress : 192.168.10.11
RemotePort : 1433
NameResolutionResults : 192.168.10.11
MatchingIPsecRules :
NetworkIsolationContext : Private Network
InterfaceAlias : Ethernet
SourceAddress : 192.168.10.20
NetRoute (NextHop) : 0.0.0.0
TcpTestSucceeded : True
Connection string look like:
private string connectionString = #"Data Source=sqlserver\sqlexpress;Initial Catalog=TestCatalog;User ID=*;Password=*";
The same connection string is used in VS. VS shows the contents of the table without problems. I can also delete or add new lines in the VS editor.
I'm talking about a remote database. In VS server Explorer I have a Data Connection where I am connected to a remote database. I can safely edit remote database data in VS (add, delete and update). I see the connection string and originally used it as the connection string in UWP app.
You have already taken a look at this microsoft link here. Long story short, have you followed the proposal below?
Trouble connecting to your database?
In most cases, some aspect of the SQL Server configuration needs to be changed. If you're able to connect to your database from another type of desktop application such as a Windows Forms or WPF application, ensure that you've enabled TCP/IP for SQL Server. You can do that in the Computer Management console.
Check to see if you have followed the rest of the solution properly. You might need to enable the tcp port from the window firewall.
Go to firewall -> Inbound Rules -> Add TCP:1433 in the rule.
Also since the error is about the connection string, try to use only the server name/ ip/ or hostname, without the server instance.
You can also add the port number and see if this works for you. When you add it, sql server ignores the instance name (as different instances require different ports to work, so it can work as a unique identifier).
Edit: How to create a valid connection string:
Look at this answer here: How to get the connection String from a database
I am developing a program with C# and WPF. I want the data to be stored in an SQL Server database. I made a connection string with the instance name in my PC, and that worked. But when I want to connect through the Internet with an IP address, I get some errors:
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. (TCP Provider,
error 0 - No connection could be made because the target machine
actively refused it.)
I enabled TCP/IP, allowed remote connection in SQL Configuration Manager, opened a port of 1433 in my firewall, but still I am getting this error.
My connection string is this:
String connString = #"Network Library=dbmssocn;
Network Address=127.0.0.1,1433;
Integrated security=SSPI;
Initial Catalog=db";
SqlConnection conn = new SqlConnection(connString);
conn.Open( );
Where is my mistake?
There is very slight error in your connection string and honestly I can't blame you for that as it is very weird way in which SQL Sever is behaving. I'm not sure if this error lies in connection provider side or SQL Server instance side. Name of Network Library that your application is using to connect to the SQL Server using dbmssocn(Win32 Winsock TCP/IP) should always be mentioned in capital letters. Though I didn't see any relevant MSDN documentation from MS to support my statement but it actually worked when I did so. Here is the connection string that you should be using to fix the error.
String connString = #"Network Library=DBMSSOCN;
Network Address=127.0.0.1,1433;
Integrated security=SSPI;
Initial Catalog=db";
Seriously, I got freaked out in reproducing your issue as instead of copying the connection string from your question I copied it from some other blog :). But all is well that ends well. I've also assumed that a database named "db" actually exists on the default (NOT named instance) instance of the sql server you are connecting to when typing this answer. If changing the casing of network library name in connection string doesn't help then double check that database "db" must exist for a sql server default instance to which you are connecting to. In case you are using a named instance of sql server in your installation then that instance name should also come in the connection string.
Configure your SQL Server to allow TCP/IP connections. Go to SQL Server Configuration Manager -> network then protocols for your SQL Server named instance -> TCP/IP.
See this image!
I created a sql server instance called 'abcd' on my computer but when I try to connect to it I receive a strange error. I am able to connect to my others instances with the same code. Is the instance name causing this error?
Here is my code :
string connectionString = "Server=192.168.1.185\\abcd;" + "Database=test;" + "User ID=sa;" + "Password=bob;";
IDbConnection dbcon;
using (dbcon = new SqlConnection(connectionString))
{
dbcon.Open(); // <-- crashes here
Console.WriteLine("Connected");
}
Crash
Mono does not support names pipes or shared memory for connecting to SQL Server. Please enable the TCP/IP protocol.
What's wierd is that I'm able to connect to my other instances. The only thing that changes is the instance name.
I'm using Xamarin.iOS 9.4.0.0, Xamarin Studio 5.10.1, Mono Framework MDK 4.2.1.102
In SQL server the enabled protocols are configured on a per-instance basis, so the most likely explanation is that the named instance you're trying to connect to doesn't have the TCP/IP protocol enabled.
You can check this by opening the Sql Server Configuration Manager tool and going to SQL Server Network Configuration -> Protocols for {instance name}. Make sure that the TCP/IP protocol is enabled.
I have two .net solutions.
They both have this code:
var connectionString = "Server = ServerName; Database = DatabaseName; Trusted_Connection=True;"
var connection = new SqlConnection(connectionString);
connection.Open();
connection.Close();
And they both use complitely the same connectionString. And they connect to MSSQL Server;
And in the first solution connection.Open() succeeded and in the second one it failed.
Error message : Named Pipes Provider, error: 40 - Could not open a connection to SQL Server
And every time I create new solution it fails. So this connection only works in one specific solution. And also, my teammates tested this and didn't have such issues.
Can you help with hints why it can be so?
Check that your SQL server has the TCP/IP protocol enabled and if it is using dynamic ports, ensure that the SQL Browser service is running.
Make sure that any firewall software (windows firewall etc.) has an exception added for the particular exe (the sqlservr.exe file gets copied into the instance folder, which will be in C:\Program Files\Microsoft SQL Server\{instance name}\MSSQL\Binn, where instance name might look something like MSSQL11.SQLEXPRESS) or TCP port 1433 or UDP port 1434 (TCP 1433 for the SQL service itself assuming standard settings, UPD 1434 for the SQL Browser Service).
Check that the user account used to access the server (it should also have sufficient permissions and the login should be enabled), in this case it will be whatever windows account you are logged in as or whatever account your IIS application pool is running as because you are using Trusted_Connection=True.
Also check that you have an up-to-date SQL Client on your machine, you can get the latest version for windows here.
Open Sql Server Configuration Manager and make sure that the Named Pipes option is enabled for your Sql server instance.
Sorry for disturbing you. My problem was my inattentiveness. I created soluiton on another network machine.
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