路径无效。检查数据库的目录
我只收到两个路径名为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条件来创建数据库。
答案 0 :(得分: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;
答案 1 :(得分: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();
}