We know that the return value of the C # Web service API can be a dataset type, which allows us to query the database through the Web Service API, which will be the basis of many of our applications. Here's a simple example of a WinForm call to illustrate the problem.
First we have a SQL Server2000 database book on the server Webserviceserver, there is a table BookInfo in the books library, including the title, author, price fields, database user SA, password 123456. We use VS2005 to create a Web service project and publish it, no more details on how to build and publish it, please refer to the relevant information if there is any unclear. I can also refer to my other two articles:
Http://blog.csdn.net/cattiger75/archive/2007/10/17/1828896.aspx (How to create and publish Web SERVICE)
Http://blog.csdn.net/cattiger75/archive/2007/10/19/1832803.aspx (How to invoke the Web Service API in C#winform)
We write an API to query the database in the Service.cs of the project, and to simplify the problem, the API we write only accepts a string parameter, which is used to do the matching query in the book Name section and returns a DataSet that contains a DataTable named BookInfo. , BookInfo contains the title, author, and Price fields, and the code is as follows: using System;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using System.Data;
Using System.Data. SqlClient;
[WebService (Namespace = http://www.mywebservice.com/)]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
public class Service:System.Web.Services.WebService
{
Define a SqlConnection connection member
SqlConnection sqlconn = new SqlConnection ("Server=webserviceserver;initial catalog=book;uid=sa;pwd=123456");
Public Service () {
If you are using a design component, uncomment it to follow the line
InitializeComponent ();
}
[WebMethod]
public string HelloWorld () {
Return "Welcome to use my Web service. " ;
}
[WebMethod]
Public DataSet Searchbook (string bookname)
{
Sqlconn. Open ();
String sql = "Select Top 50 title, author, price from BookInfo where title like '" + bookname + "% ' ORDER by id DESC";
SqlDataAdapter da = new SqlDataAdapter (SQL, sqlconn);
DataSet ds = new DataSet ();
Da. Fill (ds, "BookInfo");
return DS;
}
}
Publish the Web service when you are ready to write.
Now let's build a WinForm Windows Application project and add the Web service we just published to the project as a Web reference.
Create a TextBox control in the Project form to enter the query content, a query button named Btnbooksearch, and a DataGridView control to bind the obtained dataset. Double-click the Query button to write the following event handler function. private void Btnbooksearch_click (object sender, EventArgs e)
{
Define a Web service object, WebReference is the name we take when we add a Web reference
Webreference.service ws = new Cqust.libmanage.WebReference.Service ();
this. Datagridviewbookinfo.autogeneratecolumns = true;
Bind the returned dataset to the DataGridView
this. Datagridviewbookinfo. DataSource = Ws.searchbook (this. textboxsearchstring.text);
Specify the DataTable to display
this. Datagridviewbookinfo.datamember = "BookInfo";
}
Compile the project in parallel, and you will see the results of the query in DataGridView.
Our example is very simple and does not take into account issues such as exception capture, asynchronous invocation, security, but it is very simple to query the database with the Web Service API, and you can implement very complex database queries by extending this simple API.