[英]Webservice+consumer c# error Server was unable to process request. ---> The connection is already open
i have a webservice and a comsumer, the webservice has its methode where it returns data from a mysql database. 我有一个web服务和一个comsumer,webservice有它的方法,它从mysql数据库返回数据。 in the comsumer i called 在我打电话的消息中
WebService.Service1 Service = new WebService.Service1();
in the beginning (not within a methode) when the consumer starts asking for data it will be 20 requests within 10 minutes first 15-18 requests worked perfectly but the last few times it returns the error 在开始时(不在一个方法中)当消费者开始询问数据时,它将在10分钟内提出20个请求,首先15-18个请求完美地工作但最后几次它返回错误
Server was unable to process request. 服务器无法处理请求。 ---> The connection is already open. --->连接已经打开。
I hope i provided enough information like this, i rather not post the code. 我希望我提供了这样的足够信息,我宁愿不发布代码。
This is the methode of the webservice: 这是webservice的方法:
public string GetAnswer(string Question, string Option1, string Option2, string Option3, string Option4)
{
string connstring = "Server=Server;Port=3306;Database=DB;UID=User;password=pw;";
MySqlConnection conn = new MySqlConnection(connstring);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT * FROM `tbl` where `Question` = '" + Question + "' LIMIT 1";
conn.Open();
MySqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
string TheAnswer = "";
while (reader.Read())
{
string question = reader["Question"].ToString();
string answer = reader["Answer"].ToString();
if (Option1.Equals(answer))
TheAnswer = Option1;
if (Option2.Equals(answer))
TheAnswer = Option2;
if (Option3.Equals(answer))
TheAnswer = Option3;
if (Option4.Equals(answer))
TheAnswer = Option4;
}
conn.Close();
conn.Dispose();
return TheAnswer;
}
else
{
MySqlCommand command2 = conn.CreateCommand();
command.CommandText = "Insert Into `new` (`Question`, `Answer1`,`Answer2`,`Answer3`,`Answer4`) VALUES ('" + Question + "','" + Option1 + "','" + Option2 + "','" + Option3 + "', '" + Option4.Replace("~", " ") + "')";
conn.Open();
command.ExecuteNonQuery();
conn.Close();
conn.Dispose();
return "Error: Question is unknown, saving the question to get it answered.";
}
}
You have conn.open()
in your else statement, and the connection was already opened before that. 你的else语句中有conn.open()
,之前已经打开了连接。 You should probably consider using the using
statement: 您应该考虑使用using
语句:
using (MySqlConnection conn = new MySqlConnection(connstring))
{
using (MySqlCommand command2 = conn.CreateCommand())
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.