0

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:

enter image description here

enter image description here

PLOW
  • 379
  • 2
  • 15
  • 3
    Possible duplicate of [Connecting to local SQL Server database using C#](http://stackoverflow.com/questions/12220865/connecting-to-local-sql-server-database-using-c-sharp) – James Black Oct 26 '15 at 04:55

2 Answers2

3

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

Imad
  • 7,126
  • 12
  • 55
  • 112
  • Tested it and that worked. Connection is establish. However, that through up another error in: SqlCommand cmd = new SqlCommand(sqlAddUserPass, cn); sqlAddUserPass is insert querie – PLOW Oct 26 '15 at 05:13
  • I'm trying to insert data by using SqlCommand but I get overload error because I changed the connection to SqlCeConnection. Nonetheless, you answered my initial questions so I'm marking it as answered. – PLOW Oct 26 '15 at 05:19
  • use `SqlCeCommand` instead of `SqlCommand` and inform me whether it is solved :) – Imad Oct 26 '15 at 05:26
0

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
Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28