我正在使用一项功能来将pdf或word文件上载和下载到SQL Server中。我在下载Blob文件并将其另存为Word文件到PC时遇到问题。
我试图单独下载文件而不进行转换,因此它可以正常工作。但是我无法将其另存为普通的word或pdf文件。
string constring = "datasource=localhost;database=swiftdb;username=root;password=;SslMode=none;";
MySqlConnection con = new MySqlConnection(constring);
FileStream fs; // Writes the BLOB to a file (*.bmp).
BinaryWriter bw; // Streams the BLOB to the FileStream object.
int bufferSize = 100;
byte[] outbyte = new byte[bufferSize];
long retval;
long startIndex = 0;
string id = "";
int c = 0; ;
MySqlCommand cmd = new MySqlCommand("Select Resume from swiftdb.employee where Employeeid=@id", con);
id = dataGridView1.CurrentRow.Cells[5].Value.ToString();
cmd.Parameters.AddWithValue("id",id );
con.Open();
MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
while(myReader.Read())
{
c++;
fs = new FileStream("Resume" + id + ".pdf", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs);
// Reset the starting byte for the new BLOB.
startIndex = 0;
// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
MessageBox.Show(c.ToString());
// Continue reading and writing while there are bytes 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();
con.Close();
我收到错误消息:“索引超出了数组的范围。” 在“ retval = myReader.GetBytes(1,startIndex,outbyte,0,bufferSize);”。