gpt4 book ai didi

c# - 如何以编程方式更新嵌入式 SQL 连接的命令属性

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:54 24 4
gpt4 key购买 nike

使用 c# openxml - 我试图打开一个 excel 文件,绑定(bind)到它的 connection.xml 流,并更新嵌入式 SQL 查询。我能够使用连接/命令节点成功替换单个字符序列,但尝试显式设置命令属性(即 node.Attribute["command"].Value = select * from ....)导致损坏

xmlDoc.Load(wkb.WorkbookPart.ConnectionsPart.GetStream());

csNode = xmlDoc.SelectSingleNode("*/*/*[@connection]");

csNode.Attributes["command"].Value = Regex.Replace(csNode.Attributes["command"].Value, @"\(\[\w*\].\[\w*\].\[\w*\].\[\w*\].*\)", "(" + subQry + ")", RegexOptions.Multiline);

xmlDoc.Save(wkb.WorkbookPart.ConnectionsPart.GetStream());

wkb.Close();

最佳答案

不确定这是否是解决此问题的唯一方法,但我能够通过删除原始 connections.xml 流并创建/附加一个具有正确值的新流到工作簿来更正它。

 //select connections node from loaded xml Excel
csNode = xmlDoc.SelectSingleNode("*/*/*[@connection]");

//store original node values
oldConnValue = csNode.Attributes["connection"].Value;
oldCommValue = csNode.Attributes["command"].Value;

//delete existing ConnectionsPart - to ensure that bleed-over data is not present
wkb.WorkbookPart.DeletePart(wkb.WorkbookPart.ConnectionsPart);

//create a replacement ConnectionsPart
wkb.WorkbookPart.AddNewPart<ConnectionsPart>();

csNode.Attributes["connection"].Value = oldConnValue; //reassign existing connection value
csNode.Attributes["command"].Value = baseQry; //assign new query

//save changes to stream
xmlDoc.Save(wkb.WorkbookPart.ConnectionsPart.GetStream());

关于c# - 如何以编程方式更新嵌入式 SQL 连接的命令属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com