0

路径无效。检查数据库的目录

我只收到路径名称为 c:\temp\sdf-1\mydatabase.sdf 的两个文件的上述错误。该代码适用于几乎所有 sdf 数据库,没有问题。我没有使用模拟器,而是从 C# 应用程序访问此文件。我将完整路径作为连接字符串传递,其他文件没有问题。有什么我想念的吗?

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;
    }
}

奇怪的是,即使文件存在,应用程序也会进入 if 条件来创建数据库。

4

2 回答 2

3

你错过了一个\

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

或使用

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

试试下面的东西

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;
于 2013-06-05T12:36:38.380 回答
0

这在我的电脑上运行良好:

        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();
        }             
于 2013-06-05T12:52:41.327 回答