VisualStudio2010中创建ASP.Net WebService

时间:2022-05-05 15:13:36

相关资料:http://blog.csdn.net/yapingxin/article/details/7331375

具体操作:
1.打开“Microsoft Visual Studio 2010”->“文件”->“新建”->“项目”->“已安装的模板”->“其他语言”->“Visual C#”->“Web”->“ASP.NET 空Web应用程序”。
2.“解决方案”->“MyDataService”工程右击->“增加”->“新建项”->“已安装的模版”->“Visual C#”->“Web”->“Web 服务”。

实例代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.OleDb;
using System.Data;
using System.IO; namespace MyDataService
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public int Add(int x, int y)
{
return x + y;
} [WebMethod]
public DataSet SelectSQL()
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\\MyData.mdb");
OleDbCommand cmd = new OleDbCommand("select * from usesr", con);
OleDbDataAdapter oda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dta = new DataTable();
con.Open();
oda = new OleDbDataAdapter(cmd);
oda.Fill(ds, "usesr");
return ds;
} //
[WebMethod]
public string SelectSQL2()
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\\MyData.mdb");
OleDbCommand cmd = new OleDbCommand("select * from usesr", con);
OleDbDataAdapter oda = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dta = new DataTable();
con.Open();
oda = new OleDbDataAdapter(cmd);
oda.Fill(ds, "usesr");
//
System.Text.StringBuilder strbuilder = new System.Text.StringBuilder();
StringWriter writer = new StringWriter(strbuilder);
ds.WriteXml(writer, System.Data.XmlWriteMode.IgnoreSchema); return strbuilder.ToString(); }
[WebMethod]
public string ExecSQL(string ASQL)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\\MyData.mdb");
OleDbCommand cmd = new OleDbCommand(ASQL, con);
con.Open();
//
int num = Convert.ToInt32(cmd.ExecuteNonQuery());
if (num > )
{
string Astr;
Astr = "成功";
return Astr;
}
else
{
string Astr;
Astr = "失败";
return Astr;
}
}
}
}