Mobile RDA 同步数据库的类--转

Mobile RDA 同步数据库的类--转  
 using
 System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.IO;

namespace DataBaseCTLSpace
{
    
class DataBaseCTL
    
{
        
private string assemblyLocalFolder;
        
private SqlCeConnection SQLConn;
        
private string InternetURL;
        
private string OleDbConnectString;

        
/// <summary>
        
/// 构造函数,构造一个操作数据库类
        
/// </summary>
        
/// <param name="ServerDataBase">服务器数据库名称</param>
        
/// <param name="DataBase">本地数据库名称</param>
        
/// <param name="ServerName">服务器IP,或者名称</param>
        
/// <param name="UserName">用户名</param>
        
/// <param name="PassWord">密码</param>
        
/// <param name="IISShareDir">IIS共享服务目录,构造InternetURL用</param>

        public DataBaseCTL(string ServerDataBase, string LocalDataBase, string ServerName, string UserName, string PassWord, string IISShareDir)
        
{
            assemblyLocalFolder 
= System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            assemblyLocalFolder 
= assemblyLocalFolder + "\" + LocalDataBase + ".sdf";
            InternetURL 
= "http://" + ServerName + "/" + IISShareDir + "/sqlcesa30.dll";
            OleDbConnectString 
= "Provider=sqloledb;server=" + ServerName + ";database=" + ServerDataBase
                               
+ "; User Id=" + UserName + ";Password=" + PassWord;
        }


        
~DataBaseCTL()
        
{
            SqlCeEngine engine 
= new SqlCeEngine("Data Source =" + assemblyLocalFolder);
            engine.Shrink();

        }


        
/// <summary>
        
/// 创建数据库文件
        
/// </summary>
        
/// <returns>成功 True, 失败 False 表示有文件或者出现异常</returns>

        public bool CreateDatbase()
        
{
            
if (File.Exists(assemblyLocalFolder)) return false;
            
try
            
{
                SqlCeEngine engine 
= new SqlCeEngine("Data Source =" + assemblyLocalFolder);
                engine.CreateDatabase();
                engine.Dispose();
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }


        
/// <summary>
        
/// 删除数据库文件
        
/// </summary>
        
/// <returns>成功 True, 失败 False</returns>

        public bool DeleteDatabase()
        
{
            
if (File.Exists(assemblyLocalFolder))
            
{
                File.Delete(assemblyLocalFolder);
                
return true;
            }

            
else
                
return false;
        }


        
/// <summary>
        
/// 在本地执行SQL
        
/// </summary>
        
/// <param name="SqlStr">SQL语句</param>
        
/// <returns>结果集</returns>

        public ResultSetView ExecuteSqlLocal(string SqlStr,ResultSetView R)
        
{

            SqlCeConnection conn 
= new SqlCeConnection("Data Source =" + assemblyLocalFolder);

            
// 创建并配置 SqlCeCommand 对象
            SqlCeCommand cmd = conn.CreateCommand();
            cmd.CommandText 
= SqlStr;

            
// 创建 SqlCeResultSet 对象,并配置为可滚动、可更新、检测数据源更改
            ResultSetOptions options = ResultSetOptions.Scrollable |
                                                     ResultSetOptions.Sensitive 
|
                                                     ResultSetOptions.Updatable;
            SqlCeResultSet resultSet 
= cmd.ExecuteResultSet(options);

            
// 创建 ResultSetView 对象,配置为只显示序号为 1,3,5,8 的列
            ResultSetView resultSetView = resultSet.ResultSetView;
            
int[] ordinals = new int[] 1358 };
            resultSetView.Ordinals 
= ordinals;
            R 
= resultSetView;
            
return resultSetView;
        }


        
/// <summary>
        
/// 在本地执行SQL
        
/// </summary>
        
/// <param name="SqlStr">SQL语句</param>
        
/// <returns>结果</returns>

        public bool ExecuteSqlLocal(string SqlStr)
        
{
            
try
            
{
                SqlCeConnection conn 
= new SqlCeConnection("Data Source =" + assemblyLocalFolder);
                SqlCeCommand cmd 
= conn.CreateCommand();
                cmd.CommandText 
= SqlStr;
                conn.Open();
                cmd.ExecuteNonQuery();
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }


        
/// <summary>
        
/// 在服务器上执行Sql
        
/// </summary>
        
/// <param name="SqlStr">Sql语句</param>
        
/// <returns></returns>

        public bool ExecuteSqlServer(string SqlStr)
        
{
            
try
            
{
                SqlCeRemoteDataAccess rda 
= null;
                rda 
= new SqlCeRemoteDataAccess();
                rda.InternetUrl 
= InternetURL;
                rda.LocalConnectionString 
= "Data Source =" + assemblyLocalFolder;
                rda.SubmitSql(SqlStr,OleDbConnectString);
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }


        
/// <summary>
        
/// 从服务器上下载表
        
/// </summary>
        
/// <param name="TableName">表名称</param>
        
/// <returns></returns>

        public bool DownloadTable(string TableName)
        
{
            
if (ExecuteSqlLocal("select * from " + TableName))
            
{//有这个表
                
//提交服务器更新
                if (!UPDateTable(TableName)) return false;
                
//删除这个表
                if (!ExecuteSqlLocal("drop table " + TableName)) return false;
            }

            
            
try
            
{
                SqlCeRemoteDataAccess rda 
= null;
                rda 
= new SqlCeRemoteDataAccess();
                rda.InternetUrl 
= InternetURL;
                rda.LocalConnectionString 
= "Data Source =" + assemblyLocalFolder;
                rda.Pull(TableName, 
"Select * from " + TableName, OleDbConnectString, RdaTrackOption.TrackingOnWithIndexes);//
                return true;
            }

            
catch
            
{

                
return false;
            }

        }


        
/// <summary>
        
/// 把更改好的表提交到服务器上
        
/// </summary>
        
/// <param name="TableName">表名</param>
        
/// <returns></returns>

        public bool UPDateTable(string TableName)
        
{
           
try
            
{
                SqlCeRemoteDataAccess rda 
= null;
                rda 
= new SqlCeRemoteDataAccess();
                rda.InternetUrl 
= InternetURL;
                rda.LocalConnectionString 
= "Data Source =" + assemblyLocalFolder;
                rda.Push(TableName, OleDbConnectString);
//
                return true;
            }

            
catch
            
{
                
return false;
            }


        }

    }

}
posted @ 2008-03-25 19:49  TwinStudio  阅读(758)  评论(1编辑  收藏  举报