日笔记--C# 从数据库取表格到DataGridView---json传输
程序员文章站
2022-05-13 21:52:22
只作为个人学习笔记。 ......
只作为个人学习笔记。
class OpData { // 创建一个和客户端通信的套接字 Socket socketwatch = null; //连接Access字符串 string strCon; OleDbConnection myCon; public TcpListener link(NetChgbCientr.Form1.Updata ud) { //定义一个套接字用于监听客户端发来的消息,包含三个参数(IP4寻址协议,流式连接,Tcp协议) socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //服务端发送信息需要一个IP地址和端口号 IPAddress address = IPAddress.Parse("192.168.1.112"); TcpListener tls = new TcpListener(address, 8550); tls.Start(); Connect(); //连接数据库 ud("服务已启动!\n"); return tls; } //服务启动 - - 连接数据库 public void Connect() { strCon = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = D:\project\NEWPROJECT\NetChgbServer\bin\Debug\Data2.mdb"; //地址改为全路径 //实例化连接数据库对象 myCon = new OleDbConnection(strCon); //开启连接 myCon.Open(); } //查询全部数据json格式传个客户端 public DataSet Query(String strCom) { // String strCom = "SELECT * FROM CHGB"; //实例化数据集 DataSet myDataSet = new DataSet(); OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myCon); myCommand.Fill(myDataSet, "CHGB"); if (myDataSet != null && myDataSet.Tables.Count > 0)//判断得到的是不是null或没有数据的 { return myDataSet; } else { DataSet DS = new DataSet(); return DS; } } }
//业务逻辑 class BllData { TcpListener tls; // 创建一个和客户端通信的套接字 Socket socketwatch = null; OpData Od = new OpData(); Dictionary<string, Socket> clientConnectionItems = new Dictionary<string, Socket> { }; //启动服务器 public void Open(NetChgbCientr.Form1.Updata ud) { tls = Od.link(ud); //负责监听客户端的线程:创建一个监听线程 Thread threadwatch = new Thread(watchconnecting); threadwatch.IsBackground = true; //启动线程 threadwatch.Start(ud); } //查询全部数据--json转换 public void QueryAll() { DataSet ds = new DataSet(); List<CHGB> lis = new List<CHGB>(); String strCom = "SELECT * FROM CHGB"; ds = Od.Query(strCom); //返回查询结果 //判断是不是空数据 if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) { return; } //有数据继续往下执行 for (int i = 0; i < ds.Tables.Count; i++) { foreach (DataRow dts in ds.Tables[i].Rows) { CHGB ch = new CHGB(); ch.JH1 = dts[0].ToString(); ch.QSJS1 = dts[1].ToString(); ch.ZZJS1 = dts[2].ToString(); ch.HD1 = dts[3].ToString(); ch.SB1 = dts[4].ToString(); ch.SND1 = dts[5].ToString(); ch.CZ1 = dts[6].ToString(); ch.HD21 = dts[7].ToString(); ch.JSJG1 = dts[8].ToString(); lis.Add(ch); } } string ja = JsonConvert.SerializeObject(lis); //json序列化,直接将取出来的表格拆分成对象存入集合中,在用下面的方法序列化 this.Send(ja); } //监听客户端发来的请求 public void watchconnecting(object ud) { //tcpClient是socket的封装(tcpclient.client()方法可以得到socket TcpClient remoteClient = null; //持续不断监听客户端发来的请求 while (true) { try { remoteClient = tls.AcceptTcpClient(); } catch (Exception ex) { //提示套接字监听异常 MessageBox.Show(ex.Message); break; } //获取客户端的IP和端口号 IPAddress IP = (remoteClient.Client.RemoteEndPoint as IPEndPoint).Address; int Port = (remoteClient.Client.RemoteEndPoint as IPEndPoint).Port; //让客户显示"连接成功的"的信息 string sendmsg = "连接服务端成功!\r\n" + "本地IP:" + IP + ",本地端口" + Port; byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg); remoteClient.Client.Send(arrSendMsg); //给外面也弄个套接字 socketwatch = remoteClient.Client; //客户端网络结点号 string remoteEndPoint = remoteClient.Client.RemoteEndPoint.ToString(); //显示与客户端连接情况 String str = "成功与" + remoteEndPoint + "客户端建立连接!\t\n"; //添加客户端信息 NetChgbCientr.Form1.Updata ifu = (NetChgbCientr.Form1.Updata)ud; ifu(str); clientConnectionItems.Add(remoteEndPoint, remoteClient.Client); //IPEndPoint netpoint = new IPEndPoint(clientIP,clientPort); IPEndPoint netpoint = remoteClient.Client.RemoteEndPoint as IPEndPoint; ArrayList li = new ArrayList(); li.Add(remoteClient.Client); li.Add(ud); //创建一个通信线程 收 ParameterizedThreadStart pts = new ParameterizedThreadStart(recv); Thread thread = new Thread(pts); //设置为后台线程,随着主线程退出而退出 thread.IsBackground = true; //启动线程 thread.Start(li); } } // 接收客户端发来的信息,客户端套接字对象 public void recv(object li) { Socket socketServer = (li as ArrayList)[0] as Socket; while (true) { //创建一个内存缓冲区,其大小为1024*1024字节 即1M byte[] arrServerRecMsg = new byte[1024 * 1024]; //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 try { //获取长度判断请求 int length = socketServer.Receive(arrServerRecMsg); switch (length) { case 1: this.QueryAll(); break; case 2: MessageBox.Show("还没写 显示一条"); break; } //将机器接受到的字节数组转换为人可以读懂的字符串 //string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); //将发送的字符串信息附加到文本框txtMsg上 //NetChgbServer.Form1.Updata inc = (li as ArrayList)[1] as NetChgbServer.Form1.Updata; //inc("客户端:" + socketServer.RemoteEndPoint + ",time:" + GetCurrentTime() + "\r\n" + strSRecMsg + "\r\n\n"); } catch (Exception ex) { clientConnectionItems.Remove(socketServer.RemoteEndPoint.ToString()); MessageBox.Show("Client Count:" + clientConnectionItems.Count); //提示套接字监听异常 MessageBox.Show("客户端" + socketServer.RemoteEndPoint + "已经中断连接" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n"); //关闭之前accept出来的和客户端进行通信的套接字 socketServer.Close(); break; } } } //发送消息给客服端 public void Send(String Ja) { //将输入的内容字符串转换为机器可以识别的字节数组 byte[] arrClientSendMsg = Encoding.UTF8.GetBytes(Ja); //调用客户端套接字发送字节数组 socketwatch.Send(arrClientSendMsg); } }
public partial class Form1 : Form { public delegate void Updata(string s); ViShow Vs = new ViShow(); public Form1() { InitializeComponent(); }//启服务 private void Open_Click(object sender, EventArgs e) { Updata ud = new Updata(UpdateLabel2); Vs.ViOpen(ud); } private void text1_TextChanged(object sender, EventArgs e) { } //主线程外访问控件 private void UpdateLabel2(String str) { if (text1.InvokeRequired) { // 当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它 Action<string> actionDelegate = (x) => { text1.AppendText(x.ToString()); }; // 或者 // Action<string> actionDelegate = delegate(string txt) { this.label2.Text = txt; }; text1.Invoke(actionDelegate, str); } else { text1.AppendText(str); } } }
public partial class Form1 : Form { Thread threadclient = null; Socket socketclient = null; int sign; public Form1() { InitializeComponent();; } //DataShow 容器 private void DataShow_CellContentClick(object sender, DataGridViewCellEventArgs e) { } //显示全部数据按钮 ShowAll private void button1_Click(object sender, EventArgs e) { sign = 1; //将输入的内容字符串转换为机器可以识别的字节数组 byte[] arrClientSendMsg = Encoding.UTF8.GetBytes("1"); //调用客户端套接字发送字节数组 socketclient.Send(arrClientSendMsg); } //连接服务器 private void butt2_Click(object sender, EventArgs e) { //定义一个套接字监听 socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //获取文本框中的IP地址 IPAddress address = IPAddress.Parse("192.168.1.112"); //将获取的IP地址和端口号绑定在网络节点上 IPEndPoint point = new IPEndPoint(address, 8550); try { //客户端套接字连接到网络节点上,用的是Connect socketclient.Connect(point); sign = 0; } catch (Exception) { MessageBox.Show("连接失败\r\n"); return; } threadclient = new Thread(recv); threadclient.IsBackground = true; threadclient.Start(); } // 接收服务端发来信息的方法 void recv() { //持续监听服务端发来的消息 while (true) { //定义一个1M的内存缓冲区,用于临时性存储接收到的消息 byte[] arrRecvmsg = new byte[1024 * 1024]; //将客户端套接字接收到的数据存入内存缓冲区,并获取长度 int length = socketclient.Receive(arrRecvmsg); //将套接字获取到的字符数组转换为人可以看懂的字符串 string strRevMsg = Encoding.UTF8.GetString(arrRecvmsg, 0, length);
Control.CheckForIllegalCrossThreadCalls = false; //这里不允许这样操作访问控件,这里为了测试方便 if (sign == 0) { MessageBox.Show(strRevMsg); this.butt2.Text = "断开连接"; sign = 1; } else { MessageBox.Show(strRevMsg); List<CHGB> list = JsonConvert.DeserializeObject<List<CHGB>>(strRevMsg); //先给DataSource 一个地址,再把list给DataGridView控件 //不给地址会报 ”引用没有作用到实例上“ DataTable dt = new DataTable(); this.DataShow.DataSource = dt;// list; this.DataShow.DataSource = list; } } }
上一篇: PHP中实现crontab代码分享
下一篇: PHP连接MySQL数据的操作要点