none
LINQ SQLCE CreateDatabase() / DatabaseExists() Methods RRS feed

  • Question

  • Hi All,

    I'm just using these for the first time and I'm seeing some odd behaviour with the DatabaseExists() method.  Basically, its always returning false even when the database exists.  I'm basically testing a class in C# using my LINQ datacontext that ensures the required database for my application exists and is accessible before going on to use it.  The class is:

    Code Snippet

    Internal Static DatabaseCreator

        {
            public static bool DirectoryExisted { get; private set; }
            public static bool DirectoryCreated { get; private set; }
            public static bool DatabaseExists { get; private set; }
            public static bool DatabaseCreated { get; private set; }
            public static string AppPath { get; private set; }

            static SSdb _context;
            static DatabaseCreator()
           {
               string temp = System.Reflection.Assembly.GetExecutingAssembly().Location;
                AppPath = temp.Replace("DataLayer.dll", "");
            }

            public static void CheckDatabase()
            {
                _context = new SSdb(Properties.Settings.Default.SSDatabase);
                if (!Directory.Exists(AppPath + "\\Database"))
                {

          DirectoryExisted = false;

                   Directory.CreateDirectory(AppPath + "\\Database");
                   DirectoryCreated = true;
                   DatabaseExists = false;
                   _context.CreateDatabase();
                }
                else
               {
                    if (!_context.DatabaseExists())
                    {
                        _context.CreateDatabase();
                    }
                }
            }





    All is working fine if the database isn't there, this class happily pops off and creates the correct shape database in the correct location.  I've tested the database by connecting to it via Visual Studio with the expected connection string and password and so on, all is as it should be, the application can also carry on and use this database.  The problem happens when I run this class when the database is already in existence, it tries to create it again and says that the file already exists.

    Am I doing something obviously wrong here?  Anyone else seen this sort of thing?  I know the class needs some work before it is rock solid but this basic functionality should be ok as is.

    What I've noticed is that this problem only occurs when the connection sting in use is as follows:

    <pre>"Data Source=|DataDirectory|\\DataBase\\SSdb.sdf;Password=*********;Persist Security Info=True"</pre>

    If I use a connection string replacing |DataDirectory| with the full path the DatabaseExists() method works as expected.

    I'm not getting why CreateDatabase() can use the |DataDirectory| path ok, but DatabaseExists() cannot.  Is this a bug in the framework or am I missing something regarding the use of these methods?  I don't know if this makes any difference but I'm using SQL Compact 3.5.

    Thanks in advance ...
    Thursday, June 26, 2008 1:29 PM

Answers

  • This is a known bug with SQL CE and the use of the |DataDirectory| macro, and will be addressed in a future release.

     

    Thanks,

     

    --Samir

     

    Thursday, June 26, 2008 4:48 PM

All replies

  • This is a known bug with SQL CE and the use of the |DataDirectory| macro, and will be addressed in a future release.

     

    Thanks,

     

    --Samir

     

    Thursday, June 26, 2008 4:48 PM
  • Hi Samir,

    Thanks very much for this reply.  Are there any 'best practice' workarounds for this according to Microsoft?

    I'm thinking that in my class I update the dll's local connection string property using the full path and then use this complete connectionstring when instantiating the datacontext.
    Thursday, June 26, 2008 5:22 PM
  • After 2 years, the problem is still there! I am using VS2010 with SQLExpress with a local instance of a db placed in the |DataDirectory| and I get that the DB doesn't exists every single time.

     

     


    MCT, MCDBA, MCSD.NET, MCPD 2.0(*.*), MCPD 3.5(*.*)
    Thursday, September 2, 2010 11:00 AM