gpt4 book ai didi

sql-server - 如何将 "convert"sql server Blob 字段转换为 "regular"文件?

转载 作者:行者123 更新时间:2023-12-04 01:06:54 25 4
gpt4 key购买 nike

我有一个带有(我假设)PDF 文件的 blob 字段。如何转换得到真正的文件?

最佳答案

刚刚从我以前使用的代码中修改了这个,从代码注释判断它是从其他人那里复制的,但我不确定在哪里。绝对有效,除非我只是为了让它变得通用而把它搞砸了!

private void WriteFiles() 
{

SqlCommand cmd = new SqlCommand("SELECT FileName, FileData FROM ImageFiles", _conn);

FileStream fs; // Writes the BLOB to a file
BinaryWriter bw; // Streams the BLOB to the FileStream object.
int bufferSize = 100; // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes.
long retval; // The bytes returned from GetBytes.
long startIndex = 0; // The starting position in the BLOB output.

// Open the connection and read data into the DataReader.
_conn.Open();
SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

if (!txtPath.Text.EndsWith("\\")) txtPath.Text += "\\";

while (myReader.Read())
{
// Create a file to hold the output.
fs = new FileStream(txtPath.Text + myReader["FileName"].ToString().ToLower(),
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);

// 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();
_conn.Close();

}

关于sql-server - 如何将 "convert"sql server Blob 字段转换为 "regular"文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8357362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com