Remote Data Access 访问 通用组件

下载源代码

/**

此RDA组件  封装了手持机与服务器的数据交互的基本功能

功能:创建本地数据
设置网络连接密码
设置本地文件路径
下载数据
上传数据
提交服务器所执行的SQL语句功能.



/*
*Copyright (c)2007,
*All Rights reserved.

*文件名称:SqlCEDatabaseDll.dll
*文件标识:
*摘要:
*
*当前版本:1.0
*作者:蔡海洋
*完成日期:2007.5.31
*功能:SqlCE数据库RDA的基本操作 
*开发工具:vs2005
      
  

 

*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;
using System.IO;

namespace ZHXK.WinCE50
{
    public class SqlCEDataRDADll : IDisposable
    {
        // connection to data source
        private SqlCeEngine engin;
        private SqlCeRemoteDataAccess con;
        private string localConnectString = "";
        private string remoteConnectString = "";

        public SqlCEDataRDADll()
        {


        }

        public string LocalConnectString
        {
            set { this.localConnectString = value; }
            get { return this.localConnectString; }
        }
        public string RemoteConnectString
        {
            set { this.remoteConnectString = value; }
            get { return remoteConnectString; }
        }
        /// <summary>
        /// 创建本地数据库文件
        /// </summary>
        /// <returns>true 创建成功 ; false 创建失败</returns>
        public bool CreadDatabase()
        {
            bool state = false;
            try
            {
                engin = new SqlCeEngine(this.LocalConnectString);
                engin.CreateDatabase();
                state = true;
            }
            catch (SqlCeException exc)
            {
                throw new Exception(exc.Message);
            }
            return state;
        }
        /// <summary>
        /// 删除数据文件。   下载数据前  本地不能有相同的表名  , 所以在downloadData前先 删除数据库(建议:wince需要更新的数据和不需要更新的数据使用不同的数据 库)
        /// </summary>
        /// <param name="FileName">要删除数据库的文件路径</param>
        /// <returns>true删除成功. false 删除失败</returns>
        public bool DeleteDatabase(string FileName)
        {
            bool state = false;
            try
            {
                if (File.Exists(FileName))
                {
                    File.Delete(FileName);
                    state = true;
                }
                else
                {
                    state = false;
                }
            }
            catch (SqlCeException exc)
            {
                throw new Exception(exc.Message);
            }
            return state;
        }
        /// <summary>
        /// 实例 化远程服务连接
        /// </summary>
        /// <param name="remoteConnectStr">远程服务器连接字符串</param>
        /// <param name="internetURL">iis 发布 sql 代理</param>
        public void InitRemoteServer(string internetURL)
        {

            con = new SqlCeRemoteDataAccess();
            con.InternetUrl = internetURL;
            con.LocalConnectionString = this.LocalConnectString;
        }
        /// <summary>
        /// 从远程服务器上下载数据到本地  参数localTableName 本
        /// </summary>
        /// <param name="localTableName">本地表名  LEmployee</param>
        /// <param name="sqlSelectString">select 语句 select * from Employee</param>
        /// <param name="changeData">false:程序不会更改下载的数据
        ///                          true: 程序将会更改下载的数据。  更改操作完成后用UploadData()上传操作的表
        /// </param>
        /// <returns>true下载数据成功  ;false 下载数据失败</returns>
        public bool DownLoadData(string localTableName, string sqlSelectString, bool changeData)
        {
            bool state = false;
            try
            {
                if (changeData)
                {
                    con.Pull(localTableName, sqlSelectString, this.RemoteConnectString, RdaTrackOption.TrackingOn);
                    state = true;
                }
                else
                {
                    con.Pull(localTableName, sqlSelectString, this.RemoteConnectString, RdaTrackOption.TrackingOff);
                    state = true;
                }

            }
            catch (SqlCeException exc)
            {
                throw new Exception(exc.Message);
            }
            return state;
        }

        /// /// <summary>
        /// 将DownloadData 下载的数据 上传到程序服务器
        /// </summary>
        /// <param name="localTableName">本地要上传的表</param>
        /// <returns>true 上传成功,false上传失败</returns>
        public bool UpLoadData(string localTableName)
        {
            bool state = false;
            try
            {
                con.Push(localTableName, this.RemoteConnectString);
                state = true;
            }
            catch (SqlCeException exc)
            {
                throw new Exception(exc.Message);
            }
            return state;
        }

        /// <summary>
        /// 提交sql语句到服务器执行   适用 delete   update  insert
        /// </summary>
        /// <param name="sqlstring">sql语句"delete from tablename"</param>
        /// <returns>true 执行成功,false执行失败</returns>
        public bool RunSQLOnRomoteServer(string sqlstring)
        {
            bool state = false;
            try
            {
                con.SubmitSql(sqlstring, this.RemoteConnectString);
                state = true;
            }
            catch (SqlCeException exc)
            {
                throw new Exception(exc.Message);
            }
            return state;
        }

 

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            // make sure connection is closed
            if (con != null)
            {
                con.Dispose();
                con = null;

            }
            if (engin != null)
            {

                engin.Dispose();
                engin = null;
            }
        }
    }
}

posted on 2009-02-27 10:54  酸辣大白菜  阅读(610)  评论(0编辑  收藏  举报

导航