Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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# 数据库选择方法给出IndexoutofRangeException_C#_Sql_Tsql - Fatal编程技术网

C# 数据库选择方法给出IndexoutofRangeException

C# 数据库选择方法给出IndexoutofRangeException,c#,sql,tsql,C#,Sql,Tsql,我试图读取数据库输出,字段在数据库中,但它给了我IndexoutofRangeException错误。我像数据库中的其他值一样声明了BLabel。有什么建议吗 SqlConnection dbConn = null; StringBuilder sqlString = new StringBuilder(); sqlString.Append("SELECT f.*, v.VersionNumber "); sqlString.Append("FROM PackLabelFormat f, Pa

我试图读取数据库输出,字段在数据库中,但它给了我IndexoutofRangeException错误。我像数据库中的其他值一样声明了BLabel。有什么建议吗

SqlConnection dbConn = null;
StringBuilder sqlString = new StringBuilder();
sqlString.Append("SELECT f.*, v.VersionNumber ");
sqlString.Append("FROM PackLabelFormat f, PackLabelVersion v ");
sqlString.Append(" WHERE f.FormatID = @FormatID ");
sqlString.Append(" AND f.FormatID = v.FormatID ");
sqlString.Append(" AND v.VersionID = (SELECT MAX(VersionID) ");
sqlString.Append("FROM PackLabelVersion v2 ");
sqlString.Append("WHERE v2.FormatID = v.FormatID) ");
try
{
   using (dbConn = new SqlConnection(Properties.Settings.Default["tville"].ToString()))
   {
      SqlCommand cmd = dbConn.CreateCommand();
      cmd.CommandText = sqlString.ToString();
      cmd.Parameters.AddWithValue("@FormatID", FormatID);
      dbConn.Open();

      using (SqlDataReader reader = cmd.ExecuteReader())
      {
         if (reader.HasRows)
         {
            reader.Read();
            FormatName = reader["FormatName"].ToString();
            FormatDescription = reader["FormatDescription"].ToString();
            StockID = Convert.ToInt32(reader["StockID"].ToString());
            PrintCode = bool.Parse(reader["PrintPlantCode"].ToString());
            PrintPrice = bool.Parse(reader["PrintPrice"].ToString());
            PrintWeight = bool.Parse(reader["PrintWeight"].ToString());
            CurrentVersion = reader["VersionNumber"].ToString();
            BLabel = bool.Parse(reader["BSupported"].ToString());
            LLabel = bool.Parse(reader["LSupported"].ToString());
         }
         else
         {
            throw new Exception("No LabelFormat found for ID " + this.FormatID.ToString());
         }
      }
   }
}
catch (Exception ex)
{
   throw ex;
}
finally
{
   if (dbConn != null)
   {
      try { dbConn.Close(); }
      catch { }
   }
}

导致IndexoutofRangeException的原因是,我的查询只查找v.VersionNumber,而不是我试图检索的所有值,如BLabel和LLabel。

我几乎可以保证这是因为您的一个字段名不正确。调试读卡器并打印出读卡器看到的字段名。它通常是一些愚蠢的东西,比如“StockID”和“StockID”到底是什么。哪一行抛出异常?@Blam BLabel抛出exception@chris.ellis不幸的是,名字是正确的。我应该注意的?没有任何信息表明VersionNumber是f.*中的一列。您是否按照第7条评论中的要求发布了架构。Select*就是太草率了。你报告说那不是抛出错误的那条线。Yosi它对其他值很有效,只是没有我刚才添加的LLabel和BLabel。