在WebService中使用Microsoft.Practices.EnterpriseLibrary.Data配置數據庫


1、 新建WebApplication1項目

     1.1 新建—Web—ASP.NET Empty Web Application--WebApplication1

     1.2 添加一個WebForm1

2、 新建webService項目

     2.1 新建—Web—ASP.NET Empty Web Application—WebApplicationService

     2.2 選擇WebApplicationService—右鍵—Add—WebService (ASMX)-- WebServiceGSS.asmx

     2.3 在項目WebApplicationService中添加引用

         Microsoft.Practices.EnterpriseLibrary.Data.dll(版本4.1)

         Microsoft.Practices.EnterpriseLibrary.Common.dll(版本4.1)

         注意,如果報錯,Microsoft.Practices.EnterpriseLibrary.Common”類型不能實例化之類的,

                  可能解決方法:Microsoft.Practices.ObjectBuilder.dll加到References中就哦啦~~~

         寫服務中的方法,

         文件WebServiceGSS.asmx代碼:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebApplicationService
{
   
    [WebService(Namespace = "http://Mercer.GlobalSwitchingService/2008/09", Name = "GlobalSwitchingService")]
    [System.Web.Services.WebServiceBindingAttribute(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1, EmitConformanceClaims = true, Name = "GlobalSwitchingService")]
    [ToolboxItem(false)]
    public class WebServiceGSS : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description = "Returns a list of ReportServerURL one is for source another is for destination")]
        public DataSet GetReportServerURL(Guid sourceClientID, Guid destinationClientID)
        {
            return GSSDML.GetReportServerURL(sourceClientID, destinationClientID);
        }

        [WebMethod(Description = "Returns a list of Roles")]
        public DataSet GetReportRole()
        {
            return GSSDML.GetRole();
        }
    }
}

文件GSSDML.cs代碼(和WebServiceGSS.asmx分開,條理清楚些)

using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebApplicationService
{
    public class GSSDML
    {
        private const string DB_GSS = "DB_GSS";
        private const string DB_FAS = "DB_FAS";
        public static DataSet GetReportServerURL(Guid sourceClientID, Guid destinationClientID)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            try
            {
                using (DbConnection con = DatabaseFactory.CreateDatabase(DB_GSS).CreateConnection())
                {
                    adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand();
                    adapter.SelectCommand.Connection = con as SqlConnection;
                    adapter.SelectCommand.CommandText = "getStudentTable";
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                    //adapter.SelectCommand.Parameters.Add(new SqlParameter("@SourceClientID", sourceClientID));
                    //adapter.SelectCommand.Parameters.Add(new SqlParameter("@DestinationClientID", destinationClientID));
                    con.Open();
                    adapter.Fill(ds);
                }
            }
            catch
            {

            }
            return ds;
        }


        public static DataSet GetRole()
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            try
            {
                using (DbConnection con = DatabaseFactory.CreateDatabase(DB_FAS).CreateConnection())
                {
                    adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand();
                    adapter.SelectCommand.Connection = con as SqlConnection;
                    adapter.SelectCommand.CommandText = " SELECT * from [dbo].[Role]";
                    adapter.SelectCommand.CommandType = CommandType.Text;
                    con.Open();
                    adapter.Fill(ds);
                }
            }
            catch
            {

            }
            return ds;
        }
    }
}

服務中的Config文件

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <connectionStrings>
    <add name="DB_GSS" connectionString="Database=Test; Server=.; User ID=sa;Password=123;Connection Timeout=90;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>
    <add name="DB_FAS" connectionString="Database=fas; Server=.; User ID=sa;Password=123;Connection Timeout=90;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

</configuration>

 

3、添加WebService引用

4、添加完成后會出現一個Settings.settings和Web References

5、在WebForm1.aspx.cs中寫測試代碼

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            web();
        }

        public void web()
        {
            ServiceWeb.GlobalSwitchingService serviceWeb = new ServiceWeb.GlobalSwitchingService();
            //serviceWeb.Url = "http://localhost:25003/WebServiceGSS.asmx";
            //在setting中配置url
            //serviceWeb.Url = Properties.Settings.Default.GlobalSwitchingService;
            //在config中配置url
            //serviceWeb.Url = System.Configuration.ConfigurationManager.AppSettings["GlobalSwitchingService"];
            string s = serviceWeb.HelloWorld();

            Guid id = new Guid();
            DataSet ds = serviceWeb.GetReportServerURL(id, id);

            DataSet ds2 = serviceWeb.GetReportRole();

        }
    }
}

 

 6、如果想自定義服務的端口號,可以在屬性的web頁設置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM