我正在尝试从磁盘上传文件,然后将文件插入到 varbinary db 列中。
我似乎无法弄清楚如何插入二进制文件。
我在 WPF 应用程序中使用 C# 和 Linq to Sql。
这是我到目前为止所尝试的!任何建议或意见将不胜感激。
private void UploadFile()
{
DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title = "Select file";
FileData fd = new FileData();
if (dlgOpen.ShowDialog() ?? false)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
//FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".xlsx");
int b;
while ((b = inStream.ReadByte()) > -1)
// outStream.WriteByte((byte)b);
fd.FileId = Guid.NewGuid();
//fd.DataFile = inStream;//DataFile is the Varbinary column in the db
fd.Title = dlgOpen.FileName;
fd.FileExtension = txtExtension.text;
context.FileDatas.InsertOnSubmit(fd);
context.SubmitChanges();
//outStream.Flush();
//outStream.Close();
inStream.Close();
}
}
}