I'm new to database and I've been following a few tutorials but I'm stuck with trying to connect to my database using compact edition that I created in Visual Studio.
I'm trying to connect to the database by:
string connection = "Data Source=C:/Users/PLOW/Documents/Visual Studio 2012/Projects/File Name/File Name/Users.sdf";
// Making SQL connection
SqlConnection cn = new SqlConnection(connection);
cn.Open();
Doing so 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..."
I tried:
string connection = #"Data Source=|DataDirectory|\Users.sdf";
and I get the same error. I don't know how connect it at this point.
This are my connection string:
As you are using compact edition, so SqlConnection object is not needed here, instead use SqlCeConnection and add reference to System.Data.SqlServerCe.
Use SqlCeCommand instead of SqlCommand
if it's Connectionstring issue, You must declare required parameters to generate connectionstring
Data Source=servername;Initial Catalog=DbName;Integrated Security=True;User ID=userName;Password=password
Related
I am trying to connect to Oracle database on my local machine. I have Visual Studio 2013 and Oracle Express 11g.
Here's my c# code:
String str =
"Data Source=localhost;" +
"Initial Catalog=XE;" +
"User Id=system;" +
"Password=12345;";
SqlConnection conn = new SqlConnection(str);
conn.Open();
However it throws an exception:
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
Does anybody know what's going wrong?
Check your provider, as I commented above....
Taken from Oracle Web Site...
string oradb = "Data Source=(DESCRIPTION="
+ "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=ORASRVR)(PORT=1521)))"
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=ORCL)));"
+ "User Id=***;Password=***;";
OracleConnection conn = new OracleConnection();
conn.ConnectionString = oradb;
conn.Open();
You may want to look at ConnectionStrings.com to help with which suits you best.
The problem is that you're trying to use the classes in the System.Data.SqlClient namespace with Oracle. SqlClient is only for Sql Server. You need to use either an Oracle .Net provider or the classes in the System.Data.OleDb namespace.
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!
This is strange as I'm able to connect to localDB through SSMS 2008R2 with the same connection string ("Data Source=(LocalDB)\v11.0;Integrated Security=true")
Only C# code is unable to connect, I have tried increasing login time with Connect Timeout=60 but no luck.
I have also tried specifying the database Initial Catalog=<databasename> where the <databasename> is the one I have created on localdb via ssms.
Any pointers as to why is this not connecting?
Any chance it is because you forgot to double-escape the backslash? Did you try this:
"Data Source=(LocalDB)\\v11.0;Integrated Security=true"
Or even better, you could just use the # mark to disable character escaping for the entire connection string:
#"Data Source=(LocalDB)\v11.0;Integrated Security=true"
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
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