2

I'm fairly new to c# and just now started working with databases. I made a local database in my project folder, when I click properties on the database I get "Connection string" as:

Data Source="C:\Users\Documents\Visual Studio 2012\Projects\DataBaseTest\MyDatabase#1.sdf"

My question is simple, how do I create a connection string using that line? cause writing this generates an error for some reason.

SqlConnection con = new SqlConnection(Data Source="C:\Users\Documents\Visual Studio
    2012\Projects\DataBaseTest\MyDatabase#1.sdf");
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
jlodenius
  • 829
  • 2
  • 13
  • 24

6 Answers6

7

You are using a SQL Server Compact edition database and for that you must use SqlCeConnection instead of SqlConnection (Which is used for SQL Server).

You must also escape the back backslashes in your connection string \ becomes this \\ :

SqlCeConnection sqlConnection = new SqlCeConnection("Data Source=C:\\Users\\Documents\\Visual Studio
2012\\Projects\\DataBaseTest\\MyDatabase#1.sdf");

or use a verbatim string :

SqlCeConnection sqlConnection = new SqlCeConnection(@"Data Source=C:\Users\Documents\Visual Studio
2012\Projects\DataBaseTest\MyDatabase#1.sdf");

And if you don't have the necessary libs for using SqlCe in your projects refer to my other answer here :

Download the libs from here Microsoft SQL Server Compact 4.0

  1. Add a reference to System.Data.SqlServerCe.dll to your project
  2. Add this using directive using System.Data.SqlServerCe;
  3. Use SqlCeConnection instead of SqlConnection
Community
  • 1
  • 1
Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • Thanks for this info, had no idea :) but when I try to download it I get a message saying I already have a later version (I guess it came with the visual studio) but I still cant refer to it, cause it doesent seem to be in System.Data, any ideas? – jlodenius Oct 15 '12 at 10:22
  • @Jacco look into `C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Desktop` (for x86) or `C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Desktop` for x64. – Nasreddine Oct 15 '12 at 10:28
  • Its there but how do i refer to it? :/ – jlodenius Oct 15 '12 at 10:38
  • @Jacco Right Click on your project inside Visual Studio -> Choose `Add reference` then browse the to the Assembly (System.Data.SqlServerCe.dll) – Nasreddine Oct 15 '12 at 10:40
  • @Jacco Maybe you should post that as a new question with the full stack trace of the exception and the new code you're using for the connection. – Nasreddine Oct 15 '12 at 10:59
  • You'r probably right, will do and once again thanks alot for all help. – jlodenius Oct 15 '12 at 11:00
4

You need to escape backslashes (replace backslashes by double backslashes) or put an @ in front of your string to tell C# to take it literally, like so:

new SqlConnection("C:\\Users\\...")

or

new SqlConnection(@"C:\Users\...")
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
3

For path you can't add backslashes just like that . You need to escape backslashes . There are two ways

to escape backslash

Method #1 - Use @ in the beginning of the path string

SqlConnection con = new SqlConnection(@"Data Source=C:\Users\Documents\Visual Studio
    2012\Projects\DataBaseTest\MyDatabase#1.sdf");

Method #2 - add double backslashes instead of one backslashes

SqlConnection con = new SqlConnection("Data Source=C:\\Users\\Documents\\Visual Studio
    2012\\Projects\\DataBaseTest\\MyDatabase#1.sdf");
Jomy John
  • 6,308
  • 4
  • 28
  • 32
  • Thanks for all the quick answers! :) seems to work without any error, but when I run my code and tries to input stuff in the database using a simple form and a button I get this error on that same line: "Format of the initialization string does not conform to specification starting at index 0." Any idea what that means? – jlodenius Oct 15 '12 at 10:09
  • Please ref http://stackoverflow.com/questions/9040266/how-to-fix-error-format-of-the-initialization-string-does-not-conform-to-speci – Jomy John Oct 15 '12 at 10:17
2
SqlConnection con = new SqlConnection(@"C:\Users\Documents\Visual Studio
    2012\Projects\DataBaseTest\MyDatabase#1.sdf");
Raab
  • 34,778
  • 4
  • 50
  • 65
2

You need to use the @ symbol to show that the string is literal, and ignore special characters.

This page shows samples http://www.dotnetperls.com/string-literal

Stephen Walker
  • 574
  • 3
  • 10
0

For Local Database in Visual Studio 2012, System.Data.SqlServerCe needs to be imported in the code. I created a console application which will connect to local database that i added from solution explorer tab and the code will look like

SqlCeConnection con = new SqlCeConnection(@"Data ource=C:\Users\MyComputer\Documents\Visual Studio 2012\Projects\ConsoleApplication4\ConsoleApplication4\Database1.sdf");
            con.Open();

        string sql = "SELECT * FROM Employee";//select query
        string sql1 = "INSERT INTO Employee(Name, Age)VALUES('aaa', 12)";//insert query

                SqlCeCommand cmd = new SqlCeCommand(sql, con);
                SqlCeCommand cmd1 = new SqlCeCommand(sql1, con);
                cmd1.ExecuteScalar(); // executing insert command
                SqlCeDataReader reader = cmd.ExecuteReader();//to read data from table
                while (reader.Read())
                {
                    Console.WriteLine(String.Format("{0}, {1}", reader[0],reader[1]));
                }
               con.Close();