WinCE下的数据源操作以及天气软件的制作

发布时间:2021-06-05

WinCE下的数据源操作以及天气软件的制作

项目一 数据源显示

实验目标

本实验项目介绍如何利用DataGird控件显示数据库数据 技术要点

DataGird控件的使用,添加SqlServerCe引用,配置数据源 实验步骤

1. 首先,运行visual studio2005,创建名为“数据显示”的智能设备应用程序。 2. 右键单击“数据显示”解决方案,执行“添加引用”,如下图在“.NET”选项卡中选择;“System.Data.SqlServerCe”

3. 在窗体的代码视图中添加

using System.Data.SqlServerCe;

4.在窗体设计视图,打开“数据”菜单,执行“添加数据源…”, 如下图所示,选择“数据库”,单击“下一步”,在新对话框中单击“新建连接…”,在“添加连接”对话框中设置“数据源”为“我的电脑”,“数据库”为“C:\Program

Files\Microsoft Visual Studio 8\SmartDevices\SDK\SQL Server\Mobile\v3.0

WinCE下的数据源操作以及天气软件的制作

\Northwind.sdf”,并测试数据库连接;测试成功后,将数据文件复制到当前工程,并选中数据表。

5.执行“数据”菜单中的显示数据源,将Categories表拖动到窗体中,自动生成categoriesDataGird控件,右键单击数据网格右上角小箭头,执行“生成数据窗体”,设置“dock”属性为“top”

6. 将Products表拖入窗体中,自动生成productsDataGird控件,设置“dock”属性为“bottom”,

WinCE下的数据源操作以及天气软件的制作

项目二 SQLITE数据库操作

实验目标

本实验项目介绍数据库操作的编程方法。 技术要点

查询、插入、删除、更新数据库 实验步骤

1. 首先,运行visual studio2005,创建名为“数据显示”的智能设备应用程序

2. 添加天气源引用:

( ); 3. 安装sqlite,并添加引用; 4. 如图添加窗体控件:

5. 为按钮添加事件:

public partial class Form1 : Form {

WinCE下的数据源操作以及天气软件的制作

public Form1() {

InitializeComponent(); }

string dbPath = "/program files/weatherDB.db";

private string[] array = new string[23]; //数组存放从网络服务器获得的数据 private Bitmap MyImage;

//初始化,禁用按键

private void Form1_Load(object sender, EventArgs e) {

this.button1.Enabled = false; this.button2.Enabled = false; this.button3.Enabled = false; this.bReadDB.Enabled = false;

this.Text = "基于WINCE的天气信息终端"; }

//显示图像

public void ShowMyImage(String fileToDisplay, int xSize, int ySize) {

if (MyImage != null) {

MyImage.Dispose(); }

// 扩展图像到picturebox

pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; MyImage = new Bitmap(fileToDisplay);

pictureBox1.ClientSize = new Size(xSize, ySize); pictureBox1.Image = (Image)MyImage; }

private void callWeatherService() //建立连接,从服务器获取数据 {

string weatherCityName;

weatherCityName = this.textBox1.Text.ToString();

.webxml.www.WeatherWebService weatherWS = new WeatherWebService(); array = weatherWS.getWeatherbyCityName(weatherCityName); }

private void showTodayWeather()//当天天气 {

WinCE下的数据源操作以及天气软件的制作

//#region MyRegion

this.lProvince.Text = array[0]; this.lCity.Text = array[1]; label1.Text = "今日天气实况:"; bel3.Text = array[6]; bel4.Text = array[5]; bel5.Text = array[7];

ShowMyImage("/Program Files/weatherpic/" + array[8], 70, 65);

//#endregion }

private void showTomorrowWeather()//明天天气 {

// #region MyRegion

label1.Text = "明日天气实况:"; bel3.Text = array[13]; bel4.Text = array[12]; bel5.Text = array[14];

ShowMyImage("/Program Files/weatherpic/" + array[15], 70, 65); // #endregion }

private void showDayAfterTomWeather()//后天天气 {

//#region MyRegion

label1.Text = "后天天气实况:"; bel3.Text = array[18]; bel4.Text = array[17]; bel5.Text = array[19];

ShowMyImage("/Program Files/weatherpic/" + array[20], 70, 65); //#endregion }

private void button1_Click(object sender, EventArgs e) {

showTodayWeather(); }

private void button2_Click(object sender, EventArgs e) {

showTomorrowWeather(); }

WinCE下的数据源操作以及天气软件的制作

private void button3_Click(object sender, EventArgs e) {

showDayAfterTomWeather(); }

private void bSetCity1_Click(object sender, EventArgs e) {

this.textBox1.Text = "广州"; this.bSetCity1.Text = "查询中..."; callWeatherService(); showTodayWeather(); getFromSer();

this.bSetCity1.Text = "广州";

this.button1.Enabled = true; this.button2.Enabled = true; this.button3.Enabled = true; //this.bReadDB.Enabled = true; }

private void bSetCity2_Click(object sender, EventArgs e) {

this.textBox1.Text = "中山"; this.bSetCity2.Text = "查询中..."; callWeatherService(); showTodayWeather(); getFromSer();

this.bSetCity2.Text = "中山";

this.button1.Enabled = true; this.button2.Enabled = true; this.button3.Enabled = true; //this.bReadDB.Enabled = true; }

private void bReadDB_Click(object sender, EventArgs e) {

readDB();

this.button1.Enabled = true; this.button2.Enabled = true; this.button3.Enabled = true; }

WinCE下的数据源操作以及天气软件的制作

//---------------------数据库部分

//创建本地数据库

private void setData(string[] hello) {

if (System.IO.File.Exists(dbPath)) System.IO.File.Delete(@dbPath);//删除原文件 if (!System.IO.File.Exists(dbPath)) {

System.Data.SQLite.SQLiteConnection.CreateFile(dbPath);//创建数据库文件

//连接数据库

System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();

conn.ConnectionString = "data source=" + dbPath; conn.Open();

//创建表

System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(); string sql = "CREATE TABLE weather(province varchar(20),city varchar(20),date1 varchar(20),tem1 varchar(20),wind1 varchar(20),pic1 varchar(20),date2 varchar(20),tem2 varchar(20),wind2 varchar(20),pic2 varchar(20),date3 varchar(20),tem3 varchar(20),wind3 varchar(20),pic3 varchar(20))"; mandText = sql; cmd.Connection = conn; cmd.ExecuteNonQuery();

//插入数据

sql = "INSERT INTO weather VALUES('" + hello[0] + "','" + hello[1] + "','" + hello[2] + "','" + hello[3] + "','" + hello[4] + "','" + hello[5] + "','" + hello[6] + "','" + hello[7] + "','" + hello[8] + "','" + hello[9] + "','" + hello[10] + "','" + hello[11] + "','" + hello[12] + "','" + hello[13] + "');";

mandText = sql; cmd.ExecuteNonQuery();

MessageBox.Show("创建本地数据库:" + dbPath); conn.Close();

System.Data.SQLite.SQLiteConnection.ClearAllPools();

} }

WinCE下的数据源操作以及天气软件的制作

//服务器的数据信息存到数组中 private void getFromSer() {

string[] hello = new string[15]; hello[0] = array[0]; hello[1] = array[1]; hello[2] = array[6]; hello[3] = array[5]; hello[4] = array[7]; hello[5] = array[8]; hello[6] = array[13]; hello[7] = array[12]; hello[8] = array[14]; hello[9] = array[15]; hello[10] = array[18]; hello[11] = array[17]; hello[12] = array[19]; hello[13] = array[20]; setData(hello); }

private void readDB()//读取本地数据库 {

if (System.IO.File.Exists(dbPath)) {

this.bReadDB.Text= "读取ing"; //连接数据库

System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();

conn.ConnectionString = "data source=" + dbPath; conn.Open();

System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand(); cmd.Connection = conn;

//取出数据

string sql = "SELECT * FROM weather"; mandText = sql;

System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader(); StringBuilder sb = new StringBuilder(); while (reader.Read()) {

sb.Append(reader.GetString(0)).Append(":")

WinCE下的数据源操作以及天气软件的制作

.Append(reader.GetString(1)).Append(":") .Append(reader.GetString(2)).Append(":") .Append(reader.GetString(3)).Append(":") .Append(reader.GetString(4)).Append(":") .Append(reader.GetString(5)).Append(":") .Append(reader.GetString(6)).Append(":") .Append(reader.GetString(7)).Append(":") .Append(reader.GetString(8)).Append(":") .Append(reader.GetString(9)).Append(":") .Append(reader.GetString(10)).Append(":") .Append(reader.GetString(11)).Append(":") .Append(reader.GetString(12)).Append(":") .Append(reader.GetString(13)).Append(":"); }

string[] hello = sb.ToString().Split(':');

array[0] = hello[0]; array[1] = hello[1];

array[6] = hello[2]; array[5] = hello[3]; array[7] = hello[4]; array[8] = hello[5];

array[13] = hello[6]; array[12] = hello[7]; array[14] = hello[8]; array[15] = hello[9];

array[18] = hello[10]; array[17] = hello[11]; array[19] = hello[12]; array[20] = hello[13];

this.bReadDB.Text = "读取本地数据库"; conn.Close();

System.Data.SQLite.SQLiteConnection.ClearAllPools(); showTodayWeather();

} else {

WinCE下的数据源操作以及天气软件的制作

MessageBox.Show("无法找到本地数据库文件:" + dbPath); } }

}

    精彩图片

    热门精选

    大家正在看