C# 并发和多线程期间的MySQL连接异常
我正在从事一个项目,该项目涉及从sim卡读取消息,将它们插入MySQL数据库表,并回复每一条消息。我的sim卡最多可以处理30条信息。以前我所做的是一次读取所有30条信息,将它们插入数据库,同时从sim卡中删除它们,这样新的信息就可以进来。在消息被放入表中后,我一次只接收一条消息并回复它,同时将其从表中删除 最近我尝试了一种使用线程的新方法。现在,这些消息被读入表中,同时被回复。这大大提高了性能并缩短了响应时间,但出现了一些问题。在下面的代码中,在成功连接30-60秒后,C# 并发和多线程期间的MySQL连接异常,c#,.net,mysql,C#,.net,Mysql,我正在从事一个项目,该项目涉及从sim卡读取消息,将它们插入MySQL数据库表,并回复每一条消息。我的sim卡最多可以处理30条信息。以前我所做的是一次读取所有30条信息,将它们插入数据库,同时从sim卡中删除它们,这样新的信息就可以进来。在消息被放入表中后,我一次只接收一条消息并回复它,同时将其从表中删除 最近我尝试了一种使用线程的新方法。现在,这些消息被读入表中,同时被回复。这大大提高了性能并缩短了响应时间,但出现了一些问题。在下面的代码中,在成功连接30-60秒后,connection.o
connection.open()
函数有时会出现异常。异常情况类似于“使用localhost连接到MySQL服务器时出错”
该程序在我的电脑上运行良好,但故障发生在其他电脑上
private void btnReadMessages_Click(object sender, System.EventArgs e)
{
Thread.CurrentThread.Name = "Main";
Thread ReadMsgsThread = new Thread(new ThreadStart(delegate
{
while (true)
{
readMsgs();
}
}));
Thread ReplyMsgsThread = new Thread(new ThreadStart(delegate
{
while (true)
{
replyMsgs();
}
}));
ReadMsgsThread.Start();
Output("Reading Messages..");
Output("");
ReplyMsgsThread.Start();
}
private void readMsgs()
{
String connectionString = "DRIVER={MySQL ODBC 3.51Driver};Database=sms;Server=localhost;UID=hasnat;PWD=123;";
OdbcConnection connection = new OdbcConnection(connectionString);
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();
connection.Open();
try
{
DecodedShortMessage[] messages = comm.ReadMessages(PhoneMessageStatus.All, storage);
if (messages.Length != 0)
{
foreach (DecodedShortMessage message in messages)
{
SmsDeliverPdu data = (SmsDeliverPdu)message.Data;
OdbcCommand command = new OdbcCommand("INSERT INTO smstable (sms,phone) VALUES ('" + data.UserDataText + "','" + data.OriginatingAddress + "')", connection);
command.ExecuteNonQuery();
delMsgAtIndex(message.Index);
}
}
}
catch (Exception ex)
{
ShowException(ex);
}
connection.Close();
Thread.Sleep(5000);
Cursor.Current = Cursors.Default;
}
private void replyMsgs()
{
String connectionString = "DRIVER={MySQL ODBC 3.51 Driver};Database=sms;Server=localhost;UID=hasnat;PWD=123;";
OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();
try
{
OdbcCommand commandSelect = new OdbcCommand("SELECT id,phone,sms FROM smstable", connection);
OdbcDataReader dr = commandSelect.ExecuteReader();
while (dr.Read())
{
System.Threading.Thread.Sleep(5000);
if (dr["sms"].ToString().Length == 13)
{
OdbcCommand commandGetInfo = new OdbcCommand("SELECT nic,name,hospital,accType,date,status FROM patients where nic='"+dr["sms"].ToString()+"';", connection);
OdbcDataReader getInfo = commandGetInfo.ExecuteReader();
if (getInfo.HasRows)
{
while (getInfo.Read())
{
sendMsg(dr["phone"].ToString(), "Name: "+getInfo["name"].ToString() + " Hospital: " + getInfo["hospital"].ToString() + " Accident: " + getInfo["accType"].ToString() + " Date: " + getInfo["date"].ToString() + " Status: " + getInfo["status"].ToString());
OdbcCommand commandDelRepliedSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelRepliedSMS = commandDelRepliedSMS.ExecuteReader();
}
}
else
{
sendMsg(dr["phone"].ToString(), "No Patient Record Found For Provided NIC,Check NIC or Visit Our Website www.emg1122.com For More Detailed Search");
OdbcCommand commandDelNotFoundSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelNotFoundSMS = commandDelNotFoundSMS.ExecuteReader();
}
}
else
{
sendMsg(dr["phone"].ToString(),"NIC Should Be 13 Digits Long And No Dashes or Alphabets");
OdbcCommand commandDelWrongSMS = new OdbcCommand("Delete FROM smstable where id=" + dr["id"].ToString(), connection);
OdbcDataReader drDelWrongSMS = commandDelWrongSMS.ExecuteReader();
}
}
}
catch (Exception ex)
{
ShowException(ex);
}
connection.Close();
}
private void sendMsg(String phno,String reply)
{
Cursor.Current = Cursors.WaitCursor;
try
{
// Send an SMS message
Output("Replying To:{0}", phno);
SmsSubmitPdu pdu;
pdu = new SmsSubmitPdu(reply, phno);
comm.SendMessage(pdu);
Output("Message Replied To {0}", phno);
Output("");
}
catch (Exception ex)
{
ShowException(ex);
}
Cursor.Current = Cursors.Default;
}
private void delMsgAtIndex(int index)
{
Cursor.Current = Cursors.WaitCursor;
string storage = GetMessageStorage();
try
{
// Delete the message with the specified index from storage
comm.DeleteMessage(index, storage);
}
catch (Exception ex)
{
ShowException(ex);
}
Cursor.Current = Cursors.Default;
}
请复制确切的异常消息。错误[HY000][MySQL][ODBC 3.51驱动程序]无法连接到“localhost”(10061)上的MySQL服务器检查这个问题的答案:不,我不认为这里是这样的。程序运行30到60秒都很好。在此期间,mysql表由两个线程访问,没有任何错误或冲突。链接中讨论的问题是连接建立失败,但我这里不是这样