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