在通过Emulator测试应用程序时放置.sdf文件的位置

时间:2010-08-10 10:36:27

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

我正在使用smartApplication,当我尝试连接到我的SQL Server CE 2005数据库时,我得到了异常

路径无效。检查数据库的目录。 [Path = 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中并从connectionstring中删除除数据库文件名之外的路径时,它可以正常工作。

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

因此对于Emulator,我们必须将sdf文件放在Mobile Devide中,如上所述

相关问题