您的位置:首页 > 编程语言

windows mobile 5编程体验2 推荐

2010-08-22 16:57 375 查看
今天没事干,就练练手,做一个简单的欠债还钱系统。首先我们在SQLServer 2008上设计好数据库。打开SQLServer 2008 Management studio。服务器类型选择SQL Server Compact Edition。



数据库文件选项选择新建数据库。ok我们新建一个名为Debt的数据库文件,我们再新建一个名为为DebtInfo的表。如下



好了,ok接下来就进入我们的编程环节。我们新建一个智能设备项目,名为DebtSystem。新建两个窗体。



为了编程我在次写了一个简单的公共类,用来执行sql。

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
namespace DebtSystem
{
public class DBHelperSQL
{
private static string connectionString = "Data Source=\\Program Files\\Debt.sdf;Password=123asd";
public DBHelperSQL()
{
}

/// <summary>
/// 根据sql语句查询
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns></returns>
public static DataTable GetQueryResult(string sql)
{
using (SqlCeConnection sqlCeConnection = new SqlCeConnection(connectionString))
{
SqlCeDataAdapter sqlDataAdapter = new SqlCeDataAdapter(sql, sqlCeConnection);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
return ds.Tables[0];
}

}

public static int ExecuteSql(string sql)
{
using (SqlCeConnection sqlCeConnection = new SqlCeConnection(connectionString))
{
SqlCeCommand sqlCeCommand = new SqlCeCommand(sql,sqlCeConnection);
sqlCeConnection.Open();
int rowAffect = sqlCeCommand.ExecuteNonQuery();
sqlCeConnection.Close();
return rowAffect;
}
}
}
}

好了,接下来设计界面。如下第一个界面是欠债信息查询,选项卡可以切换功能。还有就是增加功能







下面是代码,首先是DebtInfo.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DebtSystem
{
public partial class DebtInfo : Form
{
public DebtInfo()
{
InitializeComponent();
}

private void DebtInfo_Load(object sender, EventArgs e)
{
string sql = "select * from DebtInfo";
string sql1 = "select * from DebtInfo where repaytime>'"+DateTime.Now+"' and isrepayed=0";
DataTable dt = DBHelperSQL.GetQueryResult(sql);
this.SetHeaderText(dt);
DataTable dt1 = DBHelperSQL.GetQueryResult(sql1);
this.SetHeaderText(dt1);
this.dataGridDebtInfo.DataSource = dt.DefaultView;
this.dataGridDebtQueryInfo.DataSource = dt.DefaultView;
this.dataGridAlarm.DataSource = dt1.DefaultView;
dateTimePicker1.Value = DateTime.Now.AddYears(-1);
}

private void button1_Click(object sender, EventArgs e)
{
try
{
string sqlWhere = string.Empty;
string sql = "select * from DebtInfo";
if (!string.IsNullOrEmpty(txtName.Text))
{
sqlWhere = sqlWhere == string.Empty ? string.Empty:" where debtpersonid like '%" + txtName.Text + "%'" ;
}
if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value) > 0)
{
MessageBox.Show("起始日期不能大于结束日期", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
return;
}
else
{
sqlWhere = sqlWhere == string.Empty ? " where repaytime between '"
+ dateTimePicker1.Value.Year.ToString().Substring(0, 2)
+ dateTimePicker1.Value.ToShortDateString()
+ "' and '" + dateTimePicker2.Value.Year.ToString().Substring(0,2)
+dateTimePicker2.Value.ToShortDateString() + "'" : " and repaytime between '"
+ dateTimePicker1.Value.Year.ToString().Substring(0, 2)+dateTimePicker1.Value.ToShortDateString() + "' and '"
+ dateTimePicker2.Value.Year.ToString().Substring(0,2)+dateTimePicker2.Value.ToShortDateString() + "'";
}
DataTable dt = DBHelperSQL.GetQueryResult(sql+sqlWhere);
this.SetHeaderText(dt);
this.dataGridDebtQueryInfo.DataSource = dt;
}
catch
{
MessageBox.Show("查询出错", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}

private void SetHeaderText(DataTable dt)
{
dt.Columns["debtpersonId"].ColumnName = "欠债人";
dt.Columns["amount"].ColumnName = "金额";
dt.Columns["borrowmoneytime"].ColumnName = "借钱日";
dt.Columns["repaytime"].ColumnName = "还钱日";
dt.Columns["isrepayed"].ColumnName = "是否已还款";
}

private void menuItem7_Click(object sender, EventArgs e)
{
Add add = new Add();
add.ShowDialog();
}

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
DebtInfo_Load(sender,e);
}
}
}

再看看Add.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DebtSystem
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (txtDebtPerson.Text == string.Empty)
{
ShowMessage("欠债人姓名不能为空");
return;
}
if (txtAmount.Text == string.Empty)
{
ShowMessage("金额不能为空");
return;
}
else
{
Regex reg = new Regex("^\\d+(\\.\\d+)?$");
if (!reg.IsMatch(txtAmount.Text))
{
ShowMessage("金额错误");
return;
}
}
if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value)>=0)
{
ShowMessage("借钱时间不能大于等于还钱时间");
return;
}
try
{
string id=Guid.NewGuid().ToString().Replace("-","");
string sql = "insert into debtInfo values('"
+ id + "','"
+ txtDebtPerson.Text + "',"
+ txtAmount.Text + ",'"
+ dateTimePicker1.Value.Year.ToString().Substring(0,2)+dateTimePicker1.Value + "','"
+ dateTimePicker2.Value.Year.ToString().Substring(0, 2) + dateTimePicker2.Value + "','"
+ (radioButton2.Text=="已还"? 1:0) + "','"
+ (txtRemark.Text == string.Empty ? "无" : txtRemark.Text)+"')";
int x = DBHelperSQL.ExecuteSql(sql);
if (x > 0)
{
ShowMessage("添加成功");
}
}
catch(Exception w)
{
ShowMessage(w.Message);
}

}

private void ShowMessage(string msg)
{
MessageBox.Show(msg, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}

private void button2_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
(ctrl as TextBox).Text = string.Empty;
}
}
}
}
}

好的看看运行效果







再看看添加,点击增加,弹出增加窗口







最后呢,教你怎么样调用手机模拟器的应用程序,在DebtInfo窗体的帮助菜单中选择计算器



点击计算器,代码如下

System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo("\\Windows\\calc.exe","start");
System.Diagnostics.Process.Start(p);

这段代码第一个类中的参数1是指你要打开的应用程序文件路径,参数2是指dos命令,这里我们用start。





调用就是这么容易。这里我给大家看看这些应用程序的路径。在手机模拟器的windows目录下









其实就是这么简单。又不明白的地方请留言。

附件:http://down.51cto.com/data/2356581
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息