• Winform程序调用WebService连接数据库心得


        最近做一个项目,必须得和服务器的数据库进行通讯,查了好多资料好像只有socket和WebService可以实现,socket之前完全没有听过,webService还稍微接触过,所以就决定从WebService下手,这个功能现在已经实现了,现在说说具体步骤。

    【引用】

    最初就是建

    1.首先新建Asp.NET Web服务:

    新建——网站——ASP.NET 服务

     

    默认出现以下代码:

    public Service ()

        {

            //如果使用设计的组件,请取消注释以下行

            //InitializeComponent();

        }

     

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

    }

    其中:

    public Service ()

        {

            //如果使用设计的组件,请取消注释以下行

            //InitializeComponent();

        }

    类似于Form程序中的构造函数,

    public string HelloWorld()

        {

            return "Hello World";

    }

    为默认生成的一个Web服务方法。

    2.添加自己的web方法

    第一个为测试连接数据库的方法,第二个为查询数据库数据的方法

    如下:

        [WebMethod]

        public bool TestConnection() //测试数据库连接,如果连接成功则返回True

        {

            try

            {

                string strCon = "Data Source=(local);Initial Catalog=MyTestDB;Integrated Security=True";

                sqlCon = new SqlConnection(strCon);

                sqlCon.Open();

                bool bl = true;

                return bl;

            }

            catch

            {

                return false;

            }

        }

    (说明:数据库“MyTestDB”为自己建的测试用数据库,用户需输入自己的数据库名称)

     

        [WebMethod]

        public DataSet GetDataSet(string strQuery) //根据传入的一个查询字符串对数据库进行查询

        {

            string strQuery1 = "select * from tbStudents";

            string strCon = "Data Source=(local);Initial Catalog=MyTestDB;Integrated Security=True";

            sqlCon = new SqlConnection(strCon);

            sqlCon.Open();

     

            SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery1,sqlCon);

            DataSet ds = new DataSet();

            dataAdapter.Fill(ds);

            return ds;

        }

    添加了这两个方法之后WebService中的代码总体如下:

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    using System.Data.SqlClient;

    using System.Web.Services.Description;

    using System.Data;

     

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Service : System.Web.Services.WebService

    {

        SqlConnection sqlCon;

     

        public Service ()

        {

            //如果使用设计的组件,请取消注释以下行

            //InitializeComponent();

        }

     

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

        }

     

        [WebMethod]

        public bool TestConnection() //测试数据库连接,如果连接成功则返回True

        {

            try

            {

                string strCon = "Data Source=(local);Initial Catalog=MyTestDB;Integrated Security=True";

                sqlCon = new SqlConnection(strCon);

                sqlCon.Open();

                bool bl = true;

                return bl       }

            catch

            {

                return false;

            }

        }

     

        [WebMethod]

        public DataSet GetDataSet(string strQuery) //根据传入的一个查询字符串对数据库进行查询

        {

            string strQuery1 = "select * from tbStudents";

            string strCon = "Data Source=(local);Initial Catalog=MyTestDB;Integrated Security=True";

            sqlCon = new SqlConnection(strCon);

            sqlCon.Open();

     

            SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery1,sqlCon);

            DataSet ds = new DataSet();

            dataAdapter.Fill(ds);

            return ds;

        }

    }

    2.在WinForm程序中调用Web服务

    首先新建一个Form程序,添加一个按钮用于调用测试数据库连接的方法,一个label用于显示连接状态;另一个按钮用于调用查询方法,一个DataGridView用于显示查询到的数据。

     

     

    然后再项目中添加Web引用

     

    会出现以下界面:

     

      

    在URL中填入在第一步中创建的web服务的地址。(web服务的地址可以通过运行Web服务之后启动的浏览器地址栏中得到。)

    点击“前往”,看是否引用Web服务成功,如果成功会出现以下界面:

     

      

    然后更改Web 引用名(可不更改)并点击“添加引用”,便完成了对Web服务的引用。

    调用Web服务中的方法:

    调用Web服务时类似于调用Form程序中的一个类,都需要实例化,然后调用其方法。

           实例化服务: Service ts = new Service();

    在btnTestService_Click事件中添加以下代码,实现对Web服务中测试数据库方法的调用。

            private void btnTestService_Click(object sender, EventArgs e)

            {

                bool bl = ts.TestConnection();

                lblTestService.Text = bl.ToString();

            }

    在btnQueryData_Click事件中添加以下代码,实现对Web服务中数据查询方法的调用。

            private void btnQueryData_Click(object sender, EventArgs e)

            {

                string strQuery = "select * from tbStudents";

                DataSet ds = ts.GetDataSet(strQuery);

                dataGridView1.DataSource = ds.Tables[0];

            }

     

    添加这些事件之后再Form端总的代码如下:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using UsingWebService.TestService;

    using System.Data.SqlClient;

     

    namespace UsingWebService

    {

        public partial class Form1 : Form

        {

            Service ts = new Service();

     

            public Form1()

            {

                InitializeComponent();

            }           

     

            private void Form1_Load(object sender, EventArgs e)

            {

     

            }

     

            private void btnTestService_Click(object sender, EventArgs e)

            {

                bool bl = ts.TestConnection();

                lblTestService.Text = bl.ToString();

            }

     

            private void btnQueryData_Click(object sender, EventArgs e)

            {

                string strQuery = "select * from tbStudents";

                DataSet ds = ts.GetDataSet(strQuery);

                dataGridView1.DataSource = ds.Tables[0];

            }

        }

    }

    3.测试:

    运行Form程序

     
  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/weiying/p/weiying03_13.html
Copyright © 2020-2023  润新知