路径无效.检查数据库的目录.-SDF文件位于本地驱动器-C#中
本文关键字:文件 于本地 驱动器 -C# -SDF 检查 无效 数据库 路径 | 更新日期: 2023-09-27 18:16:56
路径无效。检查数据库的目录
对于路径名为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 Condition来创建数据库。
您错过了一个'
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;
这在我的电脑上运行良好:
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();
}