简体   繁体   English

在现有文本之间将字节数组写入文本文件

[英]Write a byte array to text file between existing text

I have a file(it is a zip file) stored in MSSQL DB in a varbinary field. 我有一个文件(它是一个zip文件)存储在MSSQL DB的varbinary字段中。 I'm reading it from DB into a byte array (Fisier). 我正在从数据库读取它到字节数组(Fisier)。

I want to create a text file that will contain a sql script that can be run on a customer db and update the file on customer server. 我想创建一个文本文件,其中包含一个可以在客户数据库上运行的sql脚本,并在客户服务器上更新该文件。 I'm using something simmilar to update rdlc files, but those files are stored as text, this is binary. 我正在使用类似的东西来更新rdlc文件,但是那些文件存储为文本,这是二进制的。 Do you have any suggestions, how can I do this? 您有什么建议,我该怎么办?

 using (FileStream fs = new FileStream(FilePath, FileMode.Create))
   using (StreamWriter sw = new StreamWriter(fs))
   {
   sw.WriteLine("declare @NewFile varbinary(max)");
   sw.WriteLine("set @NewFile =Convert(varbinary(max),'");

   //this for loop is wrong
   for (int i = 0; i < Fisier.Length; i++) sw.Write((char)Fisier[i]);

   sw.WriteLine("', 2)");

   sw.WriteLine("exec dbo.SetFile");
   sw.WriteLine("@File= @NewFile ,");
}

I have tried also with BinaryWriter but I did't managed to solve it. 我也尝试过BinaryWriter,但没有设法解决。

Ok, I found a solution (not too pretty but it works). 好的,我找到了一个解决方案(虽然不太漂亮,但是可以用)。 Sql allows to define the value using hex notation so I convert each byte to hex representation SQL允许使用十六进制表示法定义值,因此我将每个字节转换为十六进制表示形式

sw.Write("set @FisierNou = 0x");
for (int i = 0; i < Fisier.Length; i++) 
  sw.Write(Fisier[i].ToString("x2"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM