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的格式的方法如下:
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了!