首先在 数据 库里新建存储过程: create procedure lcw_test--author:lcw--description:--datetime:2012-10-12@xml nvarchar(max)asbegin declare @idhandle int exec sp_xml_preparedocument @idhandle output, @xml --创建xml的句柄 --print @idhandle ins
首先在数据库里新建存储过程:
create procedure lcw_test--author:lcw--description:--datetime:2012-10-12@xml nvarchar(max)asbegin declare @idhandle int exec sp_xml_preparedocument @idhandle output, @xml --创建xml的句柄 --print @idhandle insert into t_test (id,name,msg,savetime) select id,name,msg,savetime from openxml(@idhandle,n'/root/t_test') with t_test if @@error=0 begin select 1 end else begin select 0 end exec sp_xml_removedocument @idhandle --xml文档会存储在sqlserver的缓存中,为了避免内存不足,执行该语句 以释放内存。 end
然后是c#代码:
private void button14_click(object sender, eventargs e) { list ce = tets(); xmldocument document = new xmldocument(); xmlelement root = document.createelement(root); document.appendchild(root); foreach (lcw_test personentity in ce) { xmlelement xmlperson = document.createelement(t_test); xmlperson.setattribute(id,personentity.id.tostring()); xmlperson.setattribute(name, personentity.name); xmlperson.setattribute(msg, personentity.msg); xmlperson.setattribute(savetime, personentity.dt.tostring()); root.appendchild(xmlperson); } sqlparameter[] parameters = null; parameters = new sqlparameter[] { new sqlparameter(@xml, document.innerxml) }; try { using (sqlconnection conn = new sqlconnection(server=.;database=test;user=sa;pwd=lcw;)) { conn.open(); using (sqlcommand cmd = conn.createcommand()) { cmd.commandtext = lcw_test; cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addrange(parameters); int cccc = convert.toint32(cmd.executenonquery()); } } } catch (exception ex) { throw ex; } } list tets() { list ce = new list(); lcw_test c = null; for (int i = 0; i < 5000; i++) { ce.add(c = new lcw_test { id = guid.newguid(), name = name + i, msg = msg + i, dt = datetime.now }); } return ce; }
public class lcw_test { public guid id { get; set; } public string name { get; set; } public string msg { get; set; } public datetime dt { get; set; } }
