Cannot connect to SQL Server Express database from an UWP app - c#

I have a UWP app that needs to connect to a SQL Server Express database. On my dev box, I don't have any issues.
On the client PC, I installed SQL Server Express, created the database, setup the users and logins.
When I run the app, and try to connect to the database, I get an 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: TCP Provider, error: 40 - Could not open a connection to SQL Server)
The code I'm using to connect is:
...
using (SqlConnection connection = new SqlConnection(ConnectionStringTextBox.Text.Trim()))
{
try
{
connection.Open();
MessageBox.Show("Connected successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
...
Using this same code on a desktop app, on the client machine, the connection succeeds.
I have set up the UWP app to use version 1809 of Windows 10.
The client and my dev box are both version 2004.
Package.appxmanifest file has the following capabilities checked:
Enterprise Authentication
Internet (Client & Server)
Internet (Client)
Private Networks (Client & Server)
Windows Firewall has the correct ports open. In SSMS, I have enabled allowing remote connections, enabled TCP.
I don't know what I'm doing wrong or what is going wrong. This same UWP app works great on my dev box.
Any advice is appreciated.

If you're trying to connect to a SQL Server instance on the same machine it won't work because UWP applications are sandboxed and cannot connect to localhost over TCP/UDP - while connections to remote machines will succeed. I think this is stupid, but it's By Design.
As a workaround, you can configure SQL Server to use Shared Memory or Named Pipes and those approaches should work.
Update:
I forgot that there is now a Capability you can add to your AppX manifest to enable local-loopback connections (only for TCP, it doesn't work for UDP).

You can try this example on how to connect to sql server in uwp app, and try to do the same steps on your client pc, hope it works:
https://learn.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases#first-set-up-your-solution

I tried everything I could find online, change sql server firewall settings, sql server tcp/ip settings, change sql server browser firewall settings, none of them worked for me.
I did read someone saying about turn on loopback capabilities in UWP, but I didn't see that option for the targeted window version I had. In the end, I finally got it working by running the following command
checknetisolation loopbackexempt -a -n=YourAppPackageName

Related

Run C# application without SQL Server Management Studio installed on client machine

I have created a simple login application using the C#.net. I have some basic sql database(I use SSMS). There is name, email and password. I want my application to be usable from more devices (so I can't use localdb). Everything works fine on my pc but when I move to another, where is not installed ssms it shows error 26 - Error Locating Server/Instance Specified. I don't know how exactly to run my C# application without SQL Server Management Studio installed on client machine
I have researched for a weeks so I enable tcp/ip (from sql server manager), also add new inbound rules for tcp(1433) and udp(1434) ports in windows firewall. After that I allow remote connection and add sql server in windows firewall. Also I've tried to install SSMS on client machine but nothing
here are my conncetion strings
this one I tried to solve my problem
Data Source=xxxxxxxxxxxx;Initial Catalog=xxxxxxxxxxx;Integrated Security=SSPI
the other one is my main connection string
Data Source=xxxxxxxxxxxxxxxxxxxxxxx;Initial Catalog=xxxxxxxxxxxxxx;Integrated
Integrated Security = True
The application works fine on my computer, I've installed Visual Studio 2017 and SQL Server Management studio, but when I move to another computer it doesn't work.
Thanks to everyone in advance!!
SSMS is just a Database Management Tool, the actual Database would by SQLExpress or similar. So on your remote host you need to install the latter and set it up correctly in you application to use to appropiate connectionstring to it.
Schema compare your tables from VS and your ready to go.
https://www.microsoft.com/en-us/sql-server/sql-server-editions-express
https://www.mssqltips.com/sqlservertip/5528/installing-sql-server-2017-express/
You dont need to install sql or sql managemment studio in pc where you running your application, but just as your error said "Error Locating Server/Instance Specified", the server where you want to connect, is not accesible, thats means you should open your ports in pc where your sql server is hosted, and also, put your public ip following with that port in your server name in conectionstring, like Data Source=yourserver_IP:8076; tip: make sure your app pc and server pc are in same network.
The reason it works on the same machine is most likely is is that when you go through another machine the connection has to go through a firewall that is blocking access to SQL Server. The default port that SQL Server uses is 1433 and unless you change it on the host machine when you connect from another machine that is the port that is use to connect and you don't have to specify the port. When you connect on the same machine that SQL Server is installed the firewall does not prevent you from connecting. Port 1433 is used for the default instance (The first installed instance on the machine).

Can't Connect to Azure SQL Server from Visual Studio Enterprise 2015

Okay, I am at a complete loss. I have set up a SQL server & database through Azure and have been trying to connect in Visual Studio. The database is listed under Azure in the Server Explorer in Visual Studio, but, when I try to open it in SQL Server Explorer, I get the following error when I try to connect:
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 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) (Microsoft SQL Server, Error: 10060)
I do not know what else to do. I have opened up port 1433 on my local firewall and allowed access from my computer's IP through the SQL Server's firewall. I temporarily opened it to all ip addresses, which did not work. The SQL Azure says the database is online. Is there an extra step or fix that I missed?
Connecting to your SQL Database would require you to allow it to be accessible from your IP first.
Please check this MS doc about how to do so.
By default Azure SQL DBs are exposed over the internet just with a simple username/password. Connecting from outside of Azure requires white-listing your IP.
The very simple way to do so is to connect using SQL Management Studio, which detects that and allows you to add you IP address automatically to the firewall rules. Have a look here to see how this works.
Update to include comments:
If none of this works, your problem probably has to do with from where you are trying to access it.
If you are behind a company firewall, there could be rules prohibiting connecting to certain port ranges. Try to connect from a wifi hotspot over your mobile phone is the quickest way to figure out.

How to connect two or more client desktop with one main desktop(consider as Server Desktop) and share database to each other using C#?

this is dot Net C# program,database is SQL2005.
Problem is that it is working on Main desktop that is consider as SERVER desktop. but when I try to use its database in its client desktop then it gives me 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: SQL Network Interfaces, error: 28 - Server doesn't support requested protocol)
My Connection String
"Network Library=DBMSSOCN;Data Source=.\\SQLEXPRESS;Initial Catalog=hms;
MultipleActiveResultSets=True;Integrated Security=True;"
other con string is
"Data Source=192.123.12.11;Initial Catalog=hms;MultipleActiveResultSets=True;
Integrated Security=True;"
when I add the network library then it gives me error. otherwise working fine. I also replaced etwork library to network. but not working.
I also configured MS-SQL server configuration, enabled TCP/IP and named pipes, port :1433 ,
i installed SQL server in Server desktop and copy past my all supported files in D drive. and I am using ip address of server desktop.
In Client desktop I just copy pasted all supported application files in D drive. but did not install any sql software.
What happens when we do not specify network library in connnection string?
it was only connection string problem i solved it.
return "server=Myserver; database=mydb;uid=sa;password=mypwd;MultipleActiveResultSets=True;";
i only installed MS-sql server in my SERVER machine. i shared a drive for all client machine. where i installed my app.
no extra configuration is needed .

Remote connection to SQL Server 2005. "Login failed for user" or at least SEND information to other computers through WinForm app?

I have a program that installed a server (SQL Server 2005 it seems). I have patiently read quite an amount of documentation on debugging a remote connection to SQL Server 2005 but I cannot reach an end point without your help. Also I'm running out of time..
Anyway here are the details:
Program that needs to access SQL: WinForm application
Firewalls status: Antivir Firewall and Router Firewall off (for the moment)
IPv4 Address: 192.168.1.2
SQL Server 2005 with Management Studio Express,
Code I use to access the server:
sqlConnection = new SqlConnection("user id=GEN; password=asd; Network Address =
192.168.1.2\\SERVER_INSTANCE; Trusted_Connection = yes; "database=DATABASE_NAME;
connection timeout=30");
try {sqlConnection.Open();}
catch(Exception c) {aTimer.Stop(); MessageBox.Show(c.ToString());}
Posting an image below with useful info from the server program:
EDIT: Since there doesn't seem to be many solutions, I'm also open to suggestions if I can SEND information from the WinForm on the Server, to other computers in the LAN
EDIT Attempting to upgrade the SQL Server version. (Seems that my 2005 version is not SP4.. so says the 2012 version when upgrading)
Login failed for user, means that it is actually able to connect to SQL Server, but it is being denied access. Usually this is because of an incorrect username/password combination or a lack of required permissions.
Check SQL Server permissions for the desired user (If you are using windows auth).
Furthermore, SQL Server logins are not the same as Windows users. When it asks for server authentication you must login using a user created in SQL Server, not a Windows user.

SqlConnection stuck on Named Pipes

I'm seeing the following error when attempting to open a DB connection from within my C# application. I realize this error has probably shown up on 100's of questions before. However, in this scenario the error is only showing up on C# apps running on my specific desktop PC. I've scoured the internet via Google and within this website, but I can't find a solution.
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)"
WHY THIS SCENARIO IS UNIQUE:
I am able to connect to the SQL server from SQL Server Management Studio (SSMS). This application works on another computer and is able to establish a connection to the SQL server from there. So this error scenario is specific to C# applications running on my specific desktop PC. Other apps work (SSMS). Other PC's work.
When I "sniff the wire" using WireShark, the trace shows me that my app is trying to connect via NamedPipes (i.e. \Server\IPC$). I can't seem to force it to use TCP/IP.
THINGS I'VE TRIED:
Re-installed.NET Framework
Re-installed Visual Studio (C# - Express version 2010)
Created an alias within cliconfg.exe.
Is there something I missed?
Here are Connection Strings I've tried...
Data Source=<servername>;Initial Catalog=HIE;Integrated Security=true
Server=tcp:10.240.11.81;Integrated Security=SSPI; database=HIE
Data Source=10.240.11.81,1433;Network Library=DBMSSOCN;Initial Catalog=HIE;User ID=<SqlUserIdThatISetUpAsSysAdmin>;Password=<password>
Here's the code snippet:
_conn = new SqlConnection(); // _conn declared globally
_conn.ConnectionString = <the connection string above>;
_conn.Open();
The most likely scenario is that Windows Firewall is block the SQL Server communication. From MSDN (an article about named pipes, but relevant nonetheless):
Microsoft Windows XP Service Pack 2 enables Windows Firewall, which
closes port 445 by default. Because Microsoft SQL Server communicates
over port 445, you must reopen the port if SQL Server is configured to
listen for incoming client connections using named pipes. For
information on configuring a firewall, see "How to: Configure a
Firewall for SQL Server Access" in SQL Server Books Online or review
your firewall documentation.
Another scenario is that the client configuration on the local machine is not configured correctly. From the run prompt, you can execute cliconfg (the SQL Server Client Configuration Utility) to see the enabled protocols and the aliases that are used for the protocols.
Either redefine your Data Source as suggested by usr's comment, or configure your SQL Server to allow TCP connections (Why would you not allow TCP connections anyway?).
Here is the link to enable TCP on your SQL Server (The accepted answer is where you want to look at):
Enable remote connections for SQL Server Express 2012
Are you running your code off of local disk or a network drive? I encountered a very similar problem where my network drive was not trusted in .Net and that was why my connections failed.

Categories