⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdatools.cs

📁 使用C#开发的基于windows mobile的数据同步框架
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlServerCe;

namespace SyncData
{
    class RdaTools
    {
        #region 初始化服务器信息
        public static string svrIP = @"192.168.196.134";
        public static string webdbname = @"sqlce3";
        public static string sqlAgent = @"http://" + svrIP + "/" + webdbname + "/sqlcesa30.dll";

        public static string svrDBName = "sqlce";
        public static string svrDBUser = "rda";
        public static string svrDBPwd = "rda";
        public static string rdaOleDbConnStr = "Provider=sqloledb;Data Source=" + svrIP + ";"
                                        + "Initial Catalog=" + svrDBName + ";"
                                        + "User Id=" + svrDBUser + ";Password=" + svrDBPwd + ";";
        #endregion

        #region 初始化终端数据库信息
        public static string dbName = "sqlcedb.sdf";
        private static string dbFile = string.Format(@"{0}\" + dbName,
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
        //Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
        public static string connStr = string.Format(@"Data Source = {0}", dbFile);
        #endregion

        #region 创建数据库SDF文件
        public static ErrMsg createDb()
        {
            ErrMsg msg = new ErrMsg();
            try
            {
                if (File.Exists(dbFile))
                {
                    //File.Delete(dbFile);
                }
                else
                {
                    SqlCeEngine engine = new SqlCeEngine(connStr);
                    engine.CreateDatabase();
                    engine.Dispose();
                }
                msg.result = true;
                msg.msg = "创建数据库成功";
            }
            catch (SqlCeException ex)
            {
                msg.result = false;
                msg.msg = ex.Message;
                //throw ex;
            }
            return msg;
        }
        #endregion

        #region 初始化字典表及相关数据表
        public static ErrMsg createTbl()
        {
            ErrMsg msg = new ErrMsg();
            SqlCeRemoteDataAccess rda = null;
            SqlCeConnection conn = null;
            SqlCeCommand cmd = null;
            try
            {
                rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
                conn = new SqlCeConnection(connStr);
                conn.Open();
                #region 初始化字典表
                msg = dropTable("INITTABLE", conn);
                if (!msg.result)
                {
                    return msg;
                }
                rda.Pull("INITTABLE", "Select * From INITTABLE where ISAVALID='Y'", rdaOleDbConnStr,
                    RdaTrackOption.TrackingOff);
                #endregion

                #region 根据字典表初始化表
                cmd = new SqlCeCommand("INITTABLE", conn);
                cmd.CommandType = CommandType.TableDirect;
                SqlCeDataReader rd = cmd.ExecuteReader();
                string syncTbName = null;
                while (rd.Read())
                {
                    syncTbName = (string)rd["TBNAME"];
                    dropTable(syncTbName, conn);
                    if ("Y".Equals(rd["TBTYPE"]))
                    {
                        rda.Pull(syncTbName, (string)rd["CRTSQL"], rdaOleDbConnStr, RdaTrackOption.TrackingOn);
                    }
                    else
                    {
                        rda.Pull(syncTbName, (string)rd["CRTSQL"], rdaOleDbConnStr, RdaTrackOption.TrackingOff);
                    }
                    Int64 maxTS = getMaxTS(syncTbName, conn);
                    if (maxTS != 0)
                    {
                        if (!setTableMaxTS(syncTbName, maxTS, conn).result)
                        {
                            msg.result = false;
                            msg.msg = "设置时间戳失败";
                            return msg;
                        }
                    }
                }
                msg.result = true;
                msg.msg = "初始化表成功";
                #endregion
            }
            catch (SqlCeException ex)
            {
                msg.result = false;
                msg.msg = ex.Message;
                //throw ex;
            }
            #region 释放资源
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                }
                try
                {
                    conn.Close();
                    conn.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                } try
                {
                    rda.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                }
            }
            #endregion
            return msg;
        }
        #endregion

        #region 从服务器下载指定表全表数据
        public static ErrMsg pullAll(string tablename)
        {
            ErrMsg msg = new ErrMsg();
            SqlCeRemoteDataAccess rda = null;
            SqlCeConnection conn = null;
            SqlCeCommand cmd = null;
            try
            {
                rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
                conn = new SqlCeConnection(connStr);
                conn.Open();
                TableInfo tbinfo = getTableInfo(tablename, conn);
                dropTable(tbinfo.tbname, conn);
                //if ("Y".Equals(tbinfo.tbtype))
                //{
                //    rda.Pull(tbinfo.tbname, (string)rd["CRTSQL"], rdaOleDbConnStr, RdaTrackOption.TrackingOn);
                //}
                //else
                //{
                rda.Pull(tbinfo.tbname, tbinfo.crtsql, rdaOleDbConnStr, RdaTrackOption.TrackingOff);
                //}
                Int64 maxTS = getMaxTS(tablename, conn);
                if (maxTS != 0)
                {
                    if (!setTableMaxTS(tablename, maxTS, conn).result)
                    {
                        msg.result = false;
                        msg.msg = "设置时间戳失败";
                        return msg;
                    }
                }
                msg.result = true;
                msg.msg = "数据传输成功";
            }
            catch (SqlCeException ex)
            {
                msg.result = false;
                msg.msg = ex.Message;
                //throw ex;
            }
            #region 释放资源
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                }
                try
                {
                    conn.Close();
                    conn.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                } try
                {
                    rda.Dispose();
                }
                catch (Exception ex)
                {
                    //msg.result = false;
                    //msg.msg = ex.Message;
                    //throw ex;
                }
            }
            #endregion
            return msg;
        }
        #endregion

        #region 从服务器下载指定表增量数据
        public static ErrMsg pullChange(string tablename)
        {
            ErrMsg msg = new ErrMsg();
            SqlCeRemoteDataAccess rda = null;
            SqlCeConnection conn = null;
            SqlCeCommand cmd = null;
            try
            {
                rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
                conn = new SqlCeConnection(connStr);
                conn.Open();

                TableInfo tbinfo = getTableInfo(tablename, conn);
                string syncTable = tbinfo.tbname + "pulltemp";
                dropTable(syncTable, conn);
                string pullsql = null;
                if (tbinfo.crtsql.ToUpper().IndexOf("WHERE") > 0)
                {
                    pullsql = tbinfo.crtsql + " and tmst>" + tbinfo.lastsyncdbts;
                }
                else
                {
                    pullsql = tbinfo.crtsql + " where tmst>" + tbinfo.lastsyncdbts;
                }
                rda.Pull(syncTable, pullsql, rdaOleDbConnStr, RdaTrackOption.TrackingOff);

                cmd = new SqlCeCommand("delete from " + tablename + " where " + tbinfo.tbpk + " in (select " + tbinfo.tbpk + " from " + syncTable + ")", conn);
                cmd.ExecuteNonQuery();
                cmd.CommandText = "update " + syncTable + " set chgflag='N'";
                cmd.ExecuteNonQuery();
                cmd.CommandText = "insert into " + tbinfo.tbname + "(" + tbinfo.tbcols + ") select " + tbinfo.tbcols + " from " + syncTable;
                cmd.ExecuteNonQuery();

                Int64 maxTS = getMaxTS(syncTable, conn);
                if (maxTS != 0)
                {
                    if (!setTableMaxTS(tablename, maxTS, conn).result)
                    {
                        msg.result = false;
                        msg.msg = "设置时间戳失败";
                        return msg;
                    }
                }
                msg.result = true;
                msg.msg = "数据传输成功";
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -