gpt4 book ai didi

C# winform更新mysql数据库

转载 作者:行者123 更新时间:2023-11-29 13:08:25 25 4
gpt4 key购买 nike

我这里有一个代码,用于计算每月打印的页数:

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);  

enter image description here

但我希望它在数据库中更新,因此我将计数页的值传递给类 dbConnect.Update。但是,当我添加这一行时。 count_pages 的值似乎不再显示在文本框中,并且它也没有将该值传递给数据库无法更新的 dbConnect.Update 类。有谁知道为什么这个haoppen?

最佳答案

我建议您使用 Trace.WriteLine(count_pages); 而不是消息框,因为消息框会停止您的方法。

不要忘记添加using System.Diagnostics;

您可以在output窗口中看到结果,您可以使其在Visual Studio顶部菜单的view中可见。

添加:

关于您的新问题,请查看http://msdn.microsoft.com/en-us/library/bbx2eya8(v=vs.110).aspx并制作类似的东西

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());
}
}

关于C# winform更新mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com