这些帖子帮助我解决了一些问题,我在创建 CLR 存储过程时遇到了针对 Infor M3 API 的 Restful API 调用。
这些 API 的 XML 结果如下所示:
miResult xmlns="http://lawson.com/m3/miaccess">
<Program>MMS200MI</Program>
<Transaction>Get</Transaction>
<Metadata>...</Metadata>
<MIRecord>
<RowIndex>0</RowIndex>
<NameValue>
<Name>STAT</Name>
<Value>20</Value>
</NameValue>
<NameValue>
<Name>ITNO</Name>
<Value>ITEM123</Value>
</NameValue>
<NameValue>
<Name>ITDS</Name>
<Value>ITEM DESCRIPTION 123 </Value>
</NameValue>
...
完成从 API 中列出结果集的 CLR C# 代码的工作方式如下所示:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void CallM3API_Test1()
{
SqlPipe pipe_msg = SqlContext.Pipe;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://M3Server.domain.com:12345/m3api-rest/execute/MMS200MI/Get?ITNO=ITEM123");
request.Method = "Get";
request.ContentLength = 0;
request.Credentials = new NetworkCredential("myUserID@domain.com", "MyPassword");
request.ContentType = "application/xml";
request.Accept = "application/xml";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream receiveStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
string strContent = readStream.ReadToEnd();
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(strContent);
try
{
SqlPipe pipe = SqlContext.Pipe;
//Define Output Columns and Max Length of each Column in the Resultset
SqlMetaData[] cols = new SqlMetaData[2];
cols[0] = new SqlMetaData("Name", SqlDbType.NVarChar, 50);
cols[1] = new SqlMetaData("Value", SqlDbType.NVarChar, 120);
SqlDataRecord record = new SqlDataRecord(cols);
pipe.SendResultsStart(record);
XmlNodeList nodeList = xdoc.GetElementsByTagName("NameValue");
//List ALL Output Names + Values
foreach (XmlNode nodeRes in nodeList)
{
record.SetSqlString(0, nodeRes["Name"].InnerText);
record.SetSqlString(1, nodeRes["Value"].InnerText);
pipe.SendResultsRow(record);
}
pipe.SendResultsEnd();
}
catch (Exception ex)
{
SqlContext.Pipe.Send("Error (readStream): " + ex.Message);
}
}
}
}
}
catch (Exception ex)
{
SqlContext.Pipe.Send("Error (CallM3API_Test1): " + ex.Message);
}
}
}
希望这会有所帮助。