1: 针对大批量数据入库,为了减少数据库的链接次数,使用xml的形式一次性的入库。具体参考代码如下:
SQL xml的格式写法
CREATE Procedure B2B_cPersonCard_Xml
--Author:sey
--Description:insert into B2B_cPersonCard
--DateTime:2009-08-13
@xml nvarchar(max)
AS
begin
declare @idHandle int
EXEC sp_xml_preparedocument @idHandle OUTPUT, @xml --创建xml的句柄
print @idHandle
INSERT INTO [B2B_cPersonCard] (cEmpID,cType,cName,cCode,cAirID)
SELECT cEmpID,cType,cName,cCode,cAirID FROM OPENXML(@idHandle,N'/root/PersonCard') with [B2B_cPersonCard]
IF @@ERROR=0
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 0
END
EXEC sp_xml_removedocument @idHandle --xml文档会存储在sqlserver的缓存中,为了避免内存不足,执行该语句 以释放内存。
end
2:C#组织Xml的格式的方法如下:
public bool ADDPersonCard(List<PersonCard> list, string pType, int pEmpID)
{
//throw new Exception("The method or operation is not implemented.");
XmlDocument document = new XmlDocument();
XmlElement root = document.CreateElement("root");
document.AppendChild(root);
foreach (PersonCard personEntity in list)
{
XmlElement xmlPerson = document.CreateElement("PersonCard");
xmlPerson.SetAttribute("cEmpID", pEmpID.ToString());
xmlPerson.SetAttribute("cType", pType);
xmlPerson.SetAttribute("cName", personEntity.CradName);
xmlPerson.SetAttribute("cCode", personEntity.CardCode);
xmlPerson.SetAttribute("cAirID", personEntity.AirID);
root.AppendChild(xmlPerson);
}
SqlParameter[] parameters = null;
parameters = new SqlParameter[] { new SqlParameter("@xml", document.InnerXml) };
try
{
int result = 0;
result = int.Parse(DBLib.GetDataTableBySP("B2B_cPersonCard_Xml", parameters, DBConnEnm.B2BDB).Rows[0][0].ToString());
if (result > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
throw e;
}
}
将数据组织成list 就ok了!