Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何插入列表<&燃气轮机;使用C将值转换为SQL二进制字段#_C#_Sql Server 2008 - Fatal编程技术网

C# 如何插入列表<&燃气轮机;使用C将值转换为SQL二进制字段#

C# 如何插入列表<&燃气轮机;使用C将值转换为SQL二进制字段#,c#,sql-server-2008,C#,Sql Server 2008,我对编程并不完全陌生,但我仍然认为自己是个新手。我目前正在创建一个最多有5行项目的发票系统,也就是说,我正在创建一个字符串项目,将其序列化为存储,然后反序列化为显示。 到目前为止,我已经管理了序列化和反序列化,并且从反序列化的值中,我已经在正确的字段中显示了相关信息 我的问题是:如何将String对象中的项列表添加到SQL表中的二进制或XML字段中? 我知道这应该类似于将图像对象添加到二进制文件中,但有一个缺点。通常: byte[] convertToByte(string sourcePath

我对编程并不完全陌生,但我仍然认为自己是个新手。我目前正在创建一个最多有5行项目的发票系统,也就是说,我正在创建一个字符串项目,将其序列化为存储,然后反序列化为显示。 到目前为止,我已经管理了序列化和反序列化,并且从反序列化的值中,我已经在正确的字段中显示了相关信息

我的问题是:如何将String对象中的项列表添加到SQL表中的二进制或XML字段中? 我知道这应该类似于将图像对象添加到二进制文件中,但有一个缺点。通常:

byte[] convertToByte(string sourcePath)
    { 
        //get the byte file size of image
        FileInfo fInfo = new FileInfo(sourcePath);
        long byteSize = fInfo.Length;

        //read the file using file stream
        FileStream fStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);

        //read again as byte using binary reader
        BinaryReader binRead = new BinaryReader(fStream);

        //convert image to byte (already)
        byte[] data = binRead.ReadBytes((int)byteSize);

        return data;
    }
这类操作是针对图像执行的,但是整个“长”操作不适用于列表对象


如果您只是想将数据存储为“可读”文本,则任何帮助都会很有帮助

,您可以使用
varchar(MAX)
nvarchar(MAX)
(取决于您是否需要扩展字符支持)。在ADO.NET或EntityFramework中直接转换为字符串

如果您只需要字符串中的字节,则编码类将执行以下操作:

System.Text.Encoding.Default.GetBytes(您的字符串)


请参阅:

以字符串形式保存二进制文件的一种方法是将图像转换为Base64字符串。这可以通过Convert.ToBase64String(Byte[])方法完成:


现在,您可以将Base64字符串保存在数据库中的字符串字段中。

您是否考虑过创建一个发票项目表,该表具有父发票的外键,并将行项目表示为那样?
string convertImageToBase64(string sourcePath)
{ 
    //get the byte file size of image
    FileInfo fInfo = new FileInfo(sourcePath);
    long byteSize = fInfo.Length;

    //read the file using file stream
    FileStream fStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);

    //read again as byte using binary reader
    BinaryReader binRead = new BinaryReader(fStream);

    //convert image to byte (already)
    byte[] data = binRead.ReadBytes((int)byteSize);

    return Convert.ToBase64String (data);
}