[英]How to save a binary file accessed from a database in C#?
我已經訪問了一個通過ac#查詢存儲員工照片的數據庫。 我的問題是:如何通過ac#程序將照片保存到硬盤上的常規文件中? 要創建文件,我知道我將使用FileMode.Create
以及SaveFileDialog
類?
這是我讀入文件的方式:
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}", reader["emp_id"], reader["first_name"], reader["last_name"], reader["photo"]));
FileStream fs = new FileStream(SaveFileDialog.FileName, FileMode.Open); //something of this nature?
}
}
finally
{
reader.Close();
}
}
我已經從這個SO問題中提取了必要的信息,但是對保存文件並沒有真正的幫助。 因此,我正在尋求有關如何保存要保存在計算機上的二進制文件(即圖片)的幫助。
File.WriteAllBytes
將字節寫入給定名稱的文件。
總的來說,像
File.WriteAllBytes(fileName, (byte[])reader["photo"]);
您可以使用“保存文件”對話框來保存文件。 但這實際上是您的UX設計(和應用程序設計)的問題。 通常,循環和對話框的播放效果不是很好-如果您讓我選擇10個不同的文件名來保存可以選擇的名稱的照片,我會很生您的氣:P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.