c#winform更新mysql数据库
我这里有一个代码,每月统计打印的页面:c#winform更新mysql数据库,c#,mysql,winforms,C#,Mysql,Winforms,我这里有一个代码,每月统计打印的页面: public void OnDataReceived(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;//creating object of the class //end receive of data int iR
public void OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;//creating object of the class
//end receive of data
int iReceive = 0 ;
iReceive = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iReceive + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); //creating object for decoding
int charLen = d.GetChars(theSockId.dataBuffer, 0, iReceive, chars, 0);
System.String szData = new System.String(chars);
string test = count_page.Text + szData;
string count_pages = Regex.Replace(test, "[^.0-9]", "");
count_page.Text = count_pages;
dbConnect.Update(count_pages); //problem lines
//as data arrives the bytes are appended to the existing string printer throws data in an ASCII format 1 byte at a time
WaitForData();
}
catch (ObjectDisposedException )
{
System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
}
catch(SocketException se)
{
MessageBox.Show (se.Message ); //gives an error message if any exception has occured
}
}
如果我不将注释所在的行作为问题行放在上面,则此代码可以正常工作:
dbConnect.Update(count_pages);
但是我希望它在数据库中被更新,所以我将计数页面的值传递给类dbConnect.Update。但是,当我添加这一行时。count_pages的值似乎不再显示在文本框中,它也没有将该值传递给数据库无法更新的dbConnect.Update类。有人知道为什么会这样吗 我建议您使用
Trace.WriteLine(计数页数)代码>而不是MessageBox,因为MessageBox会停止您的方法
不要忘记使用System.Diagnostics添加代码>
您可以在输出
窗口中看到结果,您可以在visual studio顶部菜单的视图中使其可见
加:
关于你的新问题,请检查
然后做这样的东西
private static void ReceiveCallback( IAsyncResult ar ) {
try {
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) {
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
new AsyncCallback(ReceiveCallback), state);
} else {
// All the data has arrived; put it in response.
if (state.sb.Length > 1) {
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
}
消息框停止您的方法。你不能那样使用它。真的吗?对不起,我是新手。我实际上只是想测试它,因为我想把值传递给数据库类,比如updateDB(count_pages)。但似乎这个值变成了空的。知道为什么吗?我已经编辑了我的问题。你能看看我真正的问题并给我一些建议吗