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

📄 rdatools.cs

📁 使用C#开发的基于windows mobile的数据同步框架
💻 CS
📖 第 1 页 / 共 2 页
字号:
            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 删除指定表
        private static ErrMsg dropTable(string tablename, SqlCeConnection conn)
        {
            ErrMsg msg = new ErrMsg();
            SqlCeCommand cmd = null;
            cmd = new SqlCeCommand("drop table " + tablename, conn);
            try
            {
                cmd.ExecuteNonQuery();                
            }
            catch (SqlCeException ex)
            {
                //msg.msg = ex.Message;
            }
            finally
            {
                try
                {
                    msg.result = true;
                    cmd.Dispose();
                }
                catch (Exception ex) { }
            }
            return msg;
        }
        #endregion

        #region 获取表中数据最大时间戳
        public static Int64 getMaxTS(string tablename, SqlCeConnection conn)
        {
            Int64 maxTS = 0;//返回0说明没有同步数据
            SqlCeCommand cmd = null;
            try
            {
                cmd = new SqlCeCommand("select max(TMST) TMST,count(*) recnum from " + tablename, conn);
                SqlCeDataReader rd = cmd.ExecuteReader();
                if (rd.Read())
                {
                    if ((int)rd["recnum"] > 0)
                    {
                        maxTS = (Int64)rd["TMST"];
                    }
                }
                rd.Dispose();
            }
            catch (SqlCeException ex)
            {
            }
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex) { }
            }
            return maxTS;
        }
        #endregion

        #region 设置指定表的时间戳标记
        public static ErrMsg setTableMaxTS(string tablename, Int64 maxTS, SqlCeConnection conn)
        {
            ErrMsg msg = new ErrMsg();
            SqlCeCommand cmd = null;
            try
            {
                cmd = new SqlCeCommand("update INITTABLE set LASTSYNCDBTS=" + maxTS.ToString() + " where tbname='" + tablename + "'", conn);
                cmd.ExecuteNonQuery();
                msg.result = true;
            }
            catch (SqlCeException ex)
            {
                msg.msg = ex.Message;
            }
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex) { }
            }
            return msg;
        }
        #endregion

        #region 获取指定表的相关信息
        public static TableInfo getTableInfo(string tablename, SqlCeConnection conn)
        {
            TableInfo tbinfo = new TableInfo();
            SqlCeCommand cmd = null;
            try
            {
                cmd = new SqlCeCommand("select * from inittable where tbname='" + tablename + "'", conn);
                SqlCeDataReader rd = cmd.ExecuteReader();
                if (rd.Read())
                {
                    tbinfo.tbid = (int)rd["tbid"];
                    tbinfo.tbname = (string)rd["tbname"];
                    tbinfo.tbpk = (string)rd["tbpk"];
                    tbinfo.tbcols = (string)rd["tbcols"];
                    tbinfo.crtsql = (string)rd["crtsql"];
                    tbinfo.lastsyncdbts = (Int64)rd["lastsyncdbts"];
                    tbinfo.pulldata = (string)rd["pulldata"];
                    tbinfo.tbtype = (string)rd["tbtype"];
                    tbinfo.isavalid = (string)rd["isavalid"];
                }
                rd.Dispose();
            }
            catch (SqlCeException ex)
            {
            }
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex) { }
            }
            return tbinfo;
        }
        #endregion

        #region 判断终端数据是否有更改
        public static string getDataChange(string tablename, SqlCeConnection conn)
        {
            string changedIds = null;//用逗号分割的主键
            TableInfo tbinfo = getTableInfo(tablename, conn);
            SqlCeCommand cmd = null;
            try
            {
                cmd = new SqlCeCommand("select " + tbinfo.tbpk + " from " + tablename + " where chgflag='Y'", conn);
                SqlCeDataReader rd = cmd.ExecuteReader();
                StringBuilder buff = new StringBuilder("");
                while (rd.Read())
                {
                    buff.Append((string)rd[tbinfo.tbpk]);
                }
                if (buff.Length > 0)
                {
                    changedIds = buff.ToString();
                }
                rd.Dispose();
            }
            catch (SqlCeException ex)
            {
            }
            finally
            {
                try
                {
                    cmd.Dispose();
                }
                catch (Exception ex) { }
            }
            return changedIds;//如果有更改,返回逗号分割的主键,否则返回null
        }
        #endregion

        #region Push Data
        public static ErrMsg pushChg(string tbname)
        {
            ErrMsg msg = new ErrMsg();
            SqlCeRemoteDataAccess rda = null;
            SqlCeConnection conn = null;
            SqlCeCommand cmd = null;
            string syncTbName = tbname + "sync";
            try
            {
                rda = new SqlCeRemoteDataAccess(sqlAgent, connStr);
                conn = new SqlCeConnection(connStr);
                conn.Open();

                #region 创建同步表

                dropTable(syncTbName, conn);
                rda.Pull(syncTbName, "Select *,chgflag From " + syncTbName + " where 1<>1", rdaOleDbConnStr,
                    RdaTrackOption.TrackingOn, syncTbName + "err");
                #endregion

                #region 将变更应用到同步表
                cmd.CommandText = "insert into " + syncTbName + " select * from " + tbname + " where chgflag='Y'";
                cmd.ExecuteNonQuery();
                rda.Push(syncTbName, rdaOleDbConnStr, RdaBatchOption.BatchingOn);
                cmd.CommandText = "update " + tbname + " set chgflag='N' where chgflag='Y'";
                cmd.ExecuteNonQuery();
                //这段代码中,存在数据同步风险,当push时,如果修改了其它数据,后面同样把chgflag改为N,会导致更改的数据不再上传
                #endregion

                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
    }
}

⌨️ 快捷键说明

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