分布式数据库定时同步问题之--SqlCommand不可序列化

近日,做一个分布式数据库定时同步的项目,也就是说有多个物理节点上的数据库需要在每天某时来同步表中的数据,对于某个指定节点上的某个表发生的变化(增量)可以通过对该表执行的SqlCommand来记录,当同步过程发生时,需要对其它所有节点上的同名表执行相同的SqlCommand。由于,同步是定时发生的,所以增量SqlCommand就需要首先被保存起来,开始我们计划将其序列化后保存在数据库中,但是到运行时,问题来了,SqlCommand是不可序列化的!!!所有继承自IDbCommand的类都是不可序列化的,所有继承自IDbParameter的实现类都是不可序列化的。
遇到了这个问题,是我们当初没有料想到的,看来要妥善的解决这个问题,否则,我们的设计和实现不可避免的要进行大量的修改!我们最后采用的解决方案是自己手写一个可以序列化的ESqlCommand,它可以和SqlCommand相互转换,就像这样:
SqlCommandcommand=newSqlCommand(this.cmdText,newSqlConnection(connStr),null);
//正向
ESqlCommandesCommand=newESqlCommand(command);
//反向
SqlCommandcommand2=esCommand.ToSqlCommand(connStr);
有了ESqlCommand之后,我们的设计几乎不需要修改,仅仅在增量序列化保存到数据库之前转换为ESqlCommand,而读出增量字段反序列化之后再转换为SqlCommand即可。
下面给出ESqlCommand的实现(当然,不可避免的也要包含可序列化的ESqlParameter实现):
#regionESqlCommand
///<summary>
///ESqlCommand可序列化的SqlCommand。System.Data.SqlClient.SqlCommand是不可序列化的
///</summary>
[Serializable]
publicclassESqlCommand
{
privateESqlParameter[]esParas;
privatestringcmdText;
privateCommandTypecmdType;

publicESqlCommand(SqlCommandcommand)
{
this.cmdText=command.CommandText;
this.cmdType=command.CommandType;

this.esParas=newESqlParameter[command.Parameters.Count];
intindex=0;
foreach(SqlParameterparaincommand.Parameters)
{
this.esParas[index]=newESqlParameter(para);
index
++;
}
}

publicSqlCommandToSqlCommand(stringconnStr)
{
SqlCommandcommand
=newSqlCommand(this.cmdText,newSqlConnection(connStr),null);

for(inti=0;i<this.esParas.Length;i++)
{
command.Parameters.Add(
this.esParas[i].ToSqlParameter());
}

command.CommandType
=this.cmdType;
returncommand;
}
}

[Serializable]
publicclassESqlParameter
{
publicESqlParameter(SqlParametersPara)
{
this.paraName=sPara.ParameterName;
this.paraLen=sPara.Size;
this.paraVal=sPara.Value;
this.sqlDbType=sPara.SqlDbType;
}

publicSqlParameterToSqlParameter()
{
SqlParameterpara
=newSqlParameter(this.paraName,this.sqlDbType,this.paraLen);
para.Value
=this.paraVal;

returnpara;
}


#regionParaName
privatestringparaName="";
publicstringParaName
{
get
{
returnthis.paraName;
}
set
{
this.paraName=value;
}
}
#endregion

#regionParaLen
privateintparaLen=0;
publicintParaLen
{
get
{
returnthis.paraLen;
}
set
{
this.paraLen=value;
}
}
#endregion

#regionParaVal
privateobjectparaVal=null;
publicobjectParaVal
{
get
{
returnthis.paraVal;
}
set
{
this.paraVal=value;
}
}
#endregion

#regionSqlDbType
privateSqlDbTypesqlDbType=SqlDbType.NVarChar;
publicSqlDbTypeSqlDbType
{
get
{
returnthis.sqlDbType;
}
set
{
this.sqlDbType=value;
}
}
#endregion

}
#endregion
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值