WebServices新建及連接數據庫
阿新 • • 發佈:2017-08-07
component urn tid sage type exceptio ron namespace ade
1.新建項目
2.右鍵項目新建
3.Test:
4.OK:
5.連接SQL Server數據庫
<1>ADO方式
添加類DBOperator
using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Data.SqlClient; namespace WebServiceTest { public class DBOperator : IDisposable {public static SqlConnection conn; private string ConnStrig = @"Data Source=.;Initial Catalog=SDTI.TicketDB;Integrated Security=True"; public DBOperator() { if (conn == null) { conn = new SqlConnection(); conn.ConnectionString= ConnStrig; conn.Open(); if (conn.State == ConnectionState.Open) { } } } public List<string> Select() { List<string> list = new List<string>();try { string sql = "select * from table"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader read = cmd.ExecuteReader(); while (read.Read()) { list.Add(read[0].ToString()); list.Add(read[1].ToString()); list.Add(read[2].ToString()); list.Add(read[3].ToString()); } read.Close(); cmd.Dispose(); } catch (Exception e) { string y = e.Message; } return list; } public void Dispose() { if (conn != null) { conn.Close(); conn = null; } } } }
調用:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServiceTest { /// <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 { DBOperator op = new DBOperator(); [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(Description = "Test")] public List<string> Test() { return op.Select(); } } }
<1>EF方式
這裏偷懶了,直接用來自數據庫的EF
新建類DBOperator,SDTIContext
using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data; namespace DBWebService { public class SDTIContext : DbContext { public SDTIContext() : base("SDTIDB") { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace DBWebService { public class DBOperator { SDTIContext db = new SDTIContext(); public List<SDTI_TicketType> GetDB() { return db.Set<SDTI_TicketType>().GroupBy(x => x.TypeNO).Select(x => x.FirstOrDefault()).ToList(); } } }
修改webservice
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace DBWebService { /// <summary> /// WebService 的摘要說明 /// </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 WebService : System.Web.Services.WebService { DBOperator db = new DBOperator(); [WebMethod] public List<SDTI_TicketType> HelloWorld() { return db.GetDB(); } } }
運行測試:
WebServices新建及連接數據庫