Connection and query of SQL databases at the beginning of WCF helloworld at the beginning of WCF

Source: Internet
Author: User

1. First, you must connect to the database. Naturally, you must have a database. Create a database table test1 and insert the data:

---- Use Database testuse test ---- create a table test1create table test1 (tname char (20) primary key not null, tintro varchar (200) not null) ---- insert into test1 (tname, tintro) values ('intel ', 'World's largest CPU and related chip manufacturers') into Table test1 insert into test1 (tname, tintro) values ('Microsoft ', 'name of the world's most famous software vendor, U.S. software giant Microsoft') insert into test1 (tname, tintro) values ('amd ', 'World's second-largest CPU manufacturer ') insert into test1 (tname, tintro) values ('sumyun', 'samsung Korea, a famous color display manufacturer, also produces optical drives, household appliances, and so on ') insert into test1 (tname, tintro) values ('apple', 'apple Inc., known for producing high-performance professional computers ') ---- query test1 content select tname, tintro from test1

2. The following figure shows how to create a WCF Service application. The project name is "connect2sql", for example:

3. then define the WCF Service Agreement in iservice1.cs. The specific code is as follows:

Using system; using system. servicemodel; using system. data; namespace connect2sql {[servicecontract] public interface iservice1 {// open the database [operationcontract] void opensql (); // close the database [operationcontract] void closesql (); // query data [operationcontract] dataset querysql ();}}

4. Implement the WCF Service Agreement in service1.svc. cs. The specific code is as follows:

Using system; using system. servicemodel; using system. data; using system. data. sqlclient; namespace connect2sql {// Note: You can use the "RENAME" command on the "refactor" menu to change the class name "service1" in code, SVC and config file together. public class service1: iservice1 {// connect to the database sqlconnection strcon = new sqlconnection ("Server = sqlserver server name; database = database name; uid = username; Pwd = password "); /// <summary> /// open the database /// </Summary> /// <returns> </returns> Public void opensql () {strcon. open () ;}/// <summary> /// close the database /// </Summary> /// <returns> </returns> Public void closesql () {strcon. close () ;}/// <summary> // query the data in Table test1 /// </Summary> /// <returns> </returns> Public dataset querysql () {try {opensql (); string strsql = "select tname, tintro from test1"; dataset DS = new dataset (); sqldataadapter S = new sqldataadapter (strsql, strcon); S. fill (DS); Return Ds;} catch (exception ex) {Throw ex;} finally {closesql ();}}}}

5. start the service, obtain the service address, create a new Web client named "showdata", and add service references to the Web Client. For details, see helloworld, add a gridview component on the web page. The Code is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "getdata. aspx. cs" inherits = "showdata. getdata" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

6. Write events on the web page and display the queried data to the gridview component. The Code is as follows:

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace showdata {public partial class getdata: system. web. UI. page {// instantiate the host class of the WCF server. service1client SQL = new host. service1client (); protected void page_load (Object sender, eventargs e) {// display the queried data to the gridview component to go to showdata. datasource = SQL. querysql (); showdata. databind ();}}}

 

7. Now you can run it as follows:

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.