Unable to connect to sql server 2008 with c# - c#

I have installed SQL Server on one PC and I have created a c# program on another computer to connect to the server. However, when I run my program, I get an exception saying that login failed for user 'testing'.
This is the code used to connect:
SqlConnection con = new SqlConnection("Data Source=IP,PORT;Network Library=DBMSSOCN;Initial Catalog=Stockinfo;uid=testing;pwd=testing");
I have created a login in the server with userid and password as testing. As for the server authentication, it has been set to SQL Server and Windows authentication. I have also created an exception for firewall. What have I done wrong?
Thanks in advance for your help!
EDIT 1: I am able to access the server with SQL Server authentication using the same userid and password on the computer running the server.

Do you have Sql Server Management Studio. If yes, then you can open it and right click the instance in question, go to Properties -> Connections -> Check "Allow remote connections to this server".
You might also have to enable TCP/IP Protocol to be enabled thru Configuration Manager.
Does it return this 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)
You can check this link: enable remote connections in SQL Server 2008
Another reason is to check another computer's firewall if port sqlbrowser (or port 1433 is open) was added on its firewall.
UPDATE 1
user testing simply doesn't exist or you have supplied incorrect password

Login failed for user 'testing'.
That usually means that the user doesn't have access to that database.

Related

My app doesn't use SQL Server Express but it is looking for a database in App_Data -- why?

My application uses SSRS (on SQL Server 2017 on a server) and IIS. It runs fine in my IDE (VS 2017 or VS 2015) but when I put it on the server I get an error page.
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)
No connection string to SQL Server Express.
Firstly make sure that your SQL server is running and SQL Server
browser service is running.
Make sure SQL Server is configured to allow remote connections.
Examine the SQL Server error log for messages confirming that SQL is
listening on the expected network interfaces and ports.
Test server connectivity with PING from the client machine.
Test port connectivity using TELNET or PowerShell to the server and
port from the client machine.
For example
a. TELNET 1433
b. PowerShell: 1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",$)) "server listening on TCP port $" }
Check firewall settings.
Also check your web.config file.
You could also refer below article:
“A network-related or instance-specific error occurred”
Regards,
Jalpa.

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.

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.

Network related error while deploying a window application & used database to another pc

My window application works fine on my computer but when I install it on another computer a network-related error occurred...
I'm using sql server 2005, I have store the backup of the database, when i run it, it flags an error.
I'm using visual studio 10, C#,sql sever 2005
Error is..
"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)""
When this happens it means that the program cannot connect to SQL Server with information you provided.
If for example you are trying to connect my-server\sqlexpress, usually you have one of this issues:
There is no computer named my-server in your network!
SQL Server is not installed on my-server.
Your client computer cannot reach my-server from the network. You can check this using ping command.
The installed SQL Server instance name on my-server is not sqlexpress (case-insensitive).
You have not enabled TCP/IP protocol for your installation instance of SQL Server in "Sql Server Configuration Manager".
You have not restarted SQL Server service after enabling TCP/IP.
Your SQL Server instance is not default instance, and SQL Server Browser service is not running on the server.
Server's firewall is preventing the client to access the server. Try turning off the firewall on the server (only to test this case).
Consider this a checklist, and start from the beginning. Check them one by one. And I hope that you find the problem.

Cannot connect to attached SQL database

I'm using linq to sql with Microsoft SQL Server Database File (SqlClient) as a data source.
The db file was created by Visual Studio 2010 while adding the connection (Database Explorer -> Add Connection).
Using this method I was unable to create a db with SQL auth, so I'm using Windows Authentication.
EDIT: Note that it's not a connection to a database, it's a connection to a database file.
1) Is it possible to create a database file with sql authentication? (I assume it is the root of the problem).
2) Connection works fine on my computer under different acocunts. However, running the release on other machine results in an exception being thrown.
Exception details:
System.Data.SqlClient.SqlException (0x80131904): 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)
I've tried to set the db path manually, but I guess it's not the issue.
What am I doing wrong?
EDIT2: the connection string I'm using:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\OperatorResults.mdf;Integrated Security=True;Connect Timeout=10;User Instance=True"
First of all, how was SQL Server installed? With Windows Authentication only or Mixed Mode authentication. If it was just Windows Authentication that explains why you were unable to use SQL Server authentication. If mixed mode authentication is enabled then you need an account that has sysadmin or CREATE DATABASE privileges.
As for your second question, did you modify connection string to match the connection configuration of another machine? If not, try doing that. From the exception above it seems that your connection string is connecting to a wrong server or the server instance is not running.
You want Microsoft SQL Server Express
It allows a file based connection.

Categories