c# database connection string

10,587

Solution 1

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

Solution 2

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\...")

Solution 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");

Solution 4

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

Solution 5

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

Share:
10,587
Jacco
Author by

Jacco

Updated on June 28, 2022

Comments

  • Jacco
    Jacco almost 2 years

    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");
    
  • Jacco
    Jacco over 11 years
    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?
  • Jomy John
    Jomy John over 11 years
  • Jacco
    Jacco over 11 years
    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?
  • shraysalvi
    shraysalvi over 11 years
    @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.
  • Jacco
    Jacco over 11 years
    Its there but how do i refer to it? :/
  • shraysalvi
    shraysalvi over 11 years
    @Jacco Right Click on your project inside Visual Studio -> Choose Add reference then browse the to the Assembly (System.Data.SqlServerCe.dll)
  • shraysalvi
    shraysalvi over 11 years
    @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.
  • Jacco
    Jacco over 11 years
    You'r probably right, will do and once again thanks alot for all help.