Can't connect local mdf database file - c#

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?

Related

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...

SQL SERVER ERROR (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I'm working on a website using ASP.NET, C# and mssql.
When I run the website on our local server it is working fine.
The following error occurs when I run it on a shared server:
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 don't know how to play with connection string now since I'm very new to this.
It is recommend to keep the connection string in web.config.
To create a valid connection string with SQL Server (either located on network or locally), use following steps:
Steps:
Create a Test.UDL file at your desktop.
Double click the file to see undermentioned Data Link Properties
Select provider i.e SQL Server in your case
Provide connection details i.e. server, username, password etc. in Connection tab.
Test Connection on connection tab and press "OK".
Open UDL file in notepad to see valid connection string.
UPDATE
check following link for creating UDL http://msdn.microsoft.com/en-us/library/e38h511e(v=vs.71).aspx
check following link for connection string Setting up connection string in ASP.NET to SQL SERVER

Error - "The server was not found or was not accessible" when trying to connect to sql server database

I'm working on a local project and trying to connect to a local database.
I have this class in it the connection string:
public class SQLConnection
{
public static string connectionString =
#"Data Source=TZVIKA-PC\SQLEXPRESS;Initial Catalog=WorldCup;Integrated Security=True";
public static WorldCupDBDataContext wcDataContext =
new WorldCupDBDataContext(connectionString);
public static WorldCupDBDataContext GetDataContextInstance()
{
return wcDataContext;
}
}
when I try to execute this line in a windows form:
teamBindingSource.DataSource = database.Teams.OrderBy(team => team.TeamId);
I get the following sql 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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
My friend ran the code and it works on his pc(when changing the connection string of course to a relevant one)
Why is it not working for me?
A good (and easy) way to test if your SQL connection is working is to use a .UDL file. Simply create an empty text (.txt) file on your desktop and rename the extension to .udl. Double click and you'll be presented with a SQL connection window, fill in the details, test connection and then (when it's working) save. Close the window and rename the file extension to .txt. Open and inside you'll find your working connection string.
Enable remote connections for SQL Server Express 2012
Most common problems are:
1) Your sql server express is not running. Check it in "sql server configuration manager"
2) server runs but don't allow remote connection. Use sql management studio to configure it.

Connecting to a SQL Database

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.

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.

Categories