Web服務初探:用Demo學Web服務系列(6)——斷開模式訪問資料庫的Web服務

來源:互聯網
上載者:User
    通過上次的《Web服務初探:用Demo學Web服務系列(5)——串連模式訪問資料庫的Web服務》學習,我們已經知道了,Web Services是如何從資料庫中來使用串連模式訪問資料庫來進行操作。下面我們來看看在上次的討論中所講述WebService再次改變,讓這個WebService能變成斷開模式訪問資料庫的Web Services。
    這次我們要改變上次的WebService時並不是在原來的方法上做改變,而是在WebService中添加了一個新方法,並且在我們建立的C/S程式工程中也加入了一個新的Windows Form來調用這個新加的方法。
    一、在前面的WebService中加入下面的方法,代碼如下: 1    [WebMethod]
 2    public DataSet SelectUser(string UserName)
 3    {
 4        Configuration WebConfig = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
 5        DataSet DS = new DataSet("WSDemoDB");
 6        DataTable DT = new DataTable("UserTable");
 7        DS.Namespace = "http://tempuri.org/DataSet";
 8        DS.Tables.Add(DT);
 9        if (WebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
10        {
11            ConnectionStringSettings ConStr = WebConfig.ConnectionStrings.ConnectionStrings["WSConStringSQL"];
12            if (ConStr != null)
13            {
14                SqlConnection SqlCon = new SqlConnection(ConStr.ConnectionString);
15                SqlCommand SqlCom = new SqlCommand("SELECT 使用者ID, 使用者名稱稱, 使用者密碼, 使用者姓名, 使用者性別, 密碼提示問題, 問題答案 FROM 使用者表 WHERE (使用者名稱稱 LIKE '%' + @使用者名稱稱 + '%')", SqlCon);
16                SqlCom.Parameters.Add("@使用者名稱稱", SqlDbType.NVarChar);
17                SqlCom.Parameters["@使用者名稱稱"].Value = UserName;
18                SqlDataAdapter SqlDA = new SqlDataAdapter(SqlCom);
19                try
20                {
21                    SqlCon.Open();
22                    SqlDA.Fill(DT);
23                }
24                finally
25                {
26                    SqlDA.Dispose();
27                    SqlCom.Dispose();
28                    SqlCon.Close();
29                    SqlCon.Dispose();
30                }
31            }
32        }
33        return DS;
34    }

WebService方法說明:添加的方法名為SelecUser,其中需要獲得一個參數UserName。此方法返回一個根據傳入的UserName查詢到的相關使用者資訊的DataSet。
    二、C/S工程中添加表單並在“查詢”按鈕中加入相關代碼,表單和代碼如下:
1、表單中加入一個TextBox、一個Button和一個DataGridView,如:

2、在其中的“查詢”按鈕下代碼為: 1private void Btn_SelectUser_Click(object sender, EventArgs e)
 2        {
 3            MyServ.MyServiceClass MyWebServ = new MyServ.MyServiceClass();
 4            DataSet DS = new DataSet();
 5            DS = MyWebServ.SelectUser(TB_User.Text);
 6            if (DS.Tables.Count > 0)
 7            {
 8                DGV_UserView.DataSource = DS.Tables["UserTable"];//此處也可寫成:DGV_UserView.DataSource = DS.Tables[0];
 9            }
10            else
11            {
12                MessageBox.Show("沒有查詢到所需要的資料!");
13            }
14        }

這樣我們完成了此次WebService的調用。
代碼說明:跟其他的調用一樣我們需要執行個體化WebService的代理類,然後定義一個DataSet用來接收WebService中SelectUser的傳回值,最後將返回的DataSet綁定到DGV_UserView上。

總結:這次我們看見了WebService傳回值是DataSet,而在Visual Studio.Net2005中建立的Web Services中可以返回DataTable,這個在Visual Studio.Net2003中是不行的,會提示“不能序列化”。至此我們把C/S程式調用Web Services的簡單方法講述完了,下次隨筆中我們講解一些Web Services的原理知識、Soap訊息和XML。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.