简体   繁体   中英

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. I'm reading it from DB into a byte array (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. I'm using something simmilar to update rdlc files, but those files are stored as text, this is binary. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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