这是一个界面和操作简单的天气查询工具。可以自动根据IP地址获取天气信息;
可以自动定时提醒;
可以显示日历信息;
可以自定义皮肤等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace WeatherSearch
{
/// <summary>
/// 获取城市信息类
/// </summary>
class ClsCityData
{
/// <summary>
/// 获取省份列表
/// </summary>
/// <param name="cbo">要获取数据的组合框</param>
public static void GetProvince(ComboBox cbo)
{
DataTable dt = ProvinceData();
cbo.DataSource = dt;
cbo.ValueMember = dt.Columns[0].ColumnName;
cbo.DisplayMember = dt.Columns[1].ColumnName;
}
/// <summary>
/// 获取城市列表
/// </summary>
/// <param name="cbo">要获取数据的组合框</param>
/// <param name="intProvinceCode">省份编码</param>
public static void GetCity(ComboBox cbo, int intProvinceCode)
{
DataTable dt = CityData(intProvinceCode);
cbo.DataSource = dt;
cbo.ValueMember = dt.Columns[0].ColumnName;
cbo.DisplayMember = dt.Columns[1].ColumnName;
}
/// <summary>
/// 设置空城市列表
/// </summary>
/// <param name="cbo">要设置的组合框</param>
public static void SetEmptyCity(ComboBox cbo)
{
DataTable dt = new DataTable();
dt.Columns.Add("CityCode", typeof(int));
dt.Columns.Add("CityName", typeof(string));
dt.Rows.Add(0, "<请选择城市>");
cbo.DataSource = dt;
cbo.ValueMember = dt.Columns[0].ColumnName;
cbo.DisplayMember = dt.Columns[1].ColumnName;
}
/// <summary>
/// 从数据库获取省份数据
/// </summary>
/// <returns>返回DataTable</returns>
private static DataTable ProvinceData()
{
string strSql = "select FProvinceId,FProvinceName from tblProvince union "
"select top 1 0,'<请选择省份>' from tblProvince";
return RunSql(strSql);
}
/// <summary>
/// 根据省份编码获取城市数据
/// </summary>
/// <param name="intProvinceCode">省份编码</param>
/// <returns>返回DataTable</returns>
private static DataTable CityData(int intProvinceCode)
{
string strSql = "select FCityCode,FCityName from tblCityCode where FProvinceId="
intProvinceCode.ToString()
" union "
"select top 1 0,'<请选择城市>' from tblCityCode";
return RunSql(strSql);
}
/// <summary>
/// 执行SQL语句,得到数据
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>返回DataTable</returns>
private static DataTable RunSql(string strSql)
{
string strConnect = "provider=microsoft.jet.oledb.4.0;data source=" Environment.CurrentDirectory "\\" ClsConfig.CityDataFileName;
OleDbConnection cn = new OleDbConnection(strConnect);
cn.Open();
OleDbCommand cmd = cn.CreateCommand();
cmd.CommandText = strSql;
OleDbDataAdapter dp = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
dp.Fill(ds);
cn.Close();
return ds.Tables[0];
}
/// <summary>
/// 执行SQL语句,得到第一个值
/// </summary>
/// <typeparam name="T">要返回值得类型</typeparam>
/// <param name="strSql">执行的SQL语句</param>
/// <param name="tDefualt">默认值</param>
/// <returns>返回执行结果</returns>
private static T RunSql<T>(string strSql,T tDefualt)
{
T tValue = tDefualt;// default(T);//设置默认值
string strConnect = "provider=microsoft.jet.oledb.4.0;data source=" Environment.CurrentDirectory "\\" ClsConfig.CityDataFileName;
OleDbConnection cn = new OleDbConnection(strConnect);
try
{
cn.Open();
OleDbCommand cmd = cn.CreateCommand();
cmd.CommandText = strSql;
tValue =(T)Convert.ChangeType(cmd.ExecuteScalar(),typeof(T));
cn.Close();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
finally {
if (cn != null) cn.Dispose();
}
return tValue;
}
/// <summary>
/// 通过城市名称获取城市编码
/// </summary>
/// <param name="strCityName">城市名称</param>
/// <returns>(int)返回城市编码</returns>
public static int GetCityCodyByName(string strCityName)
{
//去掉“市”字
if (strCityName.EndsWith("市"))
strCityName = strCityName.Substring(0, strCityName.Length - 1);
string strSql = "select FCityCode from tblCityCode where FCityName='" strCityName "'";
int intCityCode = RunSql<int>(strSql, 0);
return intCityCode;
}
/// <summary>
/// 通过城市代码,得到对应的省份
/// </summary>
/// <param name="intCityCode">城市代码</param>
/// <param name="intDefualt">默认值</param>
/// <returns>(int)省份代码</returns>
public static int GetProvinceCodeByCityCode(int intCityCode,int intDefualt)
{
string strSql = "select FProvinceId from tblCityCode where FCityCode=" intCityCode.ToString();
int intProvince = RunSql<int>(strSql, intDefualt);
return intProvince;
}
/// <summary>
/// 通过城市编码获取省份名称
/// </summary>
/// <param name="intCityCode">城市编码</param>
/// <param name="intDefualt">默认值</param>
/// <returns>返回省份名称</returns>
public static string GetProvinceNameByCityCode(int intCityCode)
{
string strSql = "SELECT tblProvince.FProvinceName "
"FROM tblCityCode INNER JOIN tblProvince ON tblCityCode.FProvinceId = tblProvince.FProvinceId "
"where FCityCode=" intCityCode.ToString();
string strProvinceName = RunSql<string>(strSql, "");
return strProvinceName;
}
/// <summary>
/// 获取对应的图片名
/// </summary>
/// <param name="strWeather">天气描述</param>
/// <param name="blnIsDay">是否是白天</param>
/// <returns>返回图片名</returns>
public static string GetImgName(string strWeather, bool blnIsDay)
{
string strSql;
if (blnIsDay)
{
strSql = "SELECT FDayImgName FROM tblWeather where FWeather='" strWeather "'";
}
else
{
strSql = "SELECT FNightImgName FROM tblWeather where FWeather='" strWeather "'";
}
string strImgName = RunSql<string>(strSql, "");
return strImgName;
}
/// <summary>
/// 通过IP获取城市编码
/// </summary>
/// <returns>返回城市编码(获取不到,则返回空字符串)</returns>
public static string GetCityCodeByIP()
{
//判断是否联网
bool blnIsConnectWeb = false;
blnIsConnectWeb = ClsInterenetState.GetInternetState();
if (!blnIsConnectWeb) return "";//没联网则跳出
//获取外网IP
string strWebIp = ClsInterenetState.WebIpAddress();
if (strWebIp == "") return "";//获取不到外网IP则跳出
//获取IP地址的信息
ClsIpInfo clsIpInfo = new ClsIpInfo(strWebIp);
clsIpInfo.GetIpInfo();
if (clsIpInfo.ErrNum != "0") return "";//获取不到对应的信息则跳出
//返回结果
return ClsCityData.GetCityCodyByName(clsIpInfo.City).ToString();
}
}
}
评论