C# 如何通过 [WebMethod] 返回数据表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10415563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a Datatable by a [WebMethod]
提问by Pomster
I have a webservice that should return the top 5 emails in my inbox and display them in a data grid. I put my data into a DataTablefirst. But keep getting errors
我有一个网络服务,它应该返回收件箱中前 5 封电子邮件并将它们显示在数据网格中。我把我的数据放到了DataTable第一个。但不断收到错误
Here is my code, Am I missing anything or declaring something wrong?
这是我的代码,我是否遗漏了什么或声明了错误?
[WebMethod]
public DataTable DisplayMailList(String inMailServer, String inPort, bool inSSlCheck, String inUsername, String inPassword)
{
objClient.Connect(inMailServer, int.Parse(inPort), inSSlCheck);
objClient.Authenticate(inUsername, inPassword);
int count = objClient.GetMessageCount();
DataTable dtMessages = new DataTable(); // Creating datatable.
dtMessages.Columns.Add("MessageNumber");
dtMessages.Columns.Add("From");
dtMessages.Columns.Add("Subject");
dtMessages.Columns.Add("DateSent");
dtMessages.TableName = "dtMessages";
int counter = 0;
for (int i = count; i >= 1; i--)
{
OpenPop.Mime.Message msg = objClient.GetMessage(i);
dtMessages.Rows.Add();
dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i; //Populateing Datatable
dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = msg.Headers.Subject;
dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = msg.Headers.DateSent;
counter++;
if (counter > 5)
{
break;
}
}
return dtMessages;
}
Think the problem is public DataTableI had it declared as an object but that did not work ether .... sigh, what should I declare it as?
this is the error....
认为问题是公共数据表我将它声明为一个对象,但它不起作用以太......叹息,我应该将它声明为什么?
这是错误....
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
System.InvalidOperationException: 生成 XML 文档时出错。---> System.InvalidOperationException: 无法序列化数据表。未设置数据表名称。
采纳答案by Ivan Karajas
Assigning a value to dtMessages.DataTablename will stop the serialization error, as the error message suggests.
dtMessages.DataTable正如错误消息所暗示的那样,为 name分配一个值将停止序列化错误。
[WebMethod]
public DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1", typeof(string));
dt.Rows.Add("testing");
dt.TableName = "Blah"; // <---
return dt;
}
But I agree with Bob Horn that you're better off defining a class for your return value than using a DataTable.
但我同意 Bob Horn 的观点,即为返回值定义一个类比使用 DataTable 更好。
回答by Mohammed Yasin
This worked for me:
这对我有用:
In your Application
在您的应用程序中
public void app()
{
try
{
DataTable dtInput = new DataTable();
DataRow drRow;
dtInput.Columns.Add("ID");
dtInput.Columns.Add("Name");
drRow = dtInput.NewRow();
drRow["ID"] = 1;
drRow["Name"] = "Star";
dtInput.Rows.Add(drRow);
dtInput.TableName = "Input";//Table name is mandatory to avoid serialization exception
DataTable dtOutput = new DataTable();
dtOutput.TableName = "Output";//Table name is mandatory to avoid serialization exception
service.TestService(dtInput ,ref dtOutput);
}
catch (Exception ex)
{
}
}
In your service
为您服务
DataTable dtOutput= new DataTable();
[WebMethod]
public void TestService(DataTable dtInput , ref DataTable dtOutput)
{
DataRow drRow;
drRow= dtInput.NewRow();
drRow["ID"] = 2;
drRow["Name"] = "Success";
dtInput.Rows.Add(drRow);
dtOutput= dtInput;
}
回答by LifeiSHot
Just give a table name when create a datatable object
创建数据表对象时只需给出表名
DataTable dt = new DataTable("tablename");

