Winform program calls WebService to connect to the database

Source: Internet
Author: User

I recently worked on a project and had to communicate with the database of the server. I checked a lot of information and it seems that only socket and WebService can be implemented. I have never heard of socket before, and WebService has been a little touched, so I decided to start With WebService. This function has now been implemented. Now let's talk about the specific steps.

[Reference]

It was initially created

1. First create an ASP. NET web service:

Create -- website -- ASP. NET service

 

The following code is displayed by default:

Public Service ()

{

// If you use the designed component, uncomment the following line

// Initializecomponent ();

}

 

[Webmethod]

Public String helloworld ()

{

Return "Hello World ";

}

 

Where:

Public Service ()

{

// If you use the designed component, uncomment the following line

// Initializecomponent ();

}

Similar to the constructor in the form program,

Public String helloworld ()

{

Return "Hello World ";

}

The default web service method.

 

2. add your own web method

The first is the method for testing database connection, and the second is the method for querying database data.

As follows:

[Webmethod]

Public bool testconnection () // test the database connection. If the connection is successful, true is returned.

{

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;

}

}

(Note: The Database "mytestdb" is your own test database. You need to enter your own database name)

 

[Webmethod]

Public dataset getdataset (string strquery) // query the database based on an input query string

{

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;

}

After these two methods are added, the code in WebService is as follows:

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 ()

{

// If you use the designed component, uncomment the following line

// Initializecomponent ();

}

 

[Webmethod]

Public String helloworld ()

{

Return "Hello World ";

}

 

[Webmethod]

Public bool testconnection () // test the database connection. If the connection is successful, true is returned.

{

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) // query the database based on an input query string

{

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. Call the Web service in the winform Program

First, create a form program and add a button to call the method for testing the database connection. One label is used to display the connection status. The other is used to call the query method, and the other is used to display the queried data.

 

 

Then add a web reference to the project.

 

 

The following page is displayed:

 

Enter the URL of the web service created in step 1. (The web service address can be obtained through the address bar of the browser started after running the web service .)

Click "go" to check whether the Web Service is successfully referenced. If yes, the following page appears:

 

Then, modify the Web reference name (do not change) and click "add reference" to complete the Web service reference.

 

Call methods in Web Services:

When you call a web service, it is similar to calling a class in the form program. You must instantiate the class and then call its method.

Instantiation service: Service Ts = new service ();

Add the following code to the btntestservice_click event to call the test database method in the Web service.

Private void btntestservice_click (Object sender, eventargs E)

{

Bool BL = ts. testconnection ();

Lbltestservice. Text = bl. tostring ();

}

Add the following code to the btnquerydata_click event to call the Data Query Method in the Web service.

Private void btnquerydata_click (Object sender, eventargs E)

{

String strquery = "select * From tbstudents ";

Dataset DS = ts. getdataset (strquery );

Datagridview1.datasource = Ds. Tables [0];

}

 

After adding these events, the overall code on the Form side is as follows:

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. test:

Run the form program

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.