Serializable数据,然后客户端反序列化,存入dataset
再绑定到datagrid
////////////////////////////////////////////////////////////
dataGridView怎么直接绑定数据库的表格?
private DataSet dataset = new DataSet();
private SqlDataAdapter DataAd;
DBhelper.conn.Open();
string sql = "";
DataAd = new SqlDataAdapter(sql, DBhelper.conn);
DataAd.Fill(dataset, "");
dataGridView1.DataSource = dataset.Tables[""];
SqlCommandBuilder builder = new SqlCommandBuilder(DataAd);
DataAd.Update(dataset, "");
DataSet1.Tables[ "表名 "].Select( "ID,NAME,DTIME where NAME= '某值 ' ") //对dataset表进行取值操作
DBhelper.conn.Close();
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
日前做一项目,access数据库,里边包含众多类似数据库,要求datagridview能够根据用户选择绑定指定数据库:
using System.IO;
using System.Diagnostics;
using System.Data.OleDb;
1.把所有的表列举到treeview控件中
treeView1.Nodes.Clear();
//list all datatables
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + cn_str.get_con_string + ";Persist Security Info=False");
connection.Open();
DataTable dtSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });
for (int i = 0; i < dtSchema.Rows.Count; i++)
{
TreeNode newnode1 = new TreeNode();
newnode1.Text = dtSchema.Rows[i]["TABLE_NAME"].ToString();
//add to the treenode
treeView1.Nodes.Add(newnode1);
}
2.用户选择数据库
if (treeView1.SelectedNode != null)
{
cn_str.set_con_string = treeView1.SelectedNode.Text.ToString();
}
3.绑定数据库
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + cn_str.get_con_string + ";Persist Security Info=False");
connection.Open();
DataSet ds = new DataSet();
string sql = "select * from " + cn_str.set_con_string;
OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
da.Fill(ds);
connection.Close();
GQ_DATA.DataSource = ds.Tables[0];
*其中,cn_str.get_con_string --表示选择的数据库
cn_str.set_con_string --表示选择的表
01.//以下是属于客户端的代码
02.
03.
04.
05.using System;
06.
07.using System.Collections.Generic;
08.
09.using System.Text;
10.
11.using System.IO;
12.
13.using System.Data;
14.
15.using System.Net.Sockets;
16.
17.using System.Data.SqlClient;
18.
19.using System.Runtime.Serialization;
20.
21.using System.Runtime.Serialization.Formatters.Binary;
22.
23.using System.Net;
24.
25.
26.
27.
28.
29.namespace ConsoleApplication1
30.
31.{
32.
33. class Program
34.
35. {
36.
37. public static byte[] GetBinaryFormatDataSet(DataSet ds)
38.
39. {
40.
41. //创建内存流
42.
43. MemoryStream memStream = new MemoryStream();
44.
45. //产生二进制序列化格式
46.
47. IFormatter formatter = new BinaryFormatter();
48.
49. //指定DataSet串行化格式是二进制
50.
51. ds.RemotingFormat = SerializationFormat.Binary;
52.
53. //串行化到内存中
54.
55. formatter.Serialize(memStream, ds);
56.
57. //将DataSet转化成byte[]
58.
59. byte[] binaryResult = memStream.ToArray();
60.
61. //清空和释放内存流
62.
63. memStream.Close();
64.
65. memStream.Dispose();
66.
67. return binaryResult;
68.
69. }
70.
71.
72.
73. static void Main(string[] args)
74.
75. {
76.
77. //
78.
79. // TODO: 在此处添加代码以启动应用程序
80.
81. //
82.
83. SqlConnection conn = new SqlConnection("server=.;database=news;uid=sa;pwd=sa;");
84.
85.
86.
87. conn.Open();
88.
89. String selstr = "select * from newsType";
90.
91. SqlDataAdapter adapter = new SqlDataAdapter(selstr, conn);
92.
93. DataSet ds = new DataSet();
94.
95. adapter.Fill(ds, "phonetab");
96.
97. conn.Close();
98.
99. byte[] input = GetBinaryFormatDataSet(ds);
100.
101.
102.
103.
104.
105. try
106.
107. {
108.
109. //远程的ip地址和端口号
110.
111. IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");
112.
113. IPEndPoint ipep = new IPEndPoint(ip, 3434);
114.
115. Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
116.
117.
118.
119. try
120.
121. {
122.
123. server.Connect(ipep);
124.
125. }
126.
127. catch (Exception e)
128.
129. {
130.
131. Console.WriteLine(e.ToString());
132.
133. }
134.
135. server.Send(input);
136.
137. byte[] response = new byte[1024];
138.
139. int bytesRead = server.Receive(response);
140.
141. Console.WriteLine(Encoding.ASCII.GetString(response, 0, bytesRead));
142.
143. server.Shutdown(SocketShutdown.Both);
144.
145. server.Close();
146.
147. }
148.
149. catch (Exception e)
150.
151. {
152.
153. Console.WriteLine(e.ToString());
154.
155. }
156.
157. Console.ReadLine();
158.
159. }
160.
161. }
162.
163.}
164.
165.
166.
167.
168.
169.//一下是属于服务器端的代码
170.
171.
172.
173.using System;
174.
175.using System.Collections.Generic;
176.
177.using System.Text;
178.
179.using System.IO;
180.
181.using System.Data;
182.
183.using System.Net.Sockets;
184.
185.using System.Data.SqlClient;
186.
187.using System.Runtime.Serialization;
188.
189.using System.Runtime.Serialization.Formatters.Binary;
190.
191.using System.Net;
192.
193.namespace ConsoleApplication2
194.
195.{
196.
197. class Program
198.
199. {
200.
201. public static DataSet ds;
202.
203. public static DataSet RetrieveDataSet(byte[] binaryData)
204.
205. {
206.
207. //创建内存流
208.
209. MemoryStream memStream = new MemoryStream(binaryData);
210.
211. memStream.Seek(0, SeekOrigin.Begin);
212.
213. //产生二进制序列化格式
214.
215. IFormatter formatter = new BinaryFormatter();
216.
217. //反串行化到内存中
218.
219. object obj = formatter.Deserialize(memStream);
220.
221. //类型检验
222.
223. if (obj is DataSet)
224.
225. {
226.
227. DataSet dataSetResult = (DataSet)obj;
228.
229. return dataSetResult;
230.
231. }
232.
233. else
234.
235. {
236.
237. return null;
238.
239. }