首页 > 解决方案 > Excel 电源查询、回退连接/OleDb 数据源的错误处理

问题描述

我有一个连接到 DB2 数据源的 PowerQuery,但是由于某种负载平衡,DB 服务器会定期更改(ip 也会更改),我无法事先知道哪个数据源 (IP),我应该使用直到我尝试一下,看看它出错了,然后我必须使用另一个,我在 PowerQuery 中搜索错误处理并找到了一些错误处理示例,但这个示例不适用于我的情况,其中大多数在连接后处理错误已创建或正在防止丢失列或未找到文件的错误,我尝试根据我的情况调整示例但无法做到。

我想要的只是尝试一个 IP,如果失败,则使用另一个 IP。

let

//fParametros("ParamQuery",1) is the "standard" Server/Ip address (provider=IBMDADB2.IBMDBCL1;data source=CP3;location=pn8us7ldbcp3.us.mycompany.com:5912)

dbSource = fParametros("ParamQuery",1),

//fParametros("ParamQuery",5) is the "Alternate" Server/Ip address (provider=IBMDADB2.IBMDBCL1;data source=CP3;location=pn8us7ldbcp3h.us.mycompany.com:5912)

AltdbSource = fParametros("ParamQuery",5),
pOrden = Text.From(fParametros("ParamQuery",2)),

//Create the query
dbQuery = "SELECT SAPCP3.vbak.VBELN SO , SAPCP3.vbap.posnr PoLine , SAPCP3.vbep.ETENR Sch_Line , SAPCP3.vbap.matnr Part_Number,SAPCP3.makt.maktx Description,SAPCP3.vbap.kwmeng Qty ,SAPCP3.vbep.BMENG Conf_qty ,SAPCP3.vbap.vrkme UOM ,SAPCP3.vbap.netpr SalesPrice ,SAPCP3.vbap.kpein LotSize FROM SAPCP3.vbak JOIN SAPCP3.vbap ON SAPCP3.VBAp.VBELN = SAPCP3.VBAK.VBELN JOIN SAPCP3.vbep ON SAPCP3.vbep.vbeln = SAPCP3.vbak.vbeln AND SAPCP3.vbap.posnr  = SAPCP3.vbep.posnr JOIN sapcp3.makt ON sapcp3.vbap.matnr=sapcp3.makt.matnr WHERE SAPCP3.VBAK.VKORG = '4000' AND (SAPCP3.vbep.edatu >= '20190701') AND SAPCP3.vbak.VBELN ="& pOrden & " ORDER BY SAPCP3.vbak.VBELN",

//Get the data
Source = OleDb.DataSource(dbSource, [Query=dbQuery]),

//Failed Attempt to handle the error:    

TestForError= try Source,

//next line does not work, I get error saying Source is already defined/declared
Source = if TestForError[HasError] then OleDb.DataSource(AltdbSource, [Query=dbQuery]) else OleDb.DataSource(dbSource, [Query=dbQuery])

in

Source

我还尝试了以下方法:

.
.
.

//Get the data
Source = OleDb.DataSource(dbSource, [Query=dbQuery]),

//Failed Attempt to handle the error:
TestForError= try Source,

Output = if TestForError[HasError] then OleDb.DataSource(AltdbSource, [Query=dbQuery]) else OleDb.DataSource(dbSource, [Query=dbQuery])

in

Output


//This last part works if the dbSource is correct, but if it is not it doesnt catch the error and gives me the Connection error shown below:

DataSource.Error: OLE DB: SQL30081N 检测到通信错误。正在使用的通信协议:“TCP/IP”。正在使用的通信 API:“SOCKETS”。检测到错误的位置:“172.16.0.1”。检测错误的通信功能:“连接”。协议特定错误代码:“10061”、“ ”、“ ”。SQLSTATE=08001 详细信息:DataSourceKind=OleDb DataSourcePath=data source=CP3;location=pn8us7ldbcp3.us.mycompany.com:5912;provider=IBMDADB2.IBMDBCL1 Message=SQL30081N 检测到通信错误。正在使用的通信协议:“TCP/IP”。正在使用的通信 API:“SOCKETS”。检测到错误的位置:“172.16.0.1”。检测错误的通信功能:“连接”。协议特定错误代码:“10061”、“ ”、“".SQLSTATE=08001 错误代码=-2147467259

我想连接到 dbSource (172.16.0.1),如果无法连接到 AltdbSource (172.16.0.2)

目前我有 2 个相同的 excel 表,如果用户遇到连接错误,需要打开一个或另一个,我希望它自动更改。

标签: error-handlingdb2powerqueryoledbconnectionm

解决方案


我猜这TestForError = try OleDb.DataSource(BadSource)不起作用,因为OleDb.DataSource(...)返回一个表值,而不是在尝试枚举行时产生错误。

如果您更改TestForError为钻入第一行(如果存在)怎么办:

TestForError= try Source{0}?,

如果这仍然不起作用,接下来尝试索引到第一行中的表格单元格。


推荐阅读