naga
naga

Reputation: 21

The path is not valid. Check the directory for the database. - SDF file is in the local drive - C#

The path is not valid. Check the directory for the database

I am getting the above error for just two files where the path name is c:\temp\sdf-1\mydatabase.sdf. The code works fine for almost all sdf databases without issues. I am not using an emulator and I am accessing this file from C# application. I am passing the full path as connectionstring and I am not having issues with other files. Is there anything I am missing?

connectionStr = "c:\\temp\\s1234-1234\mydatabase.sdf"
connectionString = "DataSource="+ connectionStr + _password;
SqlCeEngine en = new SqlCeEngine( _connectionString );
en.CreateDatabase();
en.Dispose();
return true();

public bool CreateTransactionLog(string connectionStr)
{
    try
    {
       if ( !System.IO.File.Exists( connectionStr ) )
       {
          //string connectionString = "DataSource=\"C:\\test1.sdf\"; Password=\"mypassword\"";
          _connectionString = "DataSource="+ connectionStr + _password;

          SqlCeEngine en = new SqlCeEngine( _connectionString );
          en.CreateDatabase();
          en.Dispose();
          return CreateTransactionTable();

          //return true;
       }

       else
       {
          _connectionString = "DataSource="+ connectionStr + _password;
          return true;
       }           
    }
    catch (SqlCeException e)
    {           
        MessageBox.Show( e.Message ,"Error creating Table",MessageBoxButtons.OK,MessageBoxIcon.Error);
        return false;
    }
}

What is strange is that even if the file exists, the application enters into the if Condition to create the database.

Upvotes: 1

Views: 4778

Answers (2)

ErikEJ
ErikEJ

Reputation: 41749

This Works fine on my PC:

        string _password = ";Password=secret";
        string connectionStr = @"c:\temp\s1234-1234\mydatabase.sdf";
        string _connectionString = "Data Source="+ connectionStr + _password;
        using (SqlCeEngine en = new SqlCeEngine(_connectionString))
        {
            en.CreateDatabase();
        }             

Upvotes: 0

Damith
Damith

Reputation: 63065

you missed one \

connectionStr = "c:\\temp\\s1234-1234\\mydatabase.sdf"

Or use

connectionStr = @"c:\temp\s1234-1234\mydatabase.sdf"

try something like below

string sdfPath  = @"c:\temp\s1234-1234\mydatabase.sdf";
var _connectionString  = string.Format("Data Source = {0}; Password = {1}", sdfPath, password);
using (SqlCeEngine en = new SqlCeEngine(_connectionString))
{
    en.CreateDatabase();
}
return true;

Upvotes: 3

Related Questions