[英]Passing XML parameter to a stored procedure via ASMX web service
I have a SQL Server 2005 stored procedure that has a signature like this: 我有一个SQL Server 2005存储过程,其签名如下:
CREATE PROCEDURE [dbo].[ProcTRequest]
@Pxml xml, @ClientCode varchar(10)
AS
BEGIN
...
END
I'm tasked with writing an .asmx
web service that will call this stored procedure. 我的任务是编写一个将调用此存储过程的.asmx
Web服务。 This is where I'm having a block. 这是我有一个块的地方。
If the parameter @Pxml
is specified as string in the C# web method, we get 如果参数@Pxml
在C#web方法中指定为字符串,我们得到
Server was unable to process request. 服务器无法处理请求。
Then tried XmlText
as the type for @Pxml
, which resulted in 然后尝试使用XmlText
作为@Pxml
的类型,结果是
Server was unable to process request. 服务器无法处理请求。 Method MyWebService.MyWebMethod can not be reflected. 方法MyWebService.MyWebMethod无法反映。 There was an error reflecting 'PXml'. 反映'PXml'时出错。 ->; - >; There was an error reflecting type 'System.Xml.XmlText'. 反映类型'System.Xml.XmlText'时出错。 -> System.Xml.XmlText cannot be used as: 'xml element'. - > System.Xml.XmlText不能用作:'xml元素'。
What is the correct way to call this stored procedure from a C# web method? 从C#Web方法调用此存储过程的正确方法是什么? Please note that the @Pxml
parameter will always be a well-formed XML. 请注意, @Pxml
参数将始终是格式良好的XML。
The person that will be calling this web service is using Curl on a Unix system. 将要调用此Web服务的人在Unix系统上使用Curl。
Though the requirement is an ASMX, if this can be implemented via WCF, can you please show me how with a piece of pseudo code? 虽然要求是ASMX,如果可以通过WCF实现,你能告诉我如何使用一段伪代码吗? I'm not familiar with WCF yet. 我还不熟悉WCF。
Please help. 请帮忙。 Thank you. 谢谢。
UPDATE: This is how I call the SP 更新:这就是我打电话给SP的方式
[WebMethod]
public string GetRequestID(string PXml, string clientcode)
{
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader dr = db.ExecuteReader("ProcRequestID", PXml, clientcode))
{
return dr["RequestID"].ToString();
}
}
Couldn't you use a string on the ASMX interface (so the caller send you a string), but inside the ASMX web service, you define the @PXml
parameter on the stored procedure call as SqlDbType.Xml
? 你不能在ASMX接口上使用字符串 (所以调用者发送一个字符串),但在ASMX Web服务中,你将存储过程调用的@PXml
参数定义为SqlDbType.Xml
? I think that ought to work .... 我觉得应该工作......
Something along the lines of : 有点像:
[WebMethod]
public void MyWebMethod(string input, string clientCode)
{
using (SqlConnection conn = new SqlConnection(your-connection-string-here))
using (SqlCommand cmd = new SqlCommand("dbo.ProcTRequest", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Pxml", SqlDbType.Xml);
cmd.Parameters.Add("@ClientCode", SqlDbType.VarChar, 10);
cmd.Parameters["@Pxml"].Value = input;
cmd.Parameters["@ClientCode"].Value = clientCode;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
Since you're just calling the stored proc and not expecting a result set back - use ExecuteNonQuery
instead of ExecuteReader
. 由于您只是调用存储过程而不期望结果集 - 使用ExecuteNonQuery
而不是ExecuteReader
。
Also vitally important : tell ADO.NET that you're calling a stored procedure ! 同样非常重要:告诉ADO.NET你正在调用存储过程 ! That's what this line does: 这就是这条线的作用:
cmd.CommandType = CommandType.StoredProcedure;
Otherwise, ADO.NET by default expects to get full inline SQL statement and might just not be able to parse and execute that .... 否则,默认情况下ADO.NET期望获得完整的内联SQL语句,并且可能无法解析并执行该语句....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.