-1

我有 windows 应用程序来同步存储在 SQL Server CE 数据库中的 windows mobile 应用程序的数据。我在同步连接到 Windows 7 32 位 PC 的移动设备时遇到了一些问题。

同步时,Windows 应用程序会将 SQL Server CE 数据库从移动设备复制到 PC,并与本地复制的数据库同步数据,然后复制回移动设备。如果完成同步的 PC 是 windows 7 32 位,那么在使用同步数据库时,我会在 Windows Mobile 应用程序中遇到以下问题。

如果有一个包含列的表nVarChar并对该列进行搜索查询,如果搜索条件包含带单引号的数字以将值表示为字符串(在以下示例strProductID中为nVarchar列) ,则不会返回任何数据

例如:-

SELECT * FROM Products WHERE strProductID = '2345' 

但是不带引号的数字或带引号的字母可以正常工作

例如:-

SELECT * FROM Products WHERE strProductID = 2345 

或者

SELECT * FROM Products WHERE strProductID = 'asdasd'
4

1 回答 1

0

正如 Aaron Bertrand 在评论中所说,如果您使用参数,您的查询将工作得更好。

strProductIDNVARCHAR 字段有多大?50?我将假设为 50,因此我可以编写您的参数化查询:

private DataTable Get(string strProductID, string connStr) {
  var table = new DataTable();
  string cmdText = "SELECT * FROM Products WHERE strProductID=@strProductID";
  using (var cmd = new SqlCeCommand(cmdText, new SqlCeConnection(connStr))) {
    cmd.Parameters.Add("@strProductID", SqlDbType.NVarChar, 50).Value = strProductID;
    cmd.Connection.Open();
    table.Load(cmd.ExecuteReader());
    cmd.Connection.Close();
  }
  return table;
}

但是,Microsoft 已经承认,断开连接的数据集在 Windows Mobile 设备中不是很可靠!请参阅演练:创建偶尔连接的智能设备应用程序中的注释。

于 2013-04-18T14:41:35.193 回答