RDA通用服务类和注意事项
注意:Pull方法一次只可以从SQl Server获取一个数据表,SQl Mobile 数据库不能存在同名的数据表,所以每次通过Pull方法获取新的数据时,必须先删除上次Pull方法获得的本地表.
private string _sQLCE_DROP_TABLE = "DROP TABLE {0}";
private string _sqlExistsTable = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {0}";
/// <summary>
/// 删除本地表(如果存在,否则同步会失败)
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>成功返回true,失败返回false, 出现异常则抛出异常</returns>
private bool DeleteTable(string tableName)
{
PowerNet.Mobile.Data.DAL.CommonDAL dal = new PowerNet.Mobile.Data.DAL.CommonDAL();
bool returnBool = false;
string commandText = string.Format(_sqlExistsTable, "'"+tableName+"'");
try
{
//数据表是否已经存在,存在才走下面的删除表代码
if (Convert.ToInt32(dal.ExecuteScalar(SysEnv.LocalConnStr, commandText)) == 1)
{
commandText = "";
commandText = String.Format(this._sQLCE_DROP_TABLE, tableName);
SqlMobileHelper.ExecuteNonQuery(commandText);
returnBool = true;
}
}
catch (Exception err)
{
Cursor.Current = Cursors.Default;
throw err;
}
return returnBool;
}
private string _sqlExistsTable = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = {0}";
/// <summary>
/// 删除本地表(如果存在,否则同步会失败)
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>成功返回true,失败返回false, 出现异常则抛出异常</returns>
private bool DeleteTable(string tableName)
{
PowerNet.Mobile.Data.DAL.CommonDAL dal = new PowerNet.Mobile.Data.DAL.CommonDAL();
bool returnBool = false;
string commandText = string.Format(_sqlExistsTable, "'"+tableName+"'");
try
{
//数据表是否已经存在,存在才走下面的删除表代码
if (Convert.ToInt32(dal.ExecuteScalar(SysEnv.LocalConnStr, commandText)) == 1)
{
commandText = "";
commandText = String.Format(this._sQLCE_DROP_TABLE, tableName);
SqlMobileHelper.ExecuteNonQuery(commandText);
returnBool = true;
}
}
catch (Exception err)
{
Cursor.Current = Cursors.Default;
throw err;
}
return returnBool;
}
1{
2 /// <summary>
3 /// 通用服务类
4 /// </summary>
5 public class CommonDAL : RDADAL
6 {
7 RDADAL _dal = new RDADAL();
8 public CommonDAL()
9 {
10
11 }
12
13 /// <summary>
14 /// PullData方法,把数据从远程把数据下载到本地
15 /// </summary>
16 /// <param name="localTableName">指定了在SQL Mobile数据库中的数据表名称,该数据表用于存放Pull方法从SQL Server获得的数据</param>
17 /// <param name="sqlSelectString">设置从SQl Server获取的数据集的SQL语句(可通过Where语句获得某个数据表的子集)</param>
18 /// <param name="oleDBConnectionString">指定了SQl Server的数据库连接字符串</param>
19 /// <param name="trackOption">用于SQL Mobile数据表的数据跟踪设置,指定RdaTrackOption枚举变量</param>
20 /// <param name="errorTable">设置一个数据表用于在Push方法的时候发生数据冲突时,会将数据错误信息放到该表</param>
21 /// <returns> 数据下载成功返回true,数据下载失败返回false, 出现异常则抛出异常</returns>
22 public bool PullData(string localTableName, string sqlSelectString, string oleDBConnectionString, RdaTrackOption trackOption, string errorTable)
23 {
24 //初始化RDA对象
25 bool booReturn = false;
26 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(RDADAL.SqlAgentString, _dal.LocalConnString);
27 try
28 {
29 rda.Pull(
30 localTableName, sqlSelectString,
31 oleDBConnectionString, trackOption,
32 errorTable);
33 rda.Dispose();
34 booReturn = true;
35 }
36 catch (SqlCeException ex)
37 {
38 //错误处理
39 throw ex;
40 }
41 finally
42 {
43 //释放RDA对象
44 rda.Dispose();
45 }
46 return booReturn;
47 }
48
49 /// <summary>
50 /// PushData方法,把SQL Mobile本地表的修改结果传递回SQl Server数据中
51 /// </summary>
52 /// <param name="LocalTableName">是SQL Mobile数据表中的通过Pull方法获取的数据表,这个表不能是一个SQL Mobile普通的本地表;也不能是Pull方法设置为TrackingOff时获取的表</param>
53 /// <param name="oleDBConnectionString">连接SQL Server的数据库连接字符串,该字符串必须和对应Pull方法的oleDBConnectionString相同</param>
54 /// <param name="batchOption">设置是否批量提交。(是RdaBatchOption枚举变量,默认为BatchingOff,被修改的数据被逐条提交,相反BatchingOn为批量提交)</param>
55 /// <returns> 数据上传成功返回true,数据上传失败返回false, 出现异常则抛出异常</returns>
56 public bool PushData(string LocalTableName, string oleDBConnectionString, RdaBatchOption batchOption)
57 {
58 //初始化RDA对象
59 bool booReturn = false;
60 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(SqlAgentString, _dal.LocalConnString);
61 try
62 {
63 rda.Push(LocalTableName, oleDBConnectionString, batchOption);
64 booReturn = true;
65 }
66 //错误处理
67 catch (SqlCeException ex)
68 {
69 throw ex;
70 }
71 finally
72 {
73 //释放RDA对象
74 rda.Dispose();
75 }
76 return booReturn;
77 }
78
79 /// <summary>
80 /// SubmitSQl方法,把SQL语句提交到SQl Server服务器中执行
81 /// </summary>
82 /// <param name="sqlString">提交的SQL语句</param>
83 /// <param name="oleDBConnectionString">连接SQL Server的数据库连接字符串</param>
84 /// <returns>SubmitSQl方法执行成功返回true,执行失败返回false, 出现异常则抛出异常</returns>
85 public bool SubmitSQL(string sqlString, string oleDBConnectionString)
86 {
87 //初始化RDA对象
88 bool booReturn = false;
89 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(SqlAgentString, _dal.LocalConnString);
90 try
91 {
92 rda.SubmitSql(sqlString, oleDBConnectionString);
93 booReturn = true;
94 }
95 //错误处理
96 catch (SqlCeException ex)
97 {
98 throw ex;
99 }
100 finally
101 {
102 //释放RDA对象
103 rda.Dispose();
104 }
105 return booReturn;
106 }
107
108 }
2 /// <summary>
3 /// 通用服务类
4 /// </summary>
5 public class CommonDAL : RDADAL
6 {
7 RDADAL _dal = new RDADAL();
8 public CommonDAL()
9 {
10
11 }
12
13 /// <summary>
14 /// PullData方法,把数据从远程把数据下载到本地
15 /// </summary>
16 /// <param name="localTableName">指定了在SQL Mobile数据库中的数据表名称,该数据表用于存放Pull方法从SQL Server获得的数据</param>
17 /// <param name="sqlSelectString">设置从SQl Server获取的数据集的SQL语句(可通过Where语句获得某个数据表的子集)</param>
18 /// <param name="oleDBConnectionString">指定了SQl Server的数据库连接字符串</param>
19 /// <param name="trackOption">用于SQL Mobile数据表的数据跟踪设置,指定RdaTrackOption枚举变量</param>
20 /// <param name="errorTable">设置一个数据表用于在Push方法的时候发生数据冲突时,会将数据错误信息放到该表</param>
21 /// <returns> 数据下载成功返回true,数据下载失败返回false, 出现异常则抛出异常</returns>
22 public bool PullData(string localTableName, string sqlSelectString, string oleDBConnectionString, RdaTrackOption trackOption, string errorTable)
23 {
24 //初始化RDA对象
25 bool booReturn = false;
26 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(RDADAL.SqlAgentString, _dal.LocalConnString);
27 try
28 {
29 rda.Pull(
30 localTableName, sqlSelectString,
31 oleDBConnectionString, trackOption,
32 errorTable);
33 rda.Dispose();
34 booReturn = true;
35 }
36 catch (SqlCeException ex)
37 {
38 //错误处理
39 throw ex;
40 }
41 finally
42 {
43 //释放RDA对象
44 rda.Dispose();
45 }
46 return booReturn;
47 }
48
49 /// <summary>
50 /// PushData方法,把SQL Mobile本地表的修改结果传递回SQl Server数据中
51 /// </summary>
52 /// <param name="LocalTableName">是SQL Mobile数据表中的通过Pull方法获取的数据表,这个表不能是一个SQL Mobile普通的本地表;也不能是Pull方法设置为TrackingOff时获取的表</param>
53 /// <param name="oleDBConnectionString">连接SQL Server的数据库连接字符串,该字符串必须和对应Pull方法的oleDBConnectionString相同</param>
54 /// <param name="batchOption">设置是否批量提交。(是RdaBatchOption枚举变量,默认为BatchingOff,被修改的数据被逐条提交,相反BatchingOn为批量提交)</param>
55 /// <returns> 数据上传成功返回true,数据上传失败返回false, 出现异常则抛出异常</returns>
56 public bool PushData(string LocalTableName, string oleDBConnectionString, RdaBatchOption batchOption)
57 {
58 //初始化RDA对象
59 bool booReturn = false;
60 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(SqlAgentString, _dal.LocalConnString);
61 try
62 {
63 rda.Push(LocalTableName, oleDBConnectionString, batchOption);
64 booReturn = true;
65 }
66 //错误处理
67 catch (SqlCeException ex)
68 {
69 throw ex;
70 }
71 finally
72 {
73 //释放RDA对象
74 rda.Dispose();
75 }
76 return booReturn;
77 }
78
79 /// <summary>
80 /// SubmitSQl方法,把SQL语句提交到SQl Server服务器中执行
81 /// </summary>
82 /// <param name="sqlString">提交的SQL语句</param>
83 /// <param name="oleDBConnectionString">连接SQL Server的数据库连接字符串</param>
84 /// <returns>SubmitSQl方法执行成功返回true,执行失败返回false, 出现异常则抛出异常</returns>
85 public bool SubmitSQL(string sqlString, string oleDBConnectionString)
86 {
87 //初始化RDA对象
88 bool booReturn = false;
89 SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess(SqlAgentString, _dal.LocalConnString);
90 try
91 {
92 rda.SubmitSql(sqlString, oleDBConnectionString);
93 booReturn = true;
94 }
95 //错误处理
96 catch (SqlCeException ex)
97 {
98 throw ex;
99 }
100 finally
101 {
102 //释放RDA对象
103 rda.Dispose();
104 }
105 return booReturn;
106 }
107
108 }