mysql varbinary blob_从数据库中读取SQL Varbinary Blob
我正在努力將文件保存到sql blob到varbinary(max)列,并且現在有了保存方面的工作(我相信).
我無法弄清楚如何讀取數據,因為我正在使用存儲過程檢索我的數據庫值我應該能夠訪問列數據,如ds.Tables [0] .Rows [0] [ "blobData"]; 所以我有必要像我在下面的例子中看到的那樣有一個SQLCommand等:
private void OpenFile(string selectedValue)
{
String connStr = "...connStr";
fileName = ddlFiles.GetItemText(ddlFiles.SelectedItem);
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT BLOBData FROM BLOBTest WHERE testid = " + selectedValue;
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
int size = 1024 * 1024;
byte[] buffer = new byte[size];
int readBytes = 0;
int index = 0;
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
while ((readBytes = (int)dr.GetBytes(0, index, buffer, 0, size)) > 0)
{
fs.Write(buffer, 0, readBytes);
index += readBytes;
}
}
}
}
}
}
當我可以在沒有sqlcommand的情況下訪問我需要的列時,有更簡單的方法嗎?
希望我在我的問題中足夠清楚,如果不是那么問我會詳細說明!
更新:
現在情況如此 - 我有我的存儲過程返回的blobData列的值,并且可以將其傳遞到內存流并調用'LoadDocument(memStream); 但是這會導致亂碼文本而不是我實際顯示的文件.
我現在的問題是有沒有辦法獲得完整的路徑,包括存儲在SQL Blob中的文件的文件擴展名?我目前正在考慮使用Filetable,希望我能夠獲得完整的路徑.
更新2:
我嘗試創建一個臨時文件并閱讀這個無濟于事(仍然是胡言亂語)
string fileName = System.IO.Path.GetTempFileName().ToString().Replace(".tmp", fileExt);
using (MemoryStream myMemoryStream = new MemoryStream(blobData, 0, (int)blobData.Length, false, true))
{
using (FileStream myFileStream1 = File.Create(fileName))
{
myMemoryStream.WriteTo(myFileStream1);
myMemoryStream.Flush();
myMemoryStream.Close();
myFileStream1.Flush();
myFileStream1.Close();
FileInfo fi = new FileInfo(fileName);
Process prc = new Process();
prc.StartInfo.FileName = fi.FullName;
prc.Start();
}
}
干杯,H
總結
以上是生活随笔為你收集整理的mysql varbinary blob_从数据库中读取SQL Varbinary Blob的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ext2 源代码解析之 “从路径名到目标
- 下一篇: java解析varbinary_java