Wince开发用Utils

原创 2016年05月30日 17:12:42

Wince连接Mysql数据库

Wince连接Mysql数据库所用dll:mysql.data.cf.dll

Wince操作注册表

Wince 同步/异步 播放声音

记录日志:

public static void writeLog(string sql)
{
    try
    {
        String CodePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
        CodePath = CodePath.Substring(0, CodePath.LastIndexOf(@"\"));
        String filePathName = CodePath + "\\log.txt";

        FileInfo file = new FileInfo(filePathName);
        if (!file.Exists)
            file.Create();
        StreamWriter sw = file.AppendText();
        sw.WriteLine(sql);
        sw.Flush();
        sw.Close();
    }
    catch (Exception) { }
}

Wince直连数据库


using System;

using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace JPPDA.utils
{
    public class DBUtil
    {
        private string connStr = string.Empty;

        private MySqlConnection conn;

        private MySqlCommand cmd;

        private MySqlTransaction tran;

        public DBUtil()
        {
        }

        public DBUtil(string dbType, string dbIP, string dbUser, string dbPass, string dbName,string dbPort)
        {
            if (dbType.ToUpper().Equals("MYSQL"))
            {
                this.connStr = string.Concat(new string[]
                {
                    "database=",
                    dbName,
                    ";Port=",
                    dbPort,
                    ";Password=",
                    dbPass,
                    ";User ID=",
                    dbUser,
                    ";server=",
                    dbIP,
                    ";charset=utf8;AllowZeroDatetime=true;default command timeout=100;"
                });
            }
        }

        public bool TestConn()
        {
            bool result;
            try
            {
                if (this.conn == null)
                {
                    this.conn = new MySqlConnection(this.connStr);
                }
                if (this.conn.State == ConnectionState.Closed)
                {
                    this.conn.Open();
                    this.conn.Close();
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }

        public void Open()
        {
            try
            {
                if (this.conn == null)
                {
                    this.conn = new MySqlConnection(this.connStr);
                }
                if (this.conn.State == ConnectionState.Closed)
                {
                    this.conn.Open();
                }
            }
            catch (DbException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public void Close()
        {
            if (this.conn.State == ConnectionState.Open)
            {
                this.conn.Close();
            }
        }

        public DataSet GetDataSet(string sql)
        {
            this.Open();
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            this.cmd = this.conn.CreateCommand();
            this.cmd.CommandText = sql;
            mySqlDataAdapter.SelectCommand = this.cmd;
            DataSet dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet);
            this.Close();
            return dataSet;
        }

        public MySqlCommand getCommand()
        {
            return conn.CreateCommand();
        }

        public DataTable GetDataTable(string sql)
        {
            this.Open();
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            this.cmd = this.conn.CreateCommand();
            this.cmd.CommandText = sql;
            mySqlDataAdapter.SelectCommand = this.cmd;
            DataSet dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet);
            this.Close();
            return dataSet.Tables[0];
        }

        public int Execute(string sql)
        {
            int res=0;
            this.Open();
            this.cmd = this.conn.CreateCommand();
            this.tran = this.conn.BeginTransaction();
            try
            {
                this.cmd.Transaction = this.tran;
                this.cmd.CommandText = sql;
                res = this.cmd.ExecuteNonQuery();
                this.tran.Commit();
            }
            catch (Exception ex)
            {
                res = -1;
                this.tran.Rollback();
                throw new Exception(ex.Message);
            }
            finally
            {
                this.conn.Close();
            }
            return res;
        }

        public void Transaction()
        {
            this.Open();
            this.cmd = this.conn.CreateCommand();
            this.tran = this.conn.BeginTransaction();
        }

        public DataTable GetDataTableNotClose(string sql)
        {
            MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter();
            this.cmd = this.conn.CreateCommand();
            this.cmd.CommandText = sql;
            mySqlDataAdapter.SelectCommand = this.cmd;
            DataSet dataSet = new DataSet();
            mySqlDataAdapter.Fill(dataSet);
            return dataSet.Tables[0];
        }

        public void ExecuteNotTran(string sql)
        {
            this.cmd.Transaction = this.tran;
            this.cmd.CommandText = sql;
            this.cmd.ExecuteNonQuery();
        }

        public void Commit()
        {
            this.tran.Commit();
            this.Close();
        }

        public void Rollback()
        {
            this.tran.Rollback();
            this.Close();
        }
    }
}

Wince操作注册表

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

using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;

namespace PDA.Utils
{
    class RegeditUtils
    {


        public static void writeReg(String name, String data)
        {
         /*   RegistryKey OurKey = Registry.LocalMachine;  //根路径为 HKEY_LOCAL_MACHINE 
            OurKey = OurKey.OpenSubKey(Command.RegPath, true); //注册表中项的路径,按实现修改  
            OurKey.SetValue(name, data, RegistryValueKind.String); //更新指定键的值。如果键不存在,就会直接新增。
*/
            RegistryKey rsg = Registry.LocalMachine;                         //声明一个变量
            if (Registry.LocalMachine.OpenSubKey(Command.RegPath) == null)
            {
                Registry.LocalMachine.CreateSubKey(Command.RegPath);
                //创建
            }
            rsg = Registry.LocalMachine.OpenSubKey(Command.RegPath, true);    //true表可以修改
            rsg.SetValue(name, data);       //写入
            rsg.Close();

        }

        public static String readReg(String name)
        {
            if (Registry.LocalMachine.OpenSubKey(Command.RegPath) == null)
            {
                Registry.LocalMachine.CreateSubKey(Command.RegPath);
                //创建
            }

            RegistryKey OurKey = Registry.LocalMachine;  //根路径为 HKEY_LOCAL_MACHINE 
            OurKey = OurKey.OpenSubKey(Command.RegPath, true); //注册表中项的路径,按实现修改  
            return (String)OurKey.GetValue(name,null);
        }
    }
}

Wince同步/异步 播放声音

using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace PDA.Utils
{
    public class Sound
    {

        private byte[] m_soundBytes;

        private string m_fileName;

        private enum Flags
        {
            SND_SYNC = 0x0000,  /* play synchronously (default) */
            SND_ASYNC = 0x0001,  /* play asynchronously */
            SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
            SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
            SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
            SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            SND_ALIAS = 0x00010000, /* name is a registry alias */
            SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
            SND_FILENAME = 0x00020000, /* name is file name */
            SND_RESOURCE = 0x00040004  /* name is resource name or atom */
        }



        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);



        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);



        /// <summary>  
        /// Construct the Sound object to play sound data from the specified file.  
        /// </summary>  
        public Sound(string fileName)
        {
            m_fileName = fileName;
        }


        /// <summary>  
        /// Construct the Sound object to play sound data from the specified stream.  
        /// </summary>  
        public Sound(Stream stream)
        {
            // read the data from the stream  
            m_soundBytes = new byte[stream.Length];
            stream.Read(m_soundBytes, 0, (int)stream.Length);
        }

        /// <summary>  
        /// Play the sound  
        /// </summary>  
        public void Play()
        {
            // if a file name has been registered, call WCE_PlaySound,  
            //  otherwise call WCE_PlaySoundBytes  
            if (m_fileName != null)
                WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
            else
                WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
        }


        public void PlaySync()
        {
            // if a file name has been registered, call WCE_PlaySound,  
            //  otherwise call WCE_PlaySoundBytes  
            if (m_fileName != null)
                WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_SYNC | Flags.SND_FILENAME));
            else
                WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_SYNC | Flags.SND_MEMORY));
        }

        public void PlayASync()
        {
            // if a file name has been registered, call WCE_PlaySound,  
            //  otherwise call WCE_PlaySoundBytes  
            if (m_fileName != null)
                WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME  |Flags.SND_NOSTOP));
            else
                WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY  | Flags.SND_NOSTOP));
        }

        public static void playASync(String name)
        {
            Sound sound = new Sound(Command.ExePath + "\\wav\\" + name + ".wav");
            sound.PlayASync();
        }

        public static void playSync(String name)
        {
            Sound sound = new Sound(Command.ExePath + "\\wav\\" + name + ".wav");
            sound.PlaySync();
        }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Ti3559开发板的utils文件

  • 2015年01月25日 10:47
  • 24KB
  • 下载

AndroidUtils:Android开发不得不收藏的Utils

AndroidUtils AndroidUtils Android开发不得不收藏的Utils 之前写这篇文章主要是项目应用到的Utils,发现已经有一个更全面的开源库总结,所以还是非常震惊可以总结的这...

AndroidUtils:Android开发不得不收藏的Utils

AndroidUtils:Android开发不得不收藏的Utils - Android开发社区 | CTOLib码库 AndroidUtils Andr...

安卓开发常用工具类utils

Android开发中一些小功能收藏起来,可以提高开发效率,平时的积累也是很重要的,这些功能其实不需要记住,收藏好,拿来就用,拿完即走。不多说了,抓紧保存备忘吧。1.android dp和px之间转换p...

android快速开发--常用utils类

整理出10个基本每个项目中都会使用的工具类,用于快速开发。 1、日志工具类L.java [java] view plaincopy package com.zhy.utils...

Android - 开发常用工具类Utils

Android - 开发常用工具类Utils

android快速开发--常用utils类

整理出10个基本每个项目中都会使用的工具类,用于快速开发。 1、日志工具类L.java [java] view plaincopy packa...

android快速开发--常用utils类

整理出10个基本每个项目中都会使用的工具类,用于快速开发。 1、日志工具类L.java [java] view plaincopy p...

WinCE设备仿真器+虚拟串口+GPS模拟器搭建开发测试环境

所需软件:1、WinCE 5.0设备仿真器2、虚拟串口工具Virtual Serial Port Driver 6.93、Virace GPS 模拟器0.3.1chs 模拟环境:COM2:连接WinC...

wince手持机程序开发总结--部分

1.   界面设计:   WinCE系统一般分为两部分: Windows Mobile   和Windows CE       移动设备在界面设计时应当注意的问题。                输入...
  • bobo830
  • bobo830
  • 2011年05月19日 10:49
  • 1085
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Wince开发用Utils
举报原因:
原因补充:

(最多只允许输入30个字)