在 ado.net 中的单个连接中执行reader并执行非查询

发布于 2024-12-09 15:25:13 字数 1746 浏览 0 评论 0原文

下面的代码片段在同一连接字符串中使用了多个查询。首先,我从数据库中选择一些数据,然后在关闭数据读取器(dr)之前,我想做一些验证,因此如果满足,我将在 dr 的 while 循环内将这些值插入到数据库中的表中。我收到错误,因为已经有一个打开的数据读取器,所以在关闭它之前我无法使用另一个查询。所以我暂时使用了另一个指向同一个数据库的连接字符串,并使用该连接对象(conn1)执行插入查询。但 wat 是另一种无需使用其他连接字符串即可完成此操作的方法。 任何帮助将不胜感激。谢谢。

public XmlDocument LBS_Offer_Scheduler(string dev_token, float lat, float llong)
    {
        XmlDocument xml_doc = null;
        bool result = false;
        int row_affect = 0;
        int x = (int)Math.Truncate(lat);
        int y = (int)Math.Truncate(llong);

        string qry = "select Store_id,Store_Latitude,Store_Longitude from tbl_FB_Store_Details where Store_Latitude like '"
            + x + "%' and Store_Longitude like '" + y + "%'";
        conn.Open();
        cmd = new SqlCommand(qry, conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Store_Id = dr[0].ToString();
            str_dlat = dr[1].ToString();
            str_dlon = dr[2].ToString();
            double dlat = Convert.ToDouble(str_dlat);
            double dlon = Convert.ToDouble(str_dlon);
            dist_in_sqmts = obj_distance.distance(lat, llong, dlat, dlon);
            id = Convert.ToInt32(Store_Id);
            if (dist_in_sqmts < 200.00)
            {
                string insert_qry = "insert into tbl_FB_Offer_PushNote values('" + dev_token + "', "+ id + ",'" + DateTime.Now + "','N')";
                conn1.Open();
                cmd = new SqlCommand(insert_qry, conn1);
                row_affect = cmd.ExecuteNonQuery();
                conn1.Close();
            }
        }
        conn.Close();
        if (row_affect > 0)
            result = true;
        xml_doc = x_doc.result(result);
        return xml_doc;
    }

The below snippet employs multiple queries inside the same connection string. first i'm selecting the some datas from the DB then before closing the datareader(dr) i would like to do some validations so if satisfies i ll insert those value into the table in the DB within the while loop of dr. There i get the error as there's already an open datareader so i cannot use another query before closing it. so i ve temporarily used another connection string pointing to the same database and i do the insert qry using that connection object(conn1). but wat is the other way to do this without using another connection string.
Any help ll be greatly appreciated. Thx.

public XmlDocument LBS_Offer_Scheduler(string dev_token, float lat, float llong)
    {
        XmlDocument xml_doc = null;
        bool result = false;
        int row_affect = 0;
        int x = (int)Math.Truncate(lat);
        int y = (int)Math.Truncate(llong);

        string qry = "select Store_id,Store_Latitude,Store_Longitude from tbl_FB_Store_Details where Store_Latitude like '"
            + x + "%' and Store_Longitude like '" + y + "%'";
        conn.Open();
        cmd = new SqlCommand(qry, conn);
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Store_Id = dr[0].ToString();
            str_dlat = dr[1].ToString();
            str_dlon = dr[2].ToString();
            double dlat = Convert.ToDouble(str_dlat);
            double dlon = Convert.ToDouble(str_dlon);
            dist_in_sqmts = obj_distance.distance(lat, llong, dlat, dlon);
            id = Convert.ToInt32(Store_Id);
            if (dist_in_sqmts < 200.00)
            {
                string insert_qry = "insert into tbl_FB_Offer_PushNote values('" + dev_token + "', "+ id + ",'" + DateTime.Now + "','N')";
                conn1.Open();
                cmd = new SqlCommand(insert_qry, conn1);
                row_affect = cmd.ExecuteNonQuery();
                conn1.Close();
            }
        }
        conn.Close();
        if (row_affect > 0)
            result = true;
        xml_doc = x_doc.result(result);
        return xml_doc;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国际总奸 2024-12-16 15:25:14

您需要在连接字符串中设置 MultipleActiveResultSets=True 。
请参阅 MSDN

You need to set MultipleActiveResultSets=True in the connection string.
See MSDN

羁拥 2024-12-16 15:25:14

您必须使用 DataSet 类 - SqlDataAdapterDataTable 从数据库检索结果。

string qry = "select Store_id,Store_Latitude,Store_Longitude
      from tbl_FB_Store_Details where Store_Latitude 
            like @x and Store_Longitude like @y";
SqlCommand cmd=new SqlCommand(qry,conn);
cmd.Parameters.AddWithValue("@x", x + "%");
cmd.Parameters.AddWithValue("@y", y + "%");

SqlDataAdapter adp=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
adp.Fill(dt);

foreach(DataRow dr in dt.Rows)
 {
     Store_Id = dr[0].ToString();
     str_dlat = dr[1].ToString();
     str_dlon = dr[2].ToString();
     double dlat = Convert.ToDouble(str_dlat);
     double dlon = Convert.ToDouble(str_dlon);
     dist_in_sqmts = obj_distance.distance(lat, llong, dlat, dlon);
     id = Convert.ToInt32(Store_Id);
     if (dist_in_sqmts < 200.00)
      {
           .....
       }
 }

You have to use DataSet class - SqlDataAdapter and DataTable to retrieve result from the database.

string qry = "select Store_id,Store_Latitude,Store_Longitude
      from tbl_FB_Store_Details where Store_Latitude 
            like @x and Store_Longitude like @y";
SqlCommand cmd=new SqlCommand(qry,conn);
cmd.Parameters.AddWithValue("@x", x + "%");
cmd.Parameters.AddWithValue("@y", y + "%");

SqlDataAdapter adp=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
adp.Fill(dt);

foreach(DataRow dr in dt.Rows)
 {
     Store_Id = dr[0].ToString();
     str_dlat = dr[1].ToString();
     str_dlon = dr[2].ToString();
     double dlat = Convert.ToDouble(str_dlat);
     double dlon = Convert.ToDouble(str_dlon);
     dist_in_sqmts = obj_distance.distance(lat, llong, dlat, dlon);
     id = Convert.ToInt32(Store_Id);
     if (dist_in_sqmts < 200.00)
      {
           .....
       }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文