从 Sql Server varbinary 字段将附件添加到 Azure DevOps



有没有办法从SQL SERVER获取VARBINARY字段,并使用C#将其附加到Azure DevOps中的现有错误?

使用 C# 和 ADO.NET,可以将 SQL Server 数据库varbinary字段值读取到byte[],然后可以使用该byte[]创建流

Stream stream = new MemoryStream(byteArray);

之后,您可以将此数据作为附件上传到工作项(例如 bug(,如下所示(1111是工作项 ID,test.pptx只是一个示例,您也应该从数据库中获取它(

.....//ADO.NET to read varbinary field to byte[]
Stream stream = new MemoryStream(byteArray);
    var u = new Uri("https://{org}.visualstudio.com");
                VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "personal access token"));
                var connection = new VssConnection(u, c);
                var workItemTracking = connection.GetClient<WorkItemTrackingHttpClient>();
                JsonPatchDocument jsonPatchOperations = new JsonPatchDocument();

                    var attachmentresult = workItemTracking.CreateAttachmentAsync(stream,fileName:"test.pptx").Result;
                    jsonPatchOperations.Add(new JsonPatchOperation() {
                        Operation=Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add,
                        Path= "/relations/-",
                        Value = new
                        {
                            rel="AttachedFile",
                            url=attachmentresult.Url,
                            attributes = new { comment = "Adding new attachment" }
                        }
                    });
                   var workitemupdated= workItemTracking.UpdateWorkItemAsync(jsonPatchOperations, 1111).Result;

相关引用位于 Microsoft.TeamFoundationServer.ExtendedClient 包中

添加到 bug 工作项的附件是在本地添加的,因此我认为您应该首先将 varbinary 数据下载到本地,然后将其作为附件添加到 bug 工作项。

关于从SQL服务器下载varbinary数据,您可以参考此案例或通过Google。

关于使用 C# 向工作项添加附件,可以参考之前创建的情况。

最新更新