C# 将 byte[] 数组转换为 DataTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1300043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a byte[] array into DataTable
提问by Ahmad Farid
I saved an object of type DataTable into SQL 2005 database in a field of type varbinary. I want to retrieve it back but I wasn't able to type cast it. This is how i saved it.
我将 DataTable 类型的对象保存到 SQL 2005 数据库中 varbinary 类型的字段中。我想取回它,但我无法输入它。我就是这样保存的。
MemoryStream memStream = new MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(dt);
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Tables(TableName, TableData, QuestionID) VALUES (@TableName, @TableData, @QuestionID)", con))
{
cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = "a new table";
cmd.Parameters.Add("@TableData", SqlDbType.VarBinary,Int32.MaxValue).Value = memStream.GetBuffer();
cmd.Parameters.Add("@QuestionID", SqlDbType.VarChar).Value = "2";
cmd.ExecuteNonQuery();
}
The 'dt' is the DataTable object instance.
'dt' 是 DataTable 对象实例。
采纳答案by MyItchyChin
What your talking about is Binary Serialization and Deserialization. Maybe thiswill help.
您谈论的是二进制序列化和反序列化。也许这会有所帮助。
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
using System.Text;
namespace Serial
{
public class Ser
{
public static byte[] StrToByteArray(string str)
{
UTF8Encoding encoding = new UTF8Encoding ();
return encoding.GetBytes(str);
}
public static string ByteArrayToStr(byte[] barr)
{
UTF8Encoding encoding = new UTF8Encoding ();
return encoding.GetString(barr, 0, barr.Length);
}
public static void Main(String[] args)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BooleanValue", typeof(bool)));
for (int i = 1; i <= 1; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;
dt.Rows.Add(dr);
}
//Serialize
BinaryFormatter bformatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
string s;
bformatter.Serialize(stream, dt);
byte[] b = stream.ToArray();
s = ByteArrayToStr(b);
stream.Close();
dt = null;
//Now deserialise
bformatter = new BinaryFormatter();
byte[] d;
d = StrToByteArray(s);
stream = new MemoryStream(d);
dt = (DataTable)bformatter.Deserialize(stream);
stream.Close();
}
}
}
回答by Lasse V. Karlsen
I'm afraid I'll have to disappoint you, at least until you tell us that you've created an extension method for the StreamWriter class that handles data tables.
恐怕我得让你失望了,至少在你告诉我们你已经为处理数据表的 StreamWriter 类创建了一个扩展方法之前。
The only overload of the Write
method that accepts a DataTable instance is the one that takes an object, and per the MSDN documentation, it only saves the "text representation" of the object.
Write
接受 DataTable 实例的方法的唯一重载是接受对象的方法,并且根据MSDN 文档,它只保存对象的“文本表示”。
So, let's hope that the .ToString method of DataTable outputs a string in a format that contains all the contents of the DataTable instance, but alas. The .ToString method only returns the contents of the TableName property, and a display expression, if one is present.
所以,让我们希望 DataTable 的 .ToString 方法以包含 DataTable 实例的所有内容的格式输出一个字符串,但唉。.ToString 方法仅返回 TableName 属性的内容和显示表达式(如果存在)。
So what you've saved is not the contents of the DataTable instance all, only the name of the table.
所以你保存的不是DataTable实例的全部内容,只有表名。
You should look into Serialization, it should be able to produce a binary or XML-representation of all the contents of the DataTable object.
您应该查看Serialization,它应该能够生成 DataTable 对象的所有内容的二进制或 XML 表示。
回答by Daniel Brückner
I assume your code just serialized the data table and you have to deserialize it. I don't know what formatter was used, so you will have to look at the content of you binary field. If it is binary, use the BinaryFormater(see the example code down the page). If it is not binary, try SoapFormatterand XmlSerializer.
我假设你的代码只是序列化了数据表,你必须反序列化它。我不知道使用了什么格式化程序,因此您必须查看二进制字段的内容。如果它是二进制文件,请使用BinaryFormater(请参阅页面下方的示例代码)。如果它不是二进制文件,请尝试SoapFormatter和XmlSerializer。
回答by Michael Todd
You might be better off using XmlSerializer or BinaryFormatter to serialize your DataTable and then store in a binary column.
您最好使用 XmlSerializer 或 BinaryFormatter 来序列化您的 DataTable,然后存储在二进制列中。
Links to do that:
http://sadeveloper.net/forums/p/439/1772.aspx
http://bytes.com/topic/net/answers/428472-serializing-datatable
链接:http:
//sadeveloper.net/forums/p/439/1772.aspx
http://bytes.com/topic/net/answers/428472-serializing-datatable