datareader返回空参数?

时间:2011-06-23 18:40:22

标签: c# asp.net sql-server visual-studio oop

我正在使用select where field.table =“+ foo; query,并且不返回任何数据。对于原因感到困惑。

public  Submission GetSubmissionsByID(string x)
{


      string viewQuery = "SELECT Submission.SubmissionId, Submission.CustId, Submission.BroId, Submission.Coverage, Submission.CurrentCoverage, Submission.PrimEx, Submission.Retention, Submission.EffectiveDate, Submission.Commission, Submission.Premium, Submission.Comments FROM Submission WHERE Submission.SubmissionId =" + x;
      string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
      using (SqlConnection conn = new SqlConnection(connectionString))
      {
          conn.Open();

          SqlCommand viewCmd = new SqlCommand(viewQuery, conn);
          SqlDataReader dr = null;
          dr = viewCmd.ExecuteReader();
          Submission tempSubmission = new Submission();

          tempSubmission.SubmissionId1 = dr.GetInt32(0);
          tempSubmission.CustomerId1 = dr.GetInt32(1);
          tempSubmission.BrokerId1 = dr.GetInt32(2);
          tempSubmission.Coverage1 = dr.GetInt32(3);
          tempSubmission.CurrentCoverage1 = dr.GetInt32(4);
          tempSubmission.PrimEx1 = dr.GetInt32(5);
          tempSubmission.Retention1 = dr.GetInt32(6);
          tempSubmission.EffectiveDate1 = dr.GetDateTime(7);
          tempSubmission.Commission1 = dr.GetInt32(8);
          tempSubmission.Premium1 = dr.GetInt32(9);
          tempSubmission.Comment1 = dr.GetString(10);

          return tempSubmission;
      }
} 

在这里调用,查询值X有效,并在该行上有数据。 x也设置为常量/有效值也失败。

string x = Request.QueryString["SubmissionId"];

    SubmissionService ss = new SubmissionService();
    Submission sub = ss.getSubmissionByID(x);

1 个答案:

答案 0 :(得分:3)

您必须致电Read()以前往第一条记录。

      dr = viewCmd.ExecuteReader();
      if( dr.Read())
      {
        Submission tempSubmission = new Submission();

        tempSubmission.SubmissionId1 = dr.GetInt32(0);
        tempSubmission.CustomerId1 = dr.GetInt32(1);
        tempSubmission.BrokerId1 = dr.GetInt32(2);
        tempSubmission.Coverage1 = dr.GetInt32(3);
        tempSubmission.CurrentCoverage1 = dr.GetInt32(4);
        tempSubmission.PrimEx1 = dr.GetInt32(5);
        tempSubmission.Retention1 = dr.GetInt32(6);
        tempSubmission.EffectiveDate1 = dr.GetDateTime(7);
        tempSubmission.Commission1 = dr.GetInt32(8);
        tempSubmission.Premium1 = dr.GetInt32(9);
        tempSubmission.Comment1 = dr.GetString(10);

        return tempSubmission;
      }
      return null;