相关资料: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 服务”。
实例代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6 using System.Web.Services.Protocols; 7 using System.Data.OleDb; 8 using System.Data; 9 using System.IO; 10 11 namespace MyDataService 12 { 13 /// <summary> 14 /// WebService1 的摘要说明 15 /// </summary> 16 [WebService(Namespace = "http://tempuri.org/")] 17 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 18 [System.ComponentModel.ToolboxItem(false)] 19 // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 20 // [System.Web.Script.Services.ScriptService] 21 public class WebService1 : System.Web.Services.WebService 22 { 23 24 [WebMethod] 25 public string HelloWorld() 26 { 27 return "Hello World"; 28 } 29 30 [WebMethod] 31 public int Add(int x, int y) 32 { 33 return x + y; 34 } 35 36 [WebMethod] 37 public DataSet SelectSQL() 38 { 39 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\MyData.mdb"); 40 OleDbCommand cmd = new OleDbCommand("select * from usesr", con); 41 OleDbDataAdapter oda = new OleDbDataAdapter(); 42 DataSet ds = new DataSet(); 43 DataTable dta = new DataTable(); 44 con.Open(); 45 oda = new OleDbDataAdapter(cmd); 46 oda.Fill(ds, "usesr"); 47 return ds; 48 } 49 50 // 51 [WebMethod] 52 public string SelectSQL2() 53 { 54 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\MyData.mdb"); 55 OleDbCommand cmd = new OleDbCommand("select * from usesr", con); 56 OleDbDataAdapter oda = new OleDbDataAdapter(); 57 DataSet ds = new DataSet(); 58 DataTable dta = new DataTable(); 59 con.Open(); 60 oda = new OleDbDataAdapter(cmd); 61 oda.Fill(ds, "usesr"); 62 // 63 System.Text.StringBuilder strbuilder = new System.Text.StringBuilder(); 64 StringWriter writer = new StringWriter(strbuilder); 65 ds.WriteXml(writer, System.Data.XmlWriteMode.IgnoreSchema); 66 67 return strbuilder.ToString(); 68 69 } 70 [WebMethod] 71 public string ExecSQL(string ASQL) 72 { 73 OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=E:\MyData.mdb"); 74 OleDbCommand cmd = new OleDbCommand(ASQL, con); 75 con.Open(); 76 // 77 int num = Convert.ToInt32(cmd.ExecuteNonQuery()); 78 if (num > 0) 79 { 80 string Astr; 81 Astr = "成功"; 82 return Astr; 83 } 84 else 85 { 86 string Astr; 87 Astr = "失败"; 88 return Astr; 89 } 90 } 91 } 92 }