Web サービス
基本
code.msdn.microsoft.com
DataSetにて取得
Web サービスで DataTable を送信
サンプルコード
サービスのコード
<summary>
</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(Person p)
{
return "Hello World" + " " + p.Name + " " + p.BirthYear;
}
[WebMethod]
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("result");
dt.Columns.Add("A", Type.GetType("System.String"));
dt.Columns.Add("B", Type.GetType("System.Int32"));
dt.Columns.Add("C", Type.GetType("System.DateTime"));
for (int i = 0; i < 4; i++)
{
DataRow row = dt.NewRow();
row["A"] = "文字列" + i.ToString();
row["B"] = i;
row["C"] = DateTime.Now;
dt.Rows.Add(row);
}
ds.Tables.Add(dt);
return ds;
}
}
クライアントのコード
private void btnGet_Click(object sender, EventArgs e)
{
WebServiceClientTest.ServiceReference1.WebService1SoapClient c = new WebService1SoapClient();
Person p = new Person();
p.Name = "aio";
p.BirthYear = "2017/2/1";
string r = c.HelloWorld(p);
MessageBox.Show(r);
string result = string.Empty;
DataSet ds = c.GetDataSet();
DataTable dt= ds.Tables["result"];
foreach (DataRow dr in dt.Rows)
{
result += dr["A"].ToString() + "/" + dr["B"].ToString() + "/" + dr["C"].ToString() + Environment.NewLine;
}
MessageBox.Show(result);
}
xml version="1.0" encoding="utf-8"
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<systemserviceModel>
<bindings>
<basicHttpBinding>
<binding name="WebService1Soap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:57875/WebService1.asmx" binding="basicHttpBinding"
bindingConfiguration="WebService1Soap" contract="ServiceReference1.WebService1Soap"
name="WebService1Soap" />
</client>
</systemserviceModel>
</configuration>