windows mobile 5编程体验2

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介:

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

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

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

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


 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Data;  
  6. using System.Data.SqlServerCe;  
  7. namespace DebtSystem  
  8. {  
  9.     public class DBHelperSQL  
  10.     {  
  11.         private static string connectionString = "Data Source=\\Program Files\\Debt.sdf;Password=123asd";  
  12.         public DBHelperSQL()  
  13.         {  
  14.         }  
  15.  
  16.         /// <summary>  
  17.         /// 根据sql语句查询  
  18.         /// </summary>  
  19.         /// <param name="sql">sql语句</param>  
  20.         /// <returns></returns>  
  21.         public static DataTable GetQueryResult(string sql)  
  22.         {  
  23.             using (SqlCeConnection sqlCeConnection = new SqlCeConnection(connectionString))   
  24.             {  
  25.                 SqlCeDataAdapter sqlDataAdapter = new SqlCeDataAdapter(sql, sqlCeConnection);  
  26.                 DataSet ds = new DataSet();  
  27.                 sqlDataAdapter.Fill(ds);  
  28.                 return ds.Tables[0];  
  29.             }  
  30.              
  31.         }  
  32.  
  33.         public static int ExecuteSql(string sql)  
  34.         {  
  35.             using (SqlCeConnection sqlCeConnection = new SqlCeConnection(connectionString))  
  36.             {  
  37.                 SqlCeCommand sqlCeCommand = new SqlCeCommand(sql,sqlCeConnection);  
  38.                 sqlCeConnection.Open();  
  39.                 int rowAffect = sqlCeCommand.ExecuteNonQuery();  
  40.                 sqlCeConnection.Close();  
  41.                 return rowAffect;  
  42.             }  
  43.         }  
  44.     }  
  45. }  

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

  

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


 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Text.RegularExpressions;  
  9. using System.Windows.Forms;  
  10.  
  11. namespace DebtSystem  
  12. {  
  13.     public partial class DebtInfo : Form  
  14.     {  
  15.         public DebtInfo()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.  
  20.         private void DebtInfo_Load(object sender, EventArgs e)  
  21.         {  
  22.             string sql = "select * from DebtInfo";  
  23.             string sql1 = "select * from DebtInfo where repaytime>'"+DateTime.Now+"' and isrepayed=0";  
  24.             DataTable dt = DBHelperSQL.GetQueryResult(sql);  
  25.             this.SetHeaderText(dt);  
  26.             DataTable dt1 = DBHelperSQL.GetQueryResult(sql1);  
  27.             this.SetHeaderText(dt1);  
  28.             this.dataGridDebtInfo.DataSource = dt.DefaultView;  
  29.             this.dataGridDebtQueryInfo.DataSource = dt.DefaultView;  
  30.             this.dataGridAlarm.DataSource = dt1.DefaultView;  
  31.             dateTimePicker1.Value = DateTime.Now.AddYears(-1);  
  32.         }  
  33.  
  34.         private void button1_Click(object sender, EventArgs e)  
  35.         {  
  36.             try 
  37.             {  
  38.                 string sqlWhere = string.Empty;  
  39.                 string sql = "select * from DebtInfo";  
  40.                 if (!string.IsNullOrEmpty(txtName.Text))  
  41.                 {  
  42.                     sqlWhere = sqlWhere == string.Empty ? string.Empty:" where debtpersonid like '%" + txtName.Text + "%'" ;  
  43.                 }  
  44.                 if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value) > 0)  
  45.                 {  
  46.                     MessageBox.Show("起始日期不能大于结束日期""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);  
  47.                     return;  
  48.                 }  
  49.                 else 
  50.                 {  
  51.                     sqlWhere = sqlWhere == string.Empty ? " where repaytime between '"   
  52.                         + dateTimePicker1.Value.Year.ToString().Substring(0, 2)  
  53.                         + dateTimePicker1.Value.ToShortDateString()   
  54.                         + "' and '" + dateTimePicker2.Value.Year.ToString().Substring(0,2)  
  55.                         +dateTimePicker2.Value.ToShortDateString() + "'" : " and repaytime between '"   
  56.                         + dateTimePicker1.Value.Year.ToString().Substring(0, 2)+dateTimePicker1.Value.ToShortDateString() + "' and '"   
  57.                         + dateTimePicker2.Value.Year.ToString().Substring(0,2)+dateTimePicker2.Value.ToShortDateString() + "'";  
  58.                 }  
  59.                 DataTable dt = DBHelperSQL.GetQueryResult(sql+sqlWhere);  
  60.                 this.SetHeaderText(dt);  
  61.                 this.dataGridDebtQueryInfo.DataSource = dt;  
  62.             }  
  63.             catch 
  64.             {  
  65.                 MessageBox.Show("查询出错""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);  
  66.             }  
  67.         }  
  68.  
  69.         private void SetHeaderText(DataTable dt)  
  70.         {  
  71.             dt.Columns["debtpersonId"].ColumnName = "欠债人";  
  72.             dt.Columns["amount"].ColumnName = "金额";  
  73.             dt.Columns["borrowmoneytime"].ColumnName = "借钱日";  
  74.             dt.Columns["repaytime"].ColumnName = "还钱日";  
  75.             dt.Columns["isrepayed"].ColumnName = "是否已还款";  
  76.         }  
  77.  
  78.         private void menuItem7_Click(object sender, EventArgs e)  
  79.         {  
  80.             Add add = new Add();  
  81.             add.ShowDialog();  
  82.         }  
  83.  
  84.         private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)  
  85.         {  
  86.             DebtInfo_Load(sender,e);  
  87.         }  
  88.     }  

再看看Add.cs


 
 
  1. using System;  
  2. using System.Linq;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Text.RegularExpressions;  
  9. using System.Windows.Forms;  
  10.  
  11. namespace DebtSystem  
  12. {  
  13.     public partial class Add : Form  
  14.     {  
  15.         public Add()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.  
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             if (txtDebtPerson.Text == string.Empty)  
  23.             {  
  24.                 ShowMessage("欠债人姓名不能为空");  
  25.                 return;  
  26.             }  
  27.             if (txtAmount.Text == string.Empty)  
  28.             {  
  29.                 ShowMessage("金额不能为空");  
  30.                 return;  
  31.             }  
  32.             else 
  33.             {  
  34.                 Regex reg = new Regex("^\\d+(\\.\\d+)?$");  
  35.                 if (!reg.IsMatch(txtAmount.Text))  
  36.                 {  
  37.                     ShowMessage("金额错误");  
  38.                     return;  
  39.                 }  
  40.             }  
  41.             if (dateTimePicker1.Value.CompareTo(dateTimePicker2.Value)>=0)  
  42.             {  
  43.                 ShowMessage("借钱时间不能大于等于还钱时间");  
  44.                 return;  
  45.             }  
  46.             try 
  47.             {  
  48.                 string id=Guid.NewGuid().ToString().Replace("-","");  
  49.                 string sql = "insert into debtInfo values('"   
  50.                     + id + "','"   
  51.                     + txtDebtPerson.Text + "',"   
  52.                     + txtAmount.Text + ",'" 
  53.                     + dateTimePicker1.Value.Year.ToString().Substring(0,2)+dateTimePicker1.Value + "','" 
  54.                     + dateTimePicker2.Value.Year.ToString().Substring(0, 2) + dateTimePicker2.Value + "','"   
  55.                     + (radioButton2.Text=="已还"? 1:0) + "','"   
  56.                     + (txtRemark.Text == string.Empty ? "无" : txtRemark.Text)+"')";  
  57.                 int x = DBHelperSQL.ExecuteSql(sql);  
  58.                 if (x > 0)  
  59.                 {  
  60.                     ShowMessage("添加成功");  
  61.                 }  
  62.             }  
  63.             catch(Exception w)  
  64.             {  
  65.                 ShowMessage(w.Message);  
  66.             }  
  67.              
  68.         }  
  69.  
  70.         private void ShowMessage(string msg)  
  71.         {  
  72.             MessageBox.Show(msg, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);  
  73.         }  
  74.  
  75.         private void button2_Click(object sender, EventArgs e)  
  76.         {  
  77.             foreach (Control ctrl in this.Controls)  
  78.             {  
  79.                 if (ctrl is TextBox)  
  80.                 {  
  81.                     (ctrl as TextBox).Text = string.Empty;  
  82.                 }  
  83.             }  
  84.         }  
  85.     }  

好的看看运行效果

  

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

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

点击计算器,代码如下


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

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

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

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


本文转自 BruceAndLee 51CTO博客,原文链接:http://blog.51cto.com/leelei/381289,如需转载请自行联系原作者


相关文章
|
消息中间件 C++ Windows
02 MFC - Windows 编程模型
02 MFC - Windows 编程模型
141 0
|
网络协议 API Windows
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
MASM32编程调用 API函数RtlIpv6AddressToString,windows 10 容易,Windows 7 折腾
[原创]用MASM32编程获取windows类型
[原创]用MASM32编程获取windows类型
|
JavaScript 前端开发 API
MASM32编程通过WMI获取Windows计划任务
MASM32编程通过WMI获取Windows计划任务
|
编译器 开发工具 C语言
解锁QtCreator跨界神技!Windows下轻松驾驭OpenCV动态库,让你的跨平台开发如虎添翼,秒变视觉编程大师!
【8月更文挑战第4天】QtCreator是一款强大的跨平台IDE,便于创建多平台应用。本教程教你如何在Windows环境下集成OpenCV库至Qt项目。首先,下载匹配MinGW的OpenCV预编译版并解压。接着,在QtCreator中新建或打开项目,并在.pro文件中添加OpenCV的头文件和库文件路径。确保编译器设置正确。随后编写测试代码,例如加载和显示图片,并进行编译运行。完成这些步骤后,你就能在QtCreator中利用OpenCV进行图像处理开发了。
690 6
|
API Windows
MASM32编程获取Windows当前桌面主题名
MASM32编程获取Windows当前桌面主题名
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
1351 0
|
开发框架 JavaScript .NET
【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found
【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found
187 0
|
Java C++
jni编程(windows+JDK11+clion)
jni编程(windows+JDK11+clion)
250 1
|
API C++ Windows
windows编程入门_链接错误的配置
windows编程入门_链接错误的配置
137 0

热门文章

最新文章