joozh 发表于 2018-6-17 09:00:58

windows mobile 5编程体验2

  今天没事干,就练练手,做一个简单的欠债还钱系统。首先我们在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=&quot;sql&quot;>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;
[*]            }
[*]
[*]      }
[*]
[*]      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 = &quot;select * from DebtInfo&quot;;
[*]            string sql1 = &quot;select * from DebtInfo where repaytime>'&quot;+DateTime.Now+&quot;' and isrepayed=0&quot;;
[*]            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 = &quot;select * from DebtInfo&quot;;
[*]                if (!string.IsNullOrEmpty(txtName.Text))
[*]                {
[*]                  sqlWhere = sqlWhere == string.Empty ? string.Empty:&quot; where debtpersonid like '%&quot; + txtName.Text + &quot;%'&quot; ;
[*]                }
[*]                if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value) > 0)
[*]                {
[*]                  MessageBox.Show(&quot;起始日期不能大于结束日期&quot;, &quot;提示信息&quot;, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
[*]                  return;
[*]                }
[*]                else
[*]                {
[*]                  sqlWhere = sqlWhere == string.Empty ? &quot; where repaytime between '&quot;
[*]                        + dateTimePicker1.Value.Year.ToString().Substring(0, 2)
[*]                        + dateTimePicker1.Value.ToShortDateString()
[*]                        + &quot;' and '&quot; + dateTimePicker2.Value.Year.ToString().Substring(0,2)
[*]                        +dateTimePicker2.Value.ToShortDateString() + &quot;'&quot; : &quot; and repaytime between '&quot;
[*]                        + dateTimePicker1.Value.Year.ToString().Substring(0, 2)+dateTimePicker1.Value.ToShortDateString() + &quot;' and '&quot;
[*]                        + dateTimePicker2.Value.Year.ToString().Substring(0,2)+dateTimePicker2.Value.ToShortDateString() + &quot;'&quot;;
[*]                }
[*]                DataTable dt = DBHelperSQL.GetQueryResult(sql+sqlWhere);
[*]                this.SetHeaderText(dt);
[*]                this.dataGridDebtQueryInfo.DataSource = dt;
[*]            }
[*]            catch
[*]            {
[*]                MessageBox.Show(&quot;查询出错&quot;, &quot;提示信息&quot;, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
[*]            }
[*]      }
[*]
[*]      private void SetHeaderText(DataTable dt)
[*]      {
[*]            dt.Columns[&quot;debtpersonId&quot;].ColumnName = &quot;欠债人&quot;;
[*]            dt.Columns[&quot;amount&quot;].ColumnName = &quot;金额&quot;;
[*]            dt.Columns[&quot;borrowmoneytime&quot;].ColumnName = &quot;借钱日&quot;;
[*]            dt.Columns[&quot;repaytime&quot;].ColumnName = &quot;还钱日&quot;;
[*]            dt.Columns[&quot;isrepayed&quot;].ColumnName = &quot;是否已还款&quot;;
[*]      }
[*]
[*]      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(&quot;欠债人姓名不能为空&quot;);
[*]                return;
[*]            }
[*]            if (txtAmount.Text == string.Empty)
[*]            {
[*]                ShowMessage(&quot;金额不能为空&quot;);
[*]                return;
[*]            }
[*]            else
[*]            {
[*]                Regex reg = new Regex(&quot;^\\d+(\\.\\d+)?$&quot;);
[*]                if (!reg.IsMatch(txtAmount.Text))
[*]                {
[*]                  ShowMessage(&quot;金额错误&quot;);
[*]                  return;
[*]                }
[*]            }
[*]            if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value)>=0)
[*]            {
[*]                ShowMessage(&quot;借钱时间不能大于等于还钱时间&quot;);
[*]                return;
[*]            }
[*]            try
[*]            {
[*]                string id=Guid.NewGuid().ToString().Replace(&quot;-&quot;,&quot;&quot;);
[*]                string sql = &quot;insert into debtInfo values('&quot;
[*]                  + id + &quot;','&quot;
[*]                  + txtDebtPerson.Text + &quot;',&quot;
[*]                  + txtAmount.Text + &quot;,'&quot;
[*]                  + dateTimePicker1.Value.Year.ToString().Substring(0,2)+dateTimePicker1.Value + &quot;','&quot;
[*]                  + dateTimePicker2.Value.Year.ToString().Substring(0, 2) + dateTimePicker2.Value + &quot;','&quot;
[*]                  + (radioButton2.Text==&quot;已还&quot;? 1:0) + &quot;','&quot;
[*]                  + (txtRemark.Text == string.Empty ? &quot;无&quot; : txtRemark.Text)+&quot;')&quot;;
[*]                int x = DBHelperSQL.ExecuteSql(sql);
[*]                if (x > 0)
[*]                {
[*]                  ShowMessage(&quot;添加成功&quot;);
[*]                }
[*]            }
[*]            catch(Exception w)
[*]            {
[*]                ShowMessage(w.Message);
[*]            }
[*]
[*]      }
[*]
[*]      private void ShowMessage(string msg)
[*]      {
[*]            MessageBox.Show(msg, &quot;提示信息&quot;, 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(&quot;\\Windows\\calc.exe&quot;,&quot;start&quot;);
[*]            System.Diagnostics.Process.Start(p);
  

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

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


  其实就是这么简单。又不明白的地方请留言。
页: [1]
查看完整版本: windows mobile 5编程体验2