首页 > 解决方案 > 从 Sql Server varbinary 字段向 Azure DevOps 添加附件

问题描述

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

标签: azure-devopsattachment

解决方案


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

Stream stream = new MemoryStream(byteArray);

之后,您可以将此数据作为附件上传到这样的工作项(例如错误)(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包 中


推荐阅读