ASP.NET多个查询未在循环中执行

时间:2017-04-22 18:41:18

标签: c# asp.net sql-server

我正在尝试在循环中运行多个查询。第一个查询运行正常,因为我在单步执行代码时可以看到它。

然而,第二个查询(在循环内)应该根据第一个查询的值运行。当循环基于该值运行时,它似乎忽略了查询。我放置了一个标签代替查询并显示,所以我相信我打开/关闭我的连接是不正确的。

c#c​​ode:

  protected void Page_Load(object sender, EventArgs e)
    {

        // Get the session of the user
        string staffid = Session["StaffId"].ToString();

        //Proxy on page load to check IsActive Status
        string DefaultConnection = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        SqlConnection myConnection = new SqlConnection(DefaultConnection);
        myConnection.Open();

        //select the userdetail specific to the logged in user using parameterisation 
        string query = "SELECT ProxyStatus.ProxyStatusId, ProxyStatus.FunctionId, ProxyStatus.StartDate, ProxyStatus.EndDate, ProxyStatus.IsActive FROM ProxyStatus INNER JOIN Staff ON Staff.StaffId = ProxyStatus.Proxee WHERE (Staff.StaffId = @StaffId)";

        DateTime thisDay = DateTime.Today;

        SqlCommand myCommand = new SqlCommand(query, myConnection);

        myCommand.Parameters.AddWithValue("@staffid", staffid);
        SqlDataReader rdr = myCommand.ExecuteReader();

        if (rdr.HasRows)
        {

            while (rdr.Read())
            {
                Session["StartDate"] = rdr["StartDate"].ToString();
                Session["EndDate"] = rdr["EndDate"].ToString();
                Session["ProxyStatusId"] = rdr["ProxyStatusId"].ToString();
                Session["FunctionId"] = rdr["FunctionId"].ToString();

                // Get the session of StartDate and endate, use the session value in a query to compare against the current date
                string startdate = Session["StartDate"].ToString();
                string enddate = Session["EndDate"].ToString();
                string proxystatus = Session["ProxyStatusId"].ToString();

                DateTime startdatedata = Convert.ToDateTime(startdate);
                DateTime enddatedata = Convert.ToDateTime(enddate);

                if (startdatedata > thisDay)
                {
                    string DefaultConnection2 = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

                    SqlConnection myConnection2 = new SqlConnection(DefaultConnection2);
                    myConnection2.Open();

                    string query2 = "UPDATE ProxyStatus SET ProxyStatus.IsActive = 'False' WHERE ProxyStatus.ProxyStatusId = @proxystatus";


                    myCommand.Parameters.AddWithValue("@newproxystatus", proxystatusnew); 

                    SqlCommand myCommand2 = new SqlCommand(query2, myConnection2);

                         myCommand2.ExecuteNonQuery();

                }
            } 
        }
       else
         {
            rdr.Close();
         }
    }
}

}

1 个答案:

答案 0 :(得分:2)

不应该是

SqlCommand myCommand2 = new SqlCommand(query2, myConnection2);
myCommand.ExecuteNonQuery();

SqlCommand myCommand2 = new SqlCommand(query2, myConnection2);
myCommand2.ExecuteNonQuery();

代替?第一个" myCommand"仍然会使用" rdr"。