如何将'byte []'类型转换为'System.Data.Linq.Binary'

时间:2013-08-20 00:51:29

标签: c# winforms linq linq-to-sql

我的数据库中有一个WorkflowInstances表,其中包含以下字段:ID(int),Name(nvarchar(50),WorkflowID(int), Document(varbinary(MAX) < /强>))。 我想插入一个新的WorkflowInstance,所以我写了这段代码

Stream myStream = openFileDialogDoc.OpenFile();
            if (myStream != null)
            {
                using (myStream)
                {
                    WorkflowInstance w = new WorkflowInstance();

                    byte[] bytes = new byte[myStream.Length];
                    myStream.Read(bytes, 0, (int)myStream.Length);
                    w.ID = repository.WorkflowsRepository.GetMaxIDWokflowInstance() + 1;
                    w.Name = textBoxWorkflowInstanceName.Text;
                    w.CurrentStateID = repository.WorkflowsRepository.GetWorkflowFirstState((int)listBoxMyWorkflows.SelectedValue);
                    w.WorkflowID = (int)listBoxMyWorkflows.SelectedValue;
                    w.CreationDate = System.DateTime.Now.ToString();
                    w.Document = bytes;
                    RapidWorkflowDataContext context = new RapidWorkflowDataContext();
                    context.WorkflowInstances.InsertOnSubmit(w);
                    context.SubmitChanges();
                }
            }

我在第15行遇到错误,错误是:无法将类型'byte []'隐式转换为'System.Data.Linq.Binary'

1 个答案:

答案 0 :(得分:12)

System.Data.Linq.Binary有一个构造函数带有byte[]的一个参数:

w.Document = new System.Data.Linq.Binary(bytes);