2016年5月30日 星期一

There is already an open DataReader associated with this Command which must be closed first.

這個錯誤訊息常常發生在使用C#的datareader的時候使用同一個connection。
String currentcustomertb = ConfigurationManager.AppSettings["currentcustomertb"];
cmd.CommandText = "SELECT ip,mac FROM " + db +  ".dbo." + currentcustomertb;
SqlDataReader dr = cmd.ExecuteReader();

String replacecurrent = ConfigurationManager.AppSettings["replacecurrent"];

while (dr.Read())
{
 string ip = dr[0].ToString();
 string mac = dr[1].ToString();

 String execReplaceinto = "exec " + db + ".dbo." + replacecurrent + " @cmmac = '" + mac + "' , @cmip = '" + ip + "'";
 SqlCommand cmdexec = new SqlCommand(execReplaceinto, cn);
 cmdexec.ExecuteNonQuery();
 cmdexec.Dispose();
}
因為這個dr與內部的ExecuteNonQuery,所以就會出現這個錯誤訊息。所以有人建議使用不同的connection來避免這個問題。 或是在連線字串加入:MultipleActiveResultSets=True;

沒有留言:

張貼留言

CentOS Python 3.7 安裝方式

有些開發的程式在python 3.8的檔案套件不相容,例如pymssql,等。 所以如果不建置虛擬環境的話才特意安裝3.7版本的python 安裝流程如下 # 先進行yum套件的update yum update -y # 安裝相依性套件 yum install gcc o...