Use WebService weather interface to get weather automatically and write to database regularly

Source: Internet
Author: User

Because I am also a rookie, so try to write simple .....

1. The weather interface used is http://www.webxml.com.cn/WebServices/WeatherWebService.asmx, although it is free, but there is a limit to the number of uses

2. Development is using the C # console program in VS2012, which needs to be added as a Web reference, yes, the address is the URL above

3. Reference method: Right-click Project, add Service reference, lower left corner advanced options, add a Web reference, for example, URL is an interface address, Web reference name fill in whatever you like, I'm filling in weather.

4. Introducing the Service namespace

Using

5. Call the service, get the weather data, because the return is an array, so we need to choose our own needs of data, I need is the day of the weather profile.

New Weather.weatherwebservice ();                 string New string [a];                 = S.getweatherbycityname (" Shanghai ");                 string todayweather = str[6// get a real-time weather profile for the day

6. Because you are at work, have no time to write, directly put out the full code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingGetweater.weather;usingSystem.Threading;usingsystem.timers;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Collections;namespacegetweater{classProgram {Private Static voidSay (stringtxt)        {Console.WriteLine (TXT); }        Static voidMain (string[] args) {             while(true) {mytask (); intt = +* -* -* -; Thread.Sleep (t); //time period milliseconds            }        }        Static voidMyTask () {SqlConnection conn=NewSqlConnection ("server=.; Database=weather;uid=sa;pwd=sa"); Try{weather.weatherwebservice s=NewWeather.weatherwebservice (); string[] str =New string[ A]; STR= S.getweatherbycityname ("Shanghai"); stringTodayweather = str[6]; Conn.                Open (); Console.WriteLine ("Database Connection Successful! \ n")                stringsql ="INSERT INTO weathermsg (Content) Values ('"+ Todayweather +"';SqlCommand sqlcom =NewSqlCommand (SQL, conn); Sqlcom.                                        ExecuteNonQuery (); Console.WriteLine ("Data updated successfully! \ n"); Conn.                Close (); Console.WriteLine ("Close the database connection! \ n"); }            Catch(Exception a) {Console.WriteLine (a.tostring ()); }        }    }}

First, you are welcome to criticize!

Use WebService weather interface to get weather automatically and write to database regularly

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.