PDA 入门开发的一个DEMO

  PDA 入门开发的一个DEMO

本程序基于mobile5.0的PPC开发,使用语言C#
我的机器的环境:windows 2003(E),SQL2005(E),VS2005(E)
--------------------------首先安装环境----------------
--一个模拟器,和链接PDA的工具
1 Microsoft ActiveSync 4.2.exe
--PDA中使用的数据库
2 sqlce30setupen.msi—版本ce3.0,cf2.0
 SSCE31SDK-CHS1.msi
--PPC开发工具的SDK
3 Windows Mobile 5.0 Pocket PC SDK2.msi
--3的汉化
4 Windows Mobile 5.0 Emulator Images for Pocket PC - CHS3.msi
--pda中安装的数据库sqlce,安装文件必须是cab的,否则不能识别
5 sqlce30.ppc.wce4.armv4.CAB
 --路径:C:/Program Files/Microsoft SQL Server Compact Edition/v3.1/SDK/bin/Client/v2.0/wce500/armv4i
--------------------------------配置开发环境-----------------------
1 Tools->Device Emulaor Manager
(1)默认选择:Pocket Pc 2003 SE Emulaor 右键->connection之后cradle
(2)安装mobile5.0后选择:CHS Windows Mobile5.0 Pocket Pc Emulaor
 右键->connection之后cradle
2 弹出Synchronization Setup Wizard 一直next
 将链接上模拟的PDA
 
-----------------------------创建winform项目----------------------
File -> new ->project->Visual c#->Smart Device->PPc2003->Device Application
--------------------------小测试----------------------
(1)放一个button就可以在PDA中看见效果了
(2)如果是Web的测试程序,在PDA的IE中输入
     http://192.168.0.114/localhost/website/defualt.aspx
 
----------------------------数据库的配置与基本使用---------------------
--SQL Server Mobile 学习:
--通过VS2005 创建和连接及操作SQL Server Mobile 数据库
a. 在项目外创建SQL Server Mobile数据库
b. 在项目内创建SQL Server Mobile数据库
c. 连接SQL Server Mobile数据库
d. 操作SQL Server Mobile数据库
=============================
 
a. 在项目外创建数据库
----------------------
(1).在“视图”菜单上单击“服务器资源管理器”。
(2).右击“数据连接”,然后单击“添加连接”,打开“添加连接”对话框。
(3).单击“更改”,打开“更改数据源”对话框。
(4).在“数据源”框中,选择“Microsoft SQL Server Mobile Edition”,
    然后单击“确定”。
(5).选择“Microsoft SQL Server Mobile Edition”,然后单击“确定”。
 
b.在项目内创建数据库
---------------------
(1).打开一个项目后,在“数据”菜单上单击“添加新数据源”。
    将打开“数据源配置向导”。
(2).在“选择数据源类型”页上,选择“数据库”,然后单击“下一步”。
(3).在“选择您的数据连接”页上单击“新建连接”,打开“添加连接”对话框。
(4).单击“更改”,打开“更改数据源”对话框。
(5).选择“Microsoft SQL Server Mobile Edition”,然后单击“确定”。
 
c.连接数据库
-------------
以下步骤呈接上面的a或b
 
(1).在“添加连接”对话框中,选择“我的电脑”。
(2).单击“创建”。
(3).在“创建新的SQL Server 2005 Mobile Edition 数据库”对话框中,
    键入新数据库的完全限定路径,如“c:/MyDB”。
(4).在“新密码”和“确认密码”框中键入新数据库的密码(如“MyPassword”),
    然后单击“确定”。
(5).在“添加连接”对话框中,单击“测试连接”以确保已建立连接。
    出现一条消息指示测试连接成功。
    注意SQL Server Mobile 不支持在网络共享上打开数据库文件。
(6).单击“确定”,返回到“添加连接”对话框,
    然后单击“确定”关闭该对话框。
(7).在“选择您的数据连接”页上,选择“是,在连接字符串中包含敏感数据”。
(8).单击“下一步”。
    出现“本地数据库文件”消息框,询问是否要在当前项目中包括数据文件。
    单击“是”。
(9).在“选择您的数据库对象”页上,选择要在项目中包含的表或其他对象。
(10).单击“完成”。
 
d. 操作SQL Server Mobile数据库
-------------------------
这时通过在“数据”菜单上单击“显示数据源”,
可以在“数据源”窗口中将新数据库作为数据集进行查看。
后续的操作可以通过单击某项的右键来进行
 
 
----------------------程序代码--------------------
前台:一个dataGrid,一个button,一个Text
后台如下:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//添加SqlServerCe引用后,引入此空间
using System.Data.SqlServerCe;
using System.IO;
 
namespace PDAconnection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            //DGdemo
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.createDatabase();
            this.insertData();
            SqlCeConnection scn = new SqlCeConnection("DataSource=mysdf.sdf");
            SqlCeDataAdapter sada = new SqlCeDataAdapter("select * from pc", scn);
            DataTable dt = new DataTable();
            sada.Fill(dt);
            this.DGdemo.DataSource = dt;
        }
        private void getData()
        {
 
 
        }
        private void insertData()
        {
            //string strno = "test";
            //string xh = "me";
            //string werks = "liu";
            //string pc_date = DateTime.Now.ToString();
            for (int i = 0; i < 6; i++)
            {
                SqlCeConnection myconn = new SqlCeConnection("DataSource=mysdf.sdf");
                SqlCeCommand com = new SqlCeCommand("insert into pc(pcno ,xh,werks,pc_date) values(" + "'2'," + "'ad'," + "'adf'," + "'2001-01-01'" + ")");
                myconn.Open();
                com.Connection = myconn;
                com.ExecuteNonQuery();
                myconn.Close();
            }
        }
        private void createDatabase()
        {
 
            //创建数据库
            try
            {
                if (File.Exists("mysdf.sdf"))
                {
                    MessageBox.Show("database is exists deleteing");
                    File.Delete("mysdf.sdf");
                }
                else
                {
                    SqlCeEngine eng = new SqlCeEngine("DataSource=mysdf.sdf");
                    eng.CreateDatabase();
                    eng.Dispose();
                    MessageBox.Show("create database success");
 
 
                }
                Boolean rtu;
                rtu = CreateTable();
                if (rtu == false)
                {
                    MessageBox.Show("database have already existe!");
 
                }
                else
                {
                    MessageBox.Show("cretae database success!");
 
 
                }
 
            }
            catch (Exception ex)
            {
                MessageBox.Show("create database fail " + ex.ToString().Substring(1, 60));
 
            }
        }
        private Boolean CreateTable()
        {
            try
            {
                SqlCeConnection myconn = new SqlCeConnection("DataSource=mysdf.sdf");
                SqlCeCommand com = new SqlCeCommand("create table PC (PCNO NVarChar(10) primary key,XH NVarChar(10) NOT NULL,WERKS NVarChar(4) NOT NULL,PC_DATE NVarChar(10))");
                myconn.Open();
                com.Connection = myconn;
                com.ExecuteNonQuery();
                myconn.Close();
                MessageBox.Show("create database success!");
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("create database faile" + ex.ToString().Substring(1, 60));
                return false;
            }
 
 
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "ok...good";
        }
 
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Rocker Up
                // Up
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Rocker Down
                // Down
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }
 
        }
 
      
    }
}
 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/charry0110/archive/2008/01/05/2026380.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Android平台连接PDA设备的UNI demo示例代码: ```java import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText mEditText; private TextView mTextView; private Button mButton; private Socket mSocket; private InputStream mInputStream; private OutputStream mOutputStream; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.editText1); mTextView = (TextView) findViewById(R.id.textView1); mButton = (Button) findViewById(R.id.button1); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String str = mEditText.getText().toString(); if (str.length() > 0) { send(str); } } }); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { mSocket = new Socket("192.168.1.1", 5000); mInputStream = mSocket.getInputStream(); mOutputStream = mSocket.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = mInputStream.read(buffer)) != -1) { final String str = new String(buffer, 0, len, "UTF-8"); runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub mTextView.append(str); } }); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } protected void send(String str) { // TODO Auto-generated method stub try { mOutputStream.write(str.getBytes("UTF-8")); mOutputStream.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } ``` 其中,需要注意的是: 1. 要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET"/> ``` 2. 在连接PDA设备时,需要根据实际的IP地址和端口号进行连接。 3. 在发送数据时,需要在发送的字符串前加上"\r\n",以便PDA设备能够正确的解析数据。 4. 在接收数据时,需要在UI线程中更新TextView的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值