首页 > 代码库 > WebServices新建及连接数据库
WebServices新建及连接数据库
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新建及连接数据库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。