我将一些PDF文件保存为SQL Server中的对象(我没有将它们放在此处,我的工作只是检索它们),我想将这些文件另存为PDF。
我目前有这个。它正在保存PDF,但无法打开。它们的大小合适,因此有字节读入其中。我似乎有一个我找不到的小错误。
我将不胜感激任何帮助。
我目前有这个。它正在保存PDF,但无法打开。它们的大小合适,因此有字节读入其中。我似乎有一个我找不到的小错误。
我将不胜感激任何帮助。
C#:
文件流 fs; // Writes the BLOB to a file (*.bmp).
BinaryWriter bw; // Streams the BLOB to the 文件流 object.
int bufferSize = 1000000; // Size of the BLOB buffer.
字节[] outbyte = new 字节[bufferSize]; // The BLOB 字节[] buffer to be filled by GetBytes.
long retval; // The 字节s returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.
string filename= "";
String queryString = "SELECT dbo.DBDocument.DocumentName,dbo.DBDocBlob.Object FROM dbo.DBDocBlob, dbo.DBDocument WHERE dbo.DBDocBlob.DocumentID = dbo.DBDocument.DocumentID";
String ConnectionString = "Server=SERVER_NAME;Database=DB_NAME;Trusted_Connection=True;";
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (myReader.Read())
{
// Get the filename, which must occur before getting the file.
filename = myReader[0].ToString();
// Create a file to hold the output.
fs = new 文件流(filename + ".pdf",
FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
// Reset the starting 字节 for the new BLOB.
startIndex = 0;
// Read the 字节s into outbyte[] and retain the number of 字节s returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
// Continue reading and writing while there are 字节s beyond the size of the buffer.
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
}
// Write the remaining buffer.
bw.Write(outbyte, 0, (int)retval);
bw.Flush();
// Close the output file.
bw.Close();
fs.Close();
}
// Close the reader and the connection.
myReader.Close();
connection.Close();
}