【问题标题】:Where to place my .sdf file when testing application through Emulator通过模拟器测试应用程序时在哪里放置我的 .sdf 文件
【发布时间】:2012-07-03 15:32:41
【问题描述】:

我在 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 时..

【问题讨论】:

    标签: c# .net compact-framework sql-server-ce smartphone


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2020-05-07
      相关资源
      最近更新 更多