【问题标题】:c# winform update mysql databasec# winform更新mysql数据库
【发布时间】:2014-03-14 07:28:09
【问题描述】:

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

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 类。有谁知道为什么会这样?

【问题讨论】:

  • 消息框停止您的方法。你根本不能那样使用它。
  • 真的吗?对不起,我是新手。我实际上只是想测试它,因为我想将值传递给数据库类,例如 updateDB(count_pages)。但似乎该值变为空。知道为什么吗?

标签: c# mysql winforms


【解决方案1】:

我建议你使用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());
    }
}

【讨论】:

  • 我已经编辑了我的问题。你能看看我真正的问题,请给我建议吗?tq
猜你喜欢
  • 2017-09-14
  • 2014-10-19
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2018-05-08
  • 2015-07-23
  • 1970-01-01
  • 2014-04-11
相关资源
最近更新 更多