Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlCeDataReader始终为空_C#_.net_Sql_Windows Mobile_Sql Server Ce - Fatal编程技术网

C# SqlCeDataReader始终为空

C# SqlCeDataReader始终为空,c#,.net,sql,windows-mobile,sql-server-ce,C#,.net,Sql,Windows Mobile,Sql Server Ce,嘿,伙计们,我正在Windows Mobile 5.0(.Net 2.0)上工作 我遇到了一个小问题,我的阅读器总是空的。我的关系很好,我相信其他一切都很好,唯一的问题是读者。 在我例外的情况下,它说 ((System.Data.SqlServerCe.SqlCeException)(e)) : {"The operation completed successfully."} InnerException: Could not evaluate expression 我的数据库文件没有损坏

嘿,伙计们,我正在Windows Mobile 5.0(.Net 2.0)上工作 我遇到了一个小问题,我的阅读器总是空的。我的关系很好,我相信其他一切都很好,唯一的问题是读者。 在我例外的情况下,它说

((System.Data.SqlServerCe.SqlCeException)(e)) :  {"The operation completed successfully."}
InnerException: Could not evaluate expression
我的数据库文件没有损坏,我在应用程序外部打开了它,一切看起来都很好。 我的代码如下:

  public static List<Image> GetAll(BOI caller)
    {
        List<Image> images = new List<Image>();
        SqlCeConnection c = Connection.GetConnection(caller.ConnectionString);

        if (c != null)
        {
            SqlCeCommand command = new SqlCeCommand("SELECT * FROM Images", c);

            SqlCeDataReader reader = null;
            try
            {
                reader = command.ExecuteReader();  <<<<< reader is null <<<<<<<
            }
            catch (Exception e)
            {
                while (reader != null && reader.Read())
                {
                    Image temp = new Image((int)reader["imageKey"],
                              (String)reader["filename"],
                              (byte[])reader["image"],
                              (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]),
                              (int)reader["priority"]);
                    temp.SetDBName(caller.ConnectionString);

                    images.Add(temp);
                }
            }
        }

        return images;
    }
获取连接函数:

    public static SqlCeConnection GetConnection(string connectionString)
    {
        SqlCeConnection conn = null;
        try
        {
            if (connections.Count == 0)
            {
                OpenConnection(connectionString);
            }
            conn = connections[connectionString];
        }
        catch (System.Exception)
        {
        }
        return conn;
    }
编辑:3 使用时的异常代码

SqlCeCommand命令=新建SqlCeCommand(“从图像中选择*,其中 imageKey=6“,c)


除非cm.executereader引发异常,否则永远不会出现以下代码行:

while (reader != null && reader.Read())
尝试将下面的代码移到catch语句之外

            while (reader != null && reader.Read())
            {
                Image temp = new Image((int)reader["imageKey"],
                          (String)reader["filename"],
                          (byte[])reader["image"],
                          (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]),
                          (int)reader["priority"]);
                temp.SetDBName(caller.ConnectionString);

                images.Add(temp);
            }

雷蒙德,我猜你是新来的

看看你提供的这个版本

public static List<Image> GetAll(BOI caller) {
  List<Image> images = new List<Image>();
  using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Images", Connection.GetConnection(caller.ConnectionString))) {
    try {
      cmd.Connection.Open();
      using (SqlCeReader reader = cmd.ExecuteReader()) {
        while (reader.Read()) {
          object imageKey = reader["imageKey"];
          object filename = reader["filename"];
          object image = reader["image"];
          object labelKey = reader["labelKey"];
          object priority = reader["priority"];
          Image img = new Image((interface)imageKey, (string)filename, (byte[])image, (int?)labelKey, (int)priority);
          img.SetDBName(caller.ConnectionString);
          images.Add(img);
        }
      }
    } catch (SqlCeException err) {
      Console.WriteLine(err.Message);
      throw err;
    } finally {
      cmd.Connection.Close();
    }
  }
  return images;
}
公共静态列表GetAll(BOI调用方){
列表图像=新列表();
使用(SqlCeCommand cmd=newsqlcecommand(“从图像中选择*,Connection.GetConnection(caller.ConnectionString))){
试一试{
cmd.Connection.Open();
使用(SqlCeReader=cmd.ExecuteReader()){
while(reader.Read()){
对象imageKey=读卡器[“imageKey”];
对象文件名=读取器[“文件名”];
对象图像=读卡器[“图像”];
对象labelKey=reader[“labelKey”];
对象优先级=读卡器[“优先级”];
Image img=新图像((接口)imageKey,(字符串)filename,(字节[])Image,(int?)labelKey,(int)priority);
SetDBName(caller.ConnectionString);
图像。添加(img);
}
}
}捕获(SqlCeException错误){
控制台写入线(错误消息);
犯错误;
}最后{
cmd.Connection.Close();
}
}
返回图像;
}
对于可为空的整数,您需要添加一些功能来测试您的对象,但大多数情况下都是干净简单的


控制台上放置断点。WriteLine()
,如果出现异常,请将鼠标悬停在
消息上。

问题的最终原因是,当我切换到
3.5.0.0
时,我使用的是
System.Data.SqlServerCe 3.5.1.0
,它工作正常。 我把while循环放在catch语句之外


但我仍然对微软感到恼火,因为他们在这方面没有表现出任何错误

显示接下来的几行代码会很有帮助我看不到您显式地打开了连接?那是在
GetConnection
中吗?是的,我打算编辑我的问题来写。你是否错误地剪切和粘贴了代码?我猜您没有读取catch(异常e)块中的reader对象?@GrandMasterFlush
reader=command.ExecuteReader()在catch异常块之前为null。我必须这样做吗?我认为我不需要指定命令类型。这是一个危险的问题。默认值为“Text”,在本例中可以。您是否检查connection.GetConnection中的连接状态?连接正常且已打开。您是否有任何进一步的信息。你能指定堆栈跟踪吗?不知道。我会从设备中提取数据库,使用SSMS连接到它,然后执行相同的查询。谢谢你的帮助。。。但问题是因为我使用的是System.Data.SqlServerCe版本3.5.1.0,我使用的是3.5.0.0,它工作起来很有魅力。这个解决方案与将循环移到捕获之外有关,与更改sql server compact提供程序的版本几乎没有关系。@JoelCoehoorn不是你的错。我把线圈移到了捕捉器外,它仍然给了我一个错误。然后我更改了sql dll的版本,它工作得很好。@raym0nd:然后你的电脑和目标设备上的二进制文件不匹配。在使用3.5.1时,有许多方法都没有任何问题。您必须根据正确的版本进行编译,并将同一版本部署到目标,否则会出现奇怪的行为。
            while (reader != null && reader.Read())
            {
                Image temp = new Image((int)reader["imageKey"],
                          (String)reader["filename"],
                          (byte[])reader["image"],
                          (reader["labelKey"] == DBNull.Value ? null : (int?)reader["labelKey"]),
                          (int)reader["priority"]);
                temp.SetDBName(caller.ConnectionString);

                images.Add(temp);
            }
public static List<Image> GetAll(BOI caller) {
  List<Image> images = new List<Image>();
  using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Images", Connection.GetConnection(caller.ConnectionString))) {
    try {
      cmd.Connection.Open();
      using (SqlCeReader reader = cmd.ExecuteReader()) {
        while (reader.Read()) {
          object imageKey = reader["imageKey"];
          object filename = reader["filename"];
          object image = reader["image"];
          object labelKey = reader["labelKey"];
          object priority = reader["priority"];
          Image img = new Image((interface)imageKey, (string)filename, (byte[])image, (int?)labelKey, (int)priority);
          img.SetDBName(caller.ConnectionString);
          images.Add(img);
        }
      }
    } catch (SqlCeException err) {
      Console.WriteLine(err.Message);
      throw err;
    } finally {
      cmd.Connection.Close();
    }
  }
  return images;
}