与我的一个datatable---string---datatable类似 我的做法: DataTable dt = (DataTable)dataGrid1.DataSource; string str = null; int rowCount = dt.Rows.Count; int columnCount = dt.Columns.Count; for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { str += (dt.Rows[i][j].ToString() + "-"); } } string[] str = Encoding.Default.GetString(buffer, 0, read).Split(new char[] { '-', '|' }); string[,] strb = new string[str.Length / 5 + 1, 5]; int r = 0, c = 0; for (int i = 0; i < str.Length; i++) { strb[r, c] = str[i]; c++; if ((i + 1) % 5 == 0) { c = 0; ++r; } } 我的笨方法,还没找到更好的方法.寻求
序列化: Dim msgByte() As Byte Dim tb As New DataTable Dim bf As New BinaryFormatter Dim stream As New MemoryStream bf.Serialize(stream, tb) msgByte = stream.ToArray() 反序列化: Dim msg() As Byte = receivingUdpClient.Receive(RemoteIpEndPoint) Dim dt as new DataTable Dim stream As MemoryStream = New MemoryStream(msg) Dim bf As New BinaryFormatter dt = bf.Deserialize(stream) '收到消息
请参见:
BinaryFormatter 类
http://www.microsoft.com/china/MSDN/library/archives/library/dndotnet/html/objserializ.asp
我的做法:
DataTable dt = (DataTable)dataGrid1.DataSource;
string str = null;
int rowCount = dt.Rows.Count;
int columnCount = dt.Columns.Count;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < columnCount; j++)
{
str += (dt.Rows[i][j].ToString() + "-");
}
}
string[] str = Encoding.Default.GetString(buffer, 0, read).Split(new char[] { '-', '|' });
string[,] strb = new string[str.Length / 5 + 1, 5];
int r = 0, c = 0;
for (int i = 0; i < str.Length; i++)
{
strb[r, c] = str[i];
c++;
if ((i + 1) % 5 == 0)
{
c = 0;
++r;
}
} 我的笨方法,还没找到更好的方法.寻求
Dim msgByte() As Byte
Dim tb As New DataTable
Dim bf As New BinaryFormatter
Dim stream As New MemoryStream
bf.Serialize(stream, tb)
msgByte = stream.ToArray()
反序列化:
Dim msg() As Byte = receivingUdpClient.Receive(RemoteIpEndPoint)
Dim dt as new DataTable
Dim stream As MemoryStream = New MemoryStream(msg)
Dim bf As New BinaryFormatter
dt = bf.Deserialize(stream) '收到消息