关闭

Wince开发用Utils

标签: WinceUtilsMysql注册表播放声音
261人阅读 评论(0) 收藏 举报
分类:

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();
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14095次
    • 积分:612
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:0篇
    • 译文:0篇
    • 评论:4条