将批量数据作为 XML 标记发送到 SQL 服务器的问题
本文关键字:SQL 服务器 问题 数据 XML | 更新日期: 2023-09-27 18:28:13
我正在将批量数据(序列号,引脚号(插入数据库。在插入之前,数据表中的数据被绑定到XML标签中。这里的引脚号是加密的...如下
strXml = "<?xml<pre lang="c#"></pre> version=" + @"""1.0"" encoding=" + @"""iso-8859-1""?><batch>";
strPinXml =strPinXml + "<data cardid="+@"""" +strid+@""""+" pinnumber=" + @"""" + myRC4Engine.CryptedText + @"""" + "></data>";
strXml = strXml + strPinXml + "</batch>";
问题是插入数据库后,为了验证是否插入了实际的引脚号(db中的加密格式(,我解密了引脚号,发现,
所有数据中的第一个数字被 ('( 单引号取代,最后一个数字被 某些引脚编号的数字为空(如果引脚编号为 -œ A_¡/ì·ÞvËÛ (即(以 Û 结尾,表示引脚最后一个数字为空(。
请注意,我在此应用程序中使用的是SQL Server 2000
请提供解决此问题的解决方案
结果如下
插入数据库前的引脚
- 引脚数(插入时(
- (加密格式( --- (解密格式(
- šA [¦,ÈμØzËÚ --------- 7613051524692
- œA ¡/ì•þvëû --------- 1687765748683
- ™ @X¦!Ï'ÝÎÛ --------- 4770086471383
- žA Z¡+ɹÝwïò --------- 3642720979218
- •O Q¢(˹Þ{ËÛ --------- 8879412945686
- ŸO^¡,è¶Ý}Î× --------- 2846751673342
插入后从数据库检索
引脚
- 引脚编号(从数据库中检索( ---- 检索的引脚编号
- (加密格式( --------------- (解密格式(
- A [¦,ÈμØzËÚ ------------------- '613051524692
- A _¡/ì•þvËÛ ------------------- '68776574868
- @X¦!Ï'ÝÎÛ ------------------ '77008647138
- A z¡+ɹÝwÏÒ ----------------- '642720979218
- O Q¢(˹Þ{ËÛ ------------------- '879412945686
- O ^¡,è¶Ý}Î× ------------------ '846751673342
应用程序编码如下
try
{
RC4Engine myRC4Engine = new RC4Engine();
myRC4Engine.EncryptionKey = "ab48495fdjk4950dj39405fk";
strXml = "<?xml version=" + @"""1.0"" encoding=" + @"""iso-8859-1""?> <batch>";
foreach (DataRow lobjbaseBatchDetail in dt.Rows)
{
myRC4Engine.InClearText = lobjbaseBatchDetail[3].ToString();
myRC4Engine.Encrypt();
strCardid = lobjbaseBatchDetail[0].ToString();
strBatchid = lobjbaseBatchDetail[1].ToString();
strid = strCardid + strBatchid + lobjbaseBatchDetail[2].ToString();
strPinXml =strPinXml + "<data cardid="+@"""" +strid+@""""+
" pinnumber=" + @"""" + myRC4Engine.CryptedText + @"""" + "></data>";
}
strXml = strXml + strPinXml + "</batch>";
SqlParameter[] arrParam = new SqlParameter[1];
arrParam[0] = new SqlParameter("@BATCHUPLOAD_XML", SqlDbType.Text );
arrParam[0].Direction = ParameterDirection.Input;
arrParam[0].Value = strXml;
iResult = SqlHelper.ExecuteNonQuery(objTrans, CommandType.StoredProcedure, "test_proc", arrParam);
objTrans.Commit();
}
catch(Exception ex)
{
objTrans.Rollback();
throw new Exception("Upload failed :" + ex.Message);
}
程序
create procedure test_proc
(
@BATCHUPLOAD_XML text
)
as
begin
DECLARE @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT, @BATCHUPLOAD_XML
insert into test_table_new
SELECT cardid,pinnumber
FROM OPENXML (@idoc, '/batch/data')
WITH (cardid varchar(100) '@cardid', pinnumber nvarchar(200) '@pinnumber')
EXEC sp_xml_removedocument @idoc
end
你不应该手写 xml。 让 C# 的 LinqToXml 为您完成,它不会出错。
你可以这样写:
try
{
RC4Engine myRC4Engine = new RC4Engine();
myRC4Engine.EncryptionKey = "ab48495fdjk4950dj39405fk";
XDocument doc = new XDocument(
new XDeclaration("1.0", "iso-8859-1", null),
new XElement("batch"));
foreach (DataRow lobjbaseBatchDetail in dt.Rows)
{
myRC4Engine.InClearText = lobjbaseBatchDetail[3].ToString();
myRC4Engine.Encrypt();
strCardid = lobjbaseBatchDetail[0].ToString();
strBatchid = lobjbaseBatchDetail[1].ToString();
strid = strCardid + strBatchid + lobjbaseBatchDetail[2].ToString();
XElement data = new XElement("data");
data.Add(new XAttribute("cardid", strid));
data.Add(new XAttribute("pinnumber", myRC4Engine.CryptedText));
doc.Root.Add(data);
}
SqlParameter[] arrParam = new SqlParameter[1];
arrParam[0] = new SqlParameter("@BATCHUPLOAD_XML", SqlDbType.Text );
arrParam[0].Direction = ParameterDirection.Input;
arrParam[0].Value = doc.Declaration.ToString() +
doc.ToString(SaveOptions.DisableFormatting);
iResult = SqlHelper.ExecuteNonQuery(objTrans, CommandType.StoredProcedure, "test_proc", arrParam);
objTrans.Commit();
}
catch(Exception ex)
{
objTrans.Rollback();
throw new Exception("Upload failed :" + ex.Message);
}
RC4加密的信息是二进制数据,而不是文本。加密文本不应键入为字符串。若要将二进制数据包装在 XML 中,您可能希望使用二进制数据的 BASE64 编码。