发布网友 发布时间:2022-04-29 01:14
共2个回答
懂视网 时间:2022-05-02 08:54
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新建及连接数据库
标签:tostring false uri on() box ticket 摘要 ajax ring
热心网友 时间:2022-05-02 06:02
如果两个服务是不同的服务器,数据库服务器需要开启对应的*访问端口并进行设置。如果是在同一个服务器上,使用数据库连接程序、账号密码即可连接。