使用XML Web Service返回DataTable对象的问题

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體:   |    |    |  超大  

/* * structural elements */ .KBPUBV2 { margin: 0px 4px 4px 8px; display:inline-block; FONT-FAMILY: verdana, arial, helvetica, sans-serif; FONT-SIZE: 100%; }.kbRightCol, .kbRightNav { width:165px; float:right; position:static; padding: 0px 8px 0px 16px; margin: 8px 8px 0px 12px; font-size:100%; }.kbRightCol A, .kbRightNav A { font-weight: normal; }.kbRightCol A:visited, .kbRightNav A:visited { color: Purple; }.KBTT .dec, .KBPUBV2 .dec { list-style-type: decimal; }.KBTT .alpha, .KBPUBV2 .alpha { list-style-type: lower-alpha; }.KBTT UL, .KBPUBV2 UL { list-style-type:disc }.KBTT LI, .KBPUBV2 LI { margin-left: -0.5em; }.KBTT OL LI, .KBPUBV2 OL LI { line-height: 120%; margin-bottom: 1px; margin-left: -0.2em; }.KBTT UL LI, .KBPUBV2 UL LI { line-height: 120%; margin-bottom: 1px; margin-left: -0.5em; }.KBTT UL UL, .KBPUBV2 UL UL { font-size: 100%; list-style-type:square; }.KBTT UL UL LI, .KBPUBV2 UL UL LI { font-size: 100%; }.KBTT UL UL UL, .KBPUBV2 UL UL UL { font-size: 100%; list-style-type:circle; }.KBTT UL UL UL UL, .KBPUBV2 UL UL UL UL { font-size: 100%; list-style-type:square; }.appliesto {font-weight:bold; width: 99%; border-top: solid 1px #cccccc; padding-top: 1em; margin-top: 2em; }.appliesto H3 { font-family:Verdana, Helvetica; font-size:110%; }.appliesto UL { font-weight:normal; margin-top: 0.5em;}.appliesto UL UL { font-size: 100%; margin-top: 0em; margin-bottom: 0.5em; }.appliesto UL LI { margin-top: 0em; margin-bottom: 0em; }.notice { margin-bottom: 0em; margin-top: 1.4em; padding-top: 4px; font-size: inherit; }.KBTT .indent, .KBPUBV2 .indent { margin-top: 1em; margin-bottom: 1em; margin-left: 2em; }.copyright {}.disclaimer { padding:5px 20px 5px 10px; }.TableData TABLE { margin-top: 1em; margin-bottom: 1em; width: 95%; }.TableData TH { background-color: #cccccc; text-align: center; padding: 4px; border: white 1px solid; font-size: 100%; }.TableData TR { }.TableData TD { background-color: #f4f4fC; padding: 4px; border: white 1px solid; font-size: 100%; }.TABLE.docinfo { WIDTH: 95%; border: none; }.TABLE.docinfo TD { BORDER: none; }.docinfo { margin-top: 12px; }.docinfo TD.data { font-size: 100%; background-color: #f4f4fC; }.docinfo TD.label { background-color: #cccccc; font-weight: bold; font-size: 100%; }.KBPUBV2 IMG.graphic { border-style:none; border-width:0 }.KBPUBV2 .E { margin-top: 0.5em; margin-bottom: 0.3em; display:block; }.KBPUBV2 KBD { font-weight: bold; }/* USERINPUT */ .KBPUBV2 .weboutput { }.KBPUBV2 .weboutput table object { position: static; }.KBPUBV2 .URLLink:hover, .KBPUBV2 .kblink:hover, .KBPUBV2 .UNCLink:hover { CURSOR: hand; COLOR: #ff9900 }.KBPUBV2 .URLLink:link, .KBPUBV2 .kblink:link, .KBPUBV2 .UNCLink:link {COLOR: #0000ff}.KBPUBV2 .URLLink:visited, .KBPUBV2 .kblink:visited, .KBPUBV2 .UNCLink:visited {COLOR: #800080}.KBPUBV2 .URLLink:active, .KBPUBV2 .kblink:active, .KBPUBV2 .UNCLink:active {COLOR: red}.errormsg { margin-top: 12px; margin-bottom: 12px; margin-left: 24px; COLOR: red; display:block; font-weight: normal; }.message { margin-top: 12px; margin-bottom: 12px; margin-left: 24px; }PRE { PADDING-LEFT: 0.5em; PADDING-RIGHT: 0.5em; PADDING-TOP: 0.5em; PADDING-BOTTOM: 0.5em; MARGIN-LEFT: 24px; MARGIN-TOP: 1em; MARGIN-BOTTOM: 1em; COLOR: black; BACKGROUND-COLOR: #eeeeee; width: 100%; FONT-FAMILY: "Courier New", courier, monospace; }PRE.in_text { COLOR: black; FONT-FAMILY: "Courier New", courier, monospace; }H1 { FONT-WEIGHT: bold; FONT-SIZE: 170%; MARGIN-TOP: 6px; MARGIN-LEFT: 0px; TEXT-DECORATION: none }H3 { FONT-WEIGHT: bold; FONT-SIZE: 105%; margin-bottom: 12px; MARGIN-LEFT: 0px; TEXT-DECORATION: none;}H4 { FONT-WEIGHT: bold; FONT-SIZE: 100%; MARGIN-LEFT: 0px; TEXT-DECORATION: none }H5 { FONT-WEIGHT: bold; FONT-SIZE: 95%; MARGIN-LEFT: 0px; TEXT-DECORATION: none }H2 { font-variant: small-caps; FONT-WEIGHT: bold; FONT-SIZE: 110%; font-family: Verdana, Helvetica; COLOR: #CC6600; margin: 8px 12px 6px -12px; padding-top: 8px; }.disclaimer { font-size: 95%; font-weight: 100; }.NOTICE {padding-left: 12px; padding-right: 24px; }.SUMMARY {padding-left: 12px; padding-right: 24px; }.MOREINFORMATION {padding-left: 12px; padding-right: 24px; }.SYMPTOMS {padding-left: 12px; padding-right: 24px; }.CAUSE {padding-left: 12px; padding-right: 24px; }.RESOLUTION {padding-left: 12px; padding-right: 24px; }.STATUS {padding-left: 12px; padding-right: 24px; }.WORKAROUND {padding-left: 12px; padding-right: 24px; }.REFERENCES {padding-left: 12px; padding-right: 24px; }.CAUSE {padding-left: 12px; padding-right: 24px; }.WHATTOTRY {padding-left: 12px; padding-right: 24px; }.ERRORMESSAGE {padding-left: 12px; padding-right: 24px; }.internal .head { font-weight:bold; text-align: center; background-color: white; color: red; border-top-style: solid }p.internal { margin-left: 5%; margin-right: 5% }.internal .tail { background-color: white; color: red; border-bottom-style: solid }.partner .head { font-weight:bold; text-align: center; background-color: white; color: #FFAA00; border-top-style: solid }p.partner { margin-left: 5%; margin-right: 5% }.partner .tail { background-color: white; color: #FFAA00; border-bottom-style: solid }/*** For KB-Subtypes ***/ .KBSC, .KBQA, .KBTT, .KBSI { display:inline-block; margin: 0px 8px 4px 6px; FONT-FAMILY: verdana, arial, helvetica, sans-serif; }.KBSI { margin: 0px 0px 4px 0px; }.SectionImage { width:110px; float:right; position:static; padding-left:8px; margin: 4px 10px 8px 4px; }.dropCap { font-size: x-large; }.qaText { padding-top: 9px; }.blurb { padding: 0px 10px 10px 5px; }.g_graphic { border-left: solid 1px #999999; padding: 0px 4px 0px 4px; }.b_graphic { border-right: solid 1px #999999; padding: 0px 4px 0px 4px; }.kbRightCol { float:none; }.KBSI H1 { margin-left : 10px }.KBSI H2 { font-variant: small-caps; FONT-WEIGHT: bold; FONT-SIZE: 110%; font-family: Verdana, Helvetica; COLOR: #CC6600; margin: 10px 2px 6px 0px; padding-top: 4px; }.kbQA .Summary {padding-left: 12px; padding-right: 24px; margin-top:0px; border-top: solid 1px #999999;}/************/ 第一次翻译文档,如有问题敬请原谅并指教!

英文原文

This article was previously published under Q306134 问题在Visual Studio .Net集成开发环境中试图添加到这个XML Web Service的引用时,也会看到下面类似的错误信息:

System.Data.DataRelation cannot be serialized because it does not have a default public constructor. 在Visual Studio .Net集成开发环境中试图添加到这个XML Web Service的引用时,也会看到下面类似的错误信息:

Internal Server Error. Unable to request "http://localhost/Webservice1/Service1.asmx?WSDL". The server responded with error code "ProtocolError". 原因DataTable、 DataRow、 DataView以及 DataViewManager 等对象不能被序列化也不能被从一个XML XML Web service返回。要返回,至少必须是一个完整的 DataSet ,因此必须把要返回的数据复制到一个 DataSet中。 解决解决这个问题的方法是:返回一个 DataSet 以替代 DataTable。一个 DataSet 对象中可以包含一个或多个 DataTable对象。 状态本行为正在设计中。 'Visual Basic <WebMethod()> Function GiveMeADataTable() As System.Data.DataTable Dim conn As New System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;") Dim ds As New System.Data.DataSet() Dim adapter As New System.Data.SqlClient.SqlDataAdapter() adapter.SelectCommand = New System.Data.SqlClient.SqlCommand("Select * From Authors", conn) adapter.Fill(ds, "Authors") Return ds.Tables("Authors") End Function

//Visual C# [WebMethod] public System.Data.DataTable GiveMeADataTable() { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;"); System.Data.DataSet ds = new System.Data.DataSet(); System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(); adapter.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From Authors", conn); adapter.Fill(ds, "Authors"); return ds.Tables["Authors"]; }

/**Visual J# */ /** @attribute WebMethod() */ public System.Data.DataTable GiveMeADataTable(){System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");System.Data.DataSet ds = new System.Data.DataSet();System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();adapter.set_SelectCommand(new System.Data.SqlClient.SqlCommand("Select * From Authors", conn));adapter.Fill(ds, "Authors");System.Data.DataTableCollection dtc = ds.get_Tables();return dtc.get_Item("Authors");}

编译这个XML Web service.。 浏览你添加了上面的方法的XML Web Service的URL,你将收到错误信息。 修改为如下的代码,使用 DataSet 对象替换 DataTable对象:'Visual Basic <WebMethod()> Function GiveMeADataSet() As System.Data.DataSet Dim conn As New System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;") Dim ds As New System.Data.DataSet() Dim adapter As New System.Data.SqlClient.SqlDataAdapter() adapter.SelectCommand = New System.Data.SqlClient.SqlCommand("Select * From Authors", conn) adapter.Fill(ds, "Authors") Return ds End Function

//Visual C# [WebMethod] public System.Data.DataSet GiveMeADataSet() { System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;"); System.Data.DataSet ds = new System.Data.DataSet(); System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(); adapter.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From Authors", conn); adapter.Fill(ds, "Authors"); return ds; }

/**Visual J# */ /** @attribute WebMethod() */ public System.Data.DataSet GiveMeADataSet(){System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Server=YourServer;Initial Catalog=pubs;Integrated Security=SSPI;");System.Data.DataSet ds = new System.Data.DataSet();System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();adapter.set_SelectCommand(new System.Data.SqlClient.SqlCommand("Select * From Authors", conn));adapter.Fill(ds, "Authors");return ds;}

编译这个XML Web Service。 再浏览这个XML Web Service方法的URL连接,将不会看到上面的错误提示。本文档信息应用于:Microsoft Web Services (包括.NET Framework) 1.0 Microsoft Web Services (包括.NET Framework 1.1) Microsoft Visual Basic .NET (2002) Microsoft Visual C# .NET (2002) Microsoft ASP.NET (包括.NET Framework) 1.0 Microsoft Visual J# .NET (2002) Microsoft Visual J# .NET (2003) Microsoft Visual C# .NET (2003) Microsoft Visual Basic .NET (2003) Microsoft ASP.NET (包括.NET Framework 1.1)

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航