【发布时间】:2011-04-29 08:31:39
【问题描述】:
我正在尝试从数据库中读取二进制文件并使用 c# 在本地磁盘中写入文件。
使用下面的代码...
但是这一行有问题:byte[] fileAsByte = byte.Parse(row["Blob"]);
public static void ReadBlob()
{
int icount = 0;
string FileName;
SqlConnection Mycon = new SqlConnection(Con);
Mycon.Open();
string queryString = "select * from " + TblName;
SqlDataAdapter adapter = new SqlDataAdapter(queryString, Mycon);
DataTable dtBlob = new DataTable();
adapter.Fill(dtBlob);
foreach (DataRow row in dtBlob.Rows)
{
byte[] fileAsByte = byte.Parse(row["Blob"]);
FileName = FilePath + TblName + row["BlobId"].ToString() + FileType;
WriteBlob(fileAsByte, FileName);
}
Mycon.Close();
}
public static void WriteBlob(byte[] buff, string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buff);
bw.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
【问题讨论】:
-
发生了什么?乍一看,唯一的问题是文件名,它将具有行值:“ row["BlobId"].ToString() "。
-
究竟什么是“问题”?我大概可以猜到……但是你得到一个例外吗?如果是这样,请发布整个异常消息。您向我们提供的信息越多,您获得的实际帮助就越多。