C#ExecuteReader有行,但结果视图最初带有问号

时间:2018-07-18 14:04:16

标签: c# datareader

ExecuteReader有行,但是最初当我将鼠标悬停并查看结果视图时,它对所有行都有问号。然后,当我第二次回去徘徊时,它说枚举没有结果。 HasRows是正确的。我的输出参数也很好,但select语句的结果集却没有。我正在执行一个sql服务器存储的proc。 SP在Management Studio中执行良好。再一次,我得到的是输出参数,而不是结果集。

internal static CrossWalk Create(string senderId, string ediType)
{
    var connection = ConfigurationManager.ConnectionStrings["caeCustom"];

    DbCommand cmd = new SqlCommand(); 
    cmd.CommandText = "emb_edi_xwalk_select";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.AddParameter("@pSENDER_ID", senderId);
    cmd.AddParameter("@pMAPPING_TYPE", ediType);
    var delegateId = new SqlParameter
    {
       ParameterName = "@pDELEGATE_ID",
       SqlDbType = SqlDbType.Int,
       Direction = ParameterDirection.Output
    };
    cmd.Parameters.Add(delegateId);           

    var crossWalk = new CrossWalk();
    var dbFactory = DbProviderFactories.GetFactory(connection.ProviderName);
    using(var sqlConnection = 
          dbFactory.CreateConnection(connection.ConnectionString))
    {
       cmd.Connection = sqlConnection;

       using (var sqlReader = cmd.ExecuteReader())
       {
           if (sqlReader.HasRows)
           {
               while (sqlReader.Read())
               {
                   //Mapping of AAA values will be AAA03~internal_value --> 
                    loop~external_value
                   //i.e. AAA03~1008 --> 2010EA~51
                   string key;
                   string value;
                   if (sqlReader["element"].ToString() == "AAA03")
                   {
                       key = string.Join("~", 
    sqlReader["element"].ToString(), sqlReader["internal_value"].ToString());
                       value = string.Join("~", sqlReader["loop"].ToString(), 
    sqlReader["external_value"].ToString());
                   }
                   else
                   //Normal xwalk mapping will be loop+element~external_value 
                   --> internal_value
                   //i.e. 2010ANM101~X3 --> 3
                   {
                       key = string.Join("~", sqlReader["loop"] + 
    sqlReader["element"].ToString(), sqlReader["external_value"].ToString());
                       value = sqlReader["internal_value"].ToString();                                
                    }
                   crossWalk._lookups.Add(key, value);
               }
           }
           sqlReader.Close();
       }
       crossWalk.DelegateId = 
       Convert.ToInt32(cmd.Parameters["@pDELEGATE_ID"].Value);
    }
   return crossWalk;
}

0 个答案:

没有答案