Connect to local sql database in c# - c#

I'm working on C# .net application that needs to connect to a simple database. This database is btw in the same visual studio project and is called Playerdatabase.mdf.
In the application I want to connect to the database by using the following code:
string connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\User1\\Documents\\Visual Studio 2013\\Projects\\ProreactorTest\\ProreactorTest\\Playerdatabase.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection con = new SqlConnection(connectionString);
con.Open();
But when I try to run the application and when it calls the open() method I got the exception:
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
Inner exception: can not find network path.
I looked at the path (by browsing to it) and it looks fine. I also read that disabling the firewall works, tried that but got the same exception.
Does anybody have a idea or suggestion? Any feedback is welcome!

Related

C# app with SqlClient does not work on another computer

I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.

SQL Server connection string causing error?

In a Winforms application, I have a connection string that causes an error
data source=(local);initial catalog=RegisterDB;persist security info=False
Error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
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: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Thanks for the help Arif Ansari but it still gives the same error.
I'm using Microsoft Visual Studio 2013 Express with SQL Server. I created the database from within Microsoft Visual Studio 2013 Express
Can anyone help?
Hi marc_s
Sorry it didnt work. I created the Database RegisterDB manually with Microsoft Visual Studio 2013 Express in server "(localdb)\v11.0"
Can anyone help ???????
Please Try it
SqlConnection con = new SqlConnection("Data Source=(local);Database=RegisterDB;Integrated Security= true");
try this query as your connection string format
Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;
or this
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
basically check the instance of the server that you are running.
Check if the instance service is running or not.
Type services.msc in the start menu to open the Services List.
Check for the name SQL SERVER (your instance name).
If the instance status is Started, issue is in your connection string or user permissions.
Else if the status is empty or stopped, start the instance and then try connecting.
you may also refer this site for connection string format.
The SQL Server that comes with Visual Studio 2013 is the 2012 Express version - and that by default install the "LocalDB" support.
So I would try this connection string instead:
"data source=(localdb)\v11.0;initial catalog=RegisterDB;Integrated Security=SSPI;"
Does that work? If not: are you auto-creating the database, or if not, did you manually create that database RegisterDB that you're referencing in the connection string?

Unable to connect to local database in Visual Studio 2012 express For Web: (error 26)(C#)(SQL Server 2012)

Recently, I've made a simple website with a database (in an App_Data folder). I made a class file (in an App_Code folder) with the following method that will create a connection to the database:
public static SqlConnection ConnectToDb(string dbFileName)
{
string path = HttpContext.Current.Server.MapPath("App_Data/");
path += dbFileName;
string connString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=" +
path +
";Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(connString);
return conn;
}
When I need to open that connection, I get the following message:
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)
What I don't understand is that I used the -same- method in another website that I built over a year ago in Visual Studio 2010, where it worked fine.
I checked the path and it was correct, same with the database file name in the parameters.
What is wrong with the code or with VS's settings? Thanks in advance.
EDIT: Just tried to use (localDB)\v11.0 instead of .\SQLEXPRESS
and now I get this error message:
The user instance login flag is not allowed when connecting to a user instance of SQL Server.
You're getting that error because the (localDB)\v11.0 is already a user instance of SQL Server. When you were using the SQLEXPRESS SQL Server instance, well, it's not. So, just get rid of the User Instance=true on the connection string.

Error in connection to database

The following code gives an error during execution.
string connectionString = "Data Source=D:\\Base.sdf;Persist Security Info=False";
SqlConnection sqlConnection = new SqlConnection(connectionString))
sqlConnection.Open();
The error is:
A network-related or instance-specific error occured 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 not configured to allow remote connections.
(provider:SQL network Interfaces, error: 26 - Error locating
Server/Instance Specified)
I tried SqlCeConnection instead of SqlConnection but, the compiler couldn't find the library with that class.
Please, help to solve this problem.
The OS : Windows 7
Tool : Microsoft Visual Studio 2010
Language : C#
Your database is a Sql Server compact edition one, you must use :
SqlCeConnection sqlConnection = new SqlCeConnection(connectionString);
Download the libs from here Microsoft SQL Server Compact 4.0
Add a reference to System.Data.SqlServerCe.dll to your project
Add this using directive using System.Data.SqlServerCe;
Use SqlCeConnection instead of SqlConnection

SQL Compact 2008 Connection String Problem

I have the following code to connect to a sql server compact edition 2008:
private SqlConnection sqlConn;
public void createConnection()
{
String connectionString = #"Data Source=C:\Projects\somefile.sdf;Persist Security Info=False";
sqlConn = new SqlConnection(connectionString);
sqlConn.Open();
}
However, I keep getting the following error when sqlConn.Open() is executed:
"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)"
Does anyone have any ideas what the problem might be? I can create a connection to the db in the database explorer but it doesn't seem to work in code.
The Sql* classes in System.Data.SqlClient can only be used to connect to a regular SQL Server instance.
To connect to a SQL CE database, you need to create a SqlCeConnection object in System.Data.SqlServerCe.dll.
Is the sdf file in the same directory as the executing application?
How to specify the location of the SDF file
Often times the .SDF database is not running in the current directory so it becomes necessary to programatically set the path to the SDF file. This is an example (.net C#) on how to do this when the SDF file is located in the same directory as the executing application.
Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyData.sdf;Persist Security Info=False;
Source

Categories

Resources