如何使用linq to sql将文档上传到Sql server数据库?

时间:2013-08-15 02:20:19

标签: c# sql-server windows linq linq-to-sql

我在数据库中创建了一个表,名为Workflows,其中包含名为document的二进制字段,如何将文档上传到该字段以供以后使用....

OpenFileDialog ofd = new OpenFileDialog();
RapidWorkflowDataContext context = new RapidWorkflowDataContext();
private void buttonOpen_Click(object sender, EventArgs e)
{
    if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName.Length > 0)
    {

    }
}

1 个答案:

答案 0 :(得分:0)

在不知道Workflows表的架构的情况下,它会是这样的:

Stream myStream = openFileDialog1.OpenFile();
if (myStream != null)
{
    using (myStream)
    {
        Workflows w = new Workflows();

        byte[] bytes = new byte[myStream.Length];
        myStream.Read(bytes, 0, (int)myStream.Length);

        // change here to your actual field name
        w.FileData = bytes;

        // change here according to your context
        context.Workflows.Add(w);
        context.SubmitChanges();
    }
}