开发者

Insert class instance in a List to SQL database

开发者 https://www.devze.com 2023-03-14 19:29 出处:网络
What is the best way to insert a List of class instances in SQL 2008 R2 table? I am using C# in a CLR Procedure to get and insert the data.

What is the best way to insert a List of class instances in SQL 2008 R2 table?

I am using C# in a CLR Procedure to get and insert the data.

I am trying to insert information of computer objects from Active Directory into SQL server table. With System.DirectoryServices, the objects are retrieved in a "SearchResultCollection". I modify each "searchresult" object to my custom class "ComputerObject".

Then I put them in a list List<ComputerObject>, how do I insert each "ComputerObject" as rows in a SQL table ? I don't want to use a single insert statement for each row.

If needed, I can also retrieve the List<ComputerObject> i开发者_运维技巧nto another Data Structure, so that it will be easier to insert into a SQL table.

Also, whenever the CLR Proc runs, data is appended to the table.

Thank you.


Since you are using SQL Server 2008+ and I assume you are using a .NET language, you will probably be interested in using ADO.NET to interact with a database. 2008 allows for table valued parameters and I'd suggest you look at using those as an effective means of smashing a list into a table. Detailed example of using Table Valued Parameters and C# to load data into a database here.

Otherwise, edit your question into something useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消