private void Add(int message_id, AttachmentClass attach) { using (Entities e = new Entities()) { ATTACHMENTS _attach = attach.GetAttahmentDB(); _attach.MESSAGE_ID = message_id; e.ATTACHMENTS.Add(_attach); e.SaveChanges(); } }
private void btnAdd_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog { Filter = "所有文件|*.*", Multiselect = true }; if (dialog.ShowDialog() == DialogResult.OK) { ITableAttachments attachments = (ITableAttachments)this.Object.Class; IAttachmentManager attachmentManager = attachments.AttachmentManager; ListViewItem item = null; string[] items = new string[2]; foreach (string str in dialog.FileNames) { IMemoryBlobStream stream = new MemoryBlobStreamClass(); stream.LoadFromFile(str); string fileName = Path.GetFileName(str); AttachmentClass class2 = new AttachmentClass { ContentType = this.GetType(str), Data = stream, Name = fileName }; IAttachment attachment = class2; items[0] = attachment.Name; items[1] = attachment.Size.ToString(); item = new ListViewItem(items) { Tag = attachment }; this.listView1.Items.Add(item); } } }
private void CreateAttachTable(IFeatureClass pFeatureClass,int pID,string pFilePath,string pFileType) { //要素表是否有附件表,数据库只能是10版本的 ITableAttachments pTableAtt = pFeatureClass as ITableAttachments; if (pTableAtt.HasAttachments == false) { pTableAtt.AddAttachments(); } //获取附件管理器 IAttachmentManager pAttachmentManager = pTableAtt.AttachmentManager; //用二进制流读取数据 IMemoryBlobStream pMemoryBlobStream = new MemoryBlobStreamClass(); pMemoryBlobStream.LoadFromFile(pFilePath); //创建一个附件 IAttachment pAttachment = new AttachmentClass(); pAttachment.ContentType=pFileType; pAttachment.Name = System.IO.Path.GetFileName(pFilePath); pAttachment.Data = pMemoryBlobStream; //添加到表中 pAttachmentManager.AddAttachment(pID, pAttachment); }