This time I am in the basic system, the examination system 3.0, the evaluation system and the course selection are all directly connected to the database of our basic system. This time we want to optimize the database, but no matter what we change, we have to communicate with several of them to see if they have any influence on them. This is very troublesome. So this time, we decided to create an interface for them to call interfaces to access data and prevent them from directly accessing our data, in this way, we have nothing to do with what we change. Webservice can solve this problem. We can create a webservice. If other systems want our data, we can directly call our webservice. If there is any change, they can directly update it.
Create webservice
1. Add a web service to your website
2. He will have a helloword instance by default, comment out him, and write the interface to be provided. The same as the time writing method, but the [WebMethod] is added in front. Here I added an interface for "querying information". The Code is as follows:
Namespace ceshi1 {// <summary> // summary of WebService1 /// </summary> [WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] [System. componentModel. toolboxItem (false)] // to allow ASP.. net ajax calls this Web service from a script. uncomment the following lines. // [System. web. script. services. scriptService] public class WebService1: System. web. services. webService {// [WebMethod] // public string HelloWorld () // {// return "Hello World "; /// // [WebMethod (Description = "summation method")] // information described by the webservice method // public double Add (double I, double j) // {// return I + j; //} // <summary> // database connection object // </summary> private SqlConnection sqlConn = null; /// <summary> /// DATABASE Command (SqlCommand) // </summary> private SqlCommand sqlCmd = null; # region method for connecting to the database. If the database status is disabled, open the database // <summary> // method to connect to the database. If the database status is closed, open the database // </summary> public SqlConnection GetConn () {if (sqlConn. state = ConnectionState. closed) {// if the database status is Closed, open the database sqlConn. open () ;}return sqlConn ;}# endregion [WebMethod (Description = "")] public DataSet Select () {// defines a string, the method for reading the database from the configuration file. String strConn = ConfigurationManager. connectionStrings ["strConnDB"]. connectionString; // instantiate the database connection object sqlConn = new SqlConnection (strConn); sqlCmd = new SqlCommand ("select * from SetData", GetConn (); SqlDataReader sdr = null; dataTable dtResult = new DataTable (); using (sdr = sqlCmd. executeReader (CommandBehavior. closeConnection) {dtResult. load (sdr); // closes the record set sdr. close ();} // fill the able in the dataset. because datatable does not support serialization of DataSet ds = new DataSet (); ds. tables. add (dtResult); return ds ;}}}
3. After the website is published, you can open the service in the browser. Before the website is published, the browser displays the following interface:
Summary:
It is worth noting that it is best to remember to write the Description of WebMethod, which is the information described by the webservice method. After writing, the caller can better understand the role of this method. Description is the comment of the webservice method.