Connecting to a SQL Database - c#

I am using Visual Studio and trying to connect to the Northwind database. I use the following connection command...
Data Source=C:\\Program Files (x86)\\Microsoft SQL Server Compact Edition\\v3.5\\Samples\\Northwind.sdf;Initial Catalog=Northwind;Integrated Security=SSPI"
I have also tried
Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI
both cases 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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
I can connect to an online database but not on a remote one. I have checked the config manager and all services are running.
Any helps please

there are lots of collections of connection strings so you can get help from here...
http://www.connectionstrings.com/sql-server-2008

The Visual Studio has tools to connect to databases. Using Server Explorer, you can choose which SQL Server you want to connect to and then choose the database from a list. Once you have successfully connected to one, its properties mention the connection string to use. No need to guess.

Related

Can't connect local mdf database file

I build a simple CRUD app in C# and used server based database file (.mdf) in my Project. My connection string is like this:
public string connectionString { get; set; } = $"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"{AppDomain.CurrentDomain.BaseDirectory}Passwords.mdf\";Integrated Security=True";
In my computer application works fine just I want, but after send to my friend, he got error that can't connect or find SQL database. Error text 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
So we download SQL server express 2019 for him (I use 2017 by the way) and after that, still he get the same error. I dont understand what is the problem. Any succession?

Cannot connect to any remote database using SqlConnection

I cannot connect to a particular SQL Server 2008 database server from C#.
I am able to connect using SSMS, and run queries using SQLCMD, but when I try to connect from C# using the SqlConnection it fails to open the connection with a SqlException:
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)
Here is my code:
using (SqlConnection conn = new SqlConnection(#"Server=LDNPSM050000137\PLN000000014T;Initial Catalog=MiscData;Integrated Security=True;"))
{
//exception occurs on this line
conn.Open();
//use connection
conn.Close();
}
I get a similar response using ODBC:
string connectionString = #"Driver={SQL Server Native Client 10.0};Server=LDNPSM050000137\PLN000000014T;Database=MiscData;Trusted_Connection=yes;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
}
This fails with an OdbcExcpetion:
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
ERROR [HYT00] [Microsoft][SQL Server Native Client 10.0]Login timeout expired
ERROR [08001] [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
I have checked the server and instance name are correct, and the server is configured to allow remote connections because I can connect through SSMS. Does anybody have a suggestion of what the problem could be or how to resolve this?
Update:
In case this helps somebody diagnose the problem - This is specific to my machine/user account. My colleague can run the code fine from his machine. I am also able to connect to my local instance using a connectionstring with the appropriate changes to the Server and Initial Catalog.
The error occurs for all remote database servers.
I'm not sure if this is specific to my company's specific IT infrastructure - but the reason this was not working for me was because the project was saved in My Documents which stored on a network share. Apparently .NET will not let you connect to a remote database server when the executing code is located on a network share - it work fine when I copied the project to my local drive. I am using Windows 7 and Visual Studio 2012.
Network shares by default have partial trust, so things that work when a project is on your local drive, e.g. connecting to remote server, won't from network location.
You can give the network location full trust (https://msdn.microsoft.com/en-us/library/zdc263t0(VS.80).aspx) or move your project onto your local drive.
I think the problem is the Server value in your Connect string:
From MSDN
I think you need to do this:
Server=tcp:LDNPSM050000137\PLN000000014T
Server=np:LDNPSM050000137\PLN000000014T
Depending upon whether you want to use TCP or Named Pipes...

Remote access using C# application to database created using SQL Server 2014 Management Studio

I have a database called Library created in SQL Server 2014 under my locally created server instance. I am using that database as the datasource in my windows form application. It is working perfectly when on my computer, but when I run it on other machines, it stops with the error
Unhandled exception occurred...
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 connection to SQL Server)
I have started all services from configuration manager and have enabled tcp/ip under protocols for MSSQLSERVER. Is there any way I can accomplish this, or do I have to shift to local db?
Make sure that your connection string includes your remote machine name as part of the "Data Source", so something like :
connectionString="Data Source=machinename\SQLExpress;Initial Catalog=mydbname;Integrated Security=True"
note that this connection string will target a db name "mydbname", under the machine machinename which has a SQL Express instance.
if that didn't work let me see your connection string.

Connecting to a database

I'm trying to connect to a database but nothing I try works.
SqlConnection conn = new SqlConnection(#"Data Source=C:\Users\Gerard Foley\Desktop\Northwind.sdf");
conn.Open();
No matter what I try I just get the 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: 26 - Error Locating Server/Instance Specified)
I stole the connection string from Database Explorer -> Properties -> Connection String. What am I missing? I can get the tables to show up in a DataGridView fine (by dragging from Data Sources), but I want to use my own UI and queries. I just can't seem to figure this ADO thing out.
Using c# express 2010 and sql server express 2008.
for the proper connection string to use to connect to SQL Server have a look at:
http://connectionstrings.com
the connection string you are using now is strange, it should contain server name and database name, see link above for examples...
GOT IT. I should have been using Sql*Ce*Connection. The connection string was fine.
You need to specify 'AttachDbFileName' in the connection string. See the examples for sql server express here: http://www.connectionstrings.com/sql-server-2008.

Sql Server 2005 localhost System DSN not working

I've got an C# .Net 3.5 executable that runs on a local machine and makes database calls to a server running SQL Server 2005. I've added a System DSN using the Data Sources manager on the local machine called "localserver" (with the driver set to SQL Server). When I create the DSN, I can test the connection to the server successfully.
I've added localserver to my connection string in my executable, and 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
The server I am connecting to does allow for remote connections. When I replace localserver with the server name, it connects fine, but this program will be at multiple locations with multiple server names, so I need to make the data source dynamic.
Any ideas as to why this is happening?
Thank you,
Aaron
EDIT:
I've also tried using "localserver,1433" as my data source, but I get 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: TCP Provider, error: 0 - No such host is known.)"
EDIT:
Thank you for your responses. To solve my problem, I made a small method to gather the servername using an odbc connection.
I can then send the servername to the SqlConnection. Thanks again for your input.
SqlClient (ie. SqlConnection) has absolutely nothing to do with ODBC. As such using an ODBC Data source Name in the SqlClient connection string will get you nowhere fast.
You can configure the server name in app.config and build the connection string using SqlConnectionStringBuilder. At deployment, you change the exe's or the user's .config file appropriately.
As Remus said, DSN has nothing to do with SqlConnection. Instead use this connection string:
http://www.connectionstrings.com/sql-server-2005#1
Also read this:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
An Excerpt from the above post:
To connect to a local computer,
specify "(local)" for the server. If a
server name is not specified, a
connection will be attempted to the
default instance on the local
computer.
I would repeat that SqlConnection has nothing to do with DSN,

Categories