1

我在 smartApplication 上工作,当我尝试连接到我的 SQL Server CE 2005 数据库时,我得到了异常

路径无效。检查数据库的目录。[ 路径 = D:\SmartProject\DBFile.sdf ]

我的连接字符串是

数据源=D:\SmartProject\DBFile.sdf;密码=test123

连接的代码就像

 string connectionString = "Data Source=D:\\SmartProject\\DBFile.sdf;Password=test123";
    SqlCeConnection Connection = new SqlCeConnection(connectionString);
    SqlCeCommand comm = new SqlCeCommand(SqlSelectCommandText, Connection);
    SqlCeDataAdapter da = new SqlCeDataAdapter(comm);
    DataSet ds = new DataSet();
    try
    {
        Connection.Open();
        da.Fill(ds);
        if (ds.Tables.Count > 0)
            dataTable = ds.Tables[0];
        else
            dataTable = new DataTable();
        bsuccessfullyExecuted = true;
    }
    catch (SqlCeException ex)
    {
        bsuccessfullyExecuted = false;
        dataTable = null;
    }
    finally
    {
        Connection.Close();
    }

当代码尝试打开连接时,只要文件位于指定位置或目录,它就会抛出此异常。

当我将带有 .exe 的 DBFile.sdf 文件放在 bin 中并从连接字符串中删除除数据库文件名之外的路径时,它就可以工作。

但是当我尝试通过模拟器访问它时,它会显示此错误。前提是它通过底座和 Windows Mobile 设备中心连接。它显示了所有页面,但是当我尝试通过异常访问 Db 时..

4

1 回答 1

0

实际上我们必须放入DBFile.sdf移动设备文件夹,现在连接字符串将是

Data Source=\Temp\DBFile.sdf;Password=test123

此 Temp 位于 Mobile Device 文件夹中,因为我们的设计器会话连接到 SQL Mobile 数据库,该数据库位于通过 ActiveSync 连接的移动设备上。

因此,使用 bindingsource 自动生成的到数据库的连接字符串是一个特殊的连接字符串,只能在 VS2005 中工作,它以 DataSource = Mobile Device 开头.....

所以对于模拟器,我们必须将 sdf 文件放在 Mobile Devide 中,如上

于 2010-08-10T13:04:01.730 回答