locked
Creating a SQL Database in C# RRS feed

  • Question

  • Hi and TIA:

    I have (hopefully) an easy one this has me stumped.

    I am merely trying to create a database in CE using C#. The statements:

    string filename = "GolfDatabase.sdf";
    string connectionString;

    connectionString = string.Format("DataSource = | DataDirectory |\ {0}",filename);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();

    When building, I get an error message of "Unrecognized escape sequence"

    If I add a second '\' and get:

    connectionString = string.Format("DataSource = | DataDirectory |\\ {0}",filename);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();

    The code compiles, but upon running I get a message pointing to the en.CreateDatabase(); line which says, "The file name is not valid. Check the file name for the database. [ File name = | DataDirectory |\ GolfDatabase.sdf ]"

    All I am trying to do is create my database and under locals I see:
    connectionString    "DataSource = | DataDirectory |\\ GolfDatabase.sdf"    string

    Which looks like one too many back slashes

    I do not wish to use a Password, just create my database. Seems like I either can have two back slashes or none at all.

    Note:
    When I am creating tables I use:

    SqlCeConnection cm = new SqlCeConnection(@"Data Source = | DataDirectory |\GolfDatabase.sdf");
    .....and all is well, thus the "create database" code comes from this above line of code which works just fine.

    Thanks for the help,

    Cliff
    Tuesday, October 20, 2009 9:46 PM

Answers

  • I am merely trying to create a database in CE using C#. The statements:

    string filename = "GolfDatabase.sdf";
    string connectionString;

    connectionString = string.Format("DataSource = | DataDirectory |\ {0}",filename);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();

    When building, I get an error message of "Unrecognized escape sequence"

    If I add a second '\' and get:

    connectionString = string.Format("DataSource = | DataDirectory |\\ {0}",filename);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();
    Make sure there are no white-spaces between '|' , DataDirectory keyword and '|'. The following code should work fine:
    connectionString = string.Format("DataSource = |DataDirectory|\\ {0}",filename);
    SqlCeEngine en = new SqlCeEngine(connectionString);
    en.CreateDatabase();



    “This posting is provided "AS IS" with no warranties, and confers no rights”.
    Wednesday, October 21, 2009 2:53 AM