在 ado.net 中的单个连接中执行reader并执行非查询
下面的代码片段在同一连接字符串中使用了多个查询。首先,我从数据库中选择一些数据,然后在关闭数据读取器(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在连接字符串中设置 MultipleActiveResultSets=True 。
请参阅 MSDN
You need to set MultipleActiveResultSets=True in the connection string.
See MSDN
您必须使用
DataSet
类 -SqlDataAdapter
和DataTable
从数据库检索结果。You have to use
DataSet
class -SqlDataAdapter
andDataTable
to retrieve result from the database.