我将尝试将二进制数据转换为其原始格式".PDF",但是我现有的两个解决方案都出现了问题。第一个解决方案创建了一个PDF文件,但该文件为空。第二个解决方案也创建了一个PDF文件,但我无法打开它。错误在哪里?
第一个代码:
第二段代码:
第一个代码:
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();
string s = Encoding.UTF8.GetString(binaryData);
File.WriteAllText("algo.pdf", s);
第二段代码:
Conn.Open();
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandText = "Select Artigo From Artigo WHERE (IDArtigo ='" + id + "')";
byte[] binaryData = (byte[])cmd.ExecuteScalar();
// Convert the binary input into Base64 UUEncoded output.
string base64String;
try
{
base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}
catch (System.ArgumentNullException)
{
MessageBox.Show("Binary data array is null.");
return;
}
cmd.CommandText = "Select Titulo From Artigo WHERE (IDArtigo ='" + id + "')";
string titulo = (string)cmd.ExecuteScalar();
// Write the UUEncoded version to the output file.
System.IO.StreamWriter outFile;
try
{
outFile = new StreamWriter(titulo + ".pdf", false, System.Text.Encoding.ASCII);
outFile.Write(base64String);
outFile.Close();
}
catch (System.Exception exp)
{
System.Console.WriteLine("{0}", exp.Message);
}