5,699,997 members and growing! (25,106 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples License: The Code Project Open License (CPOL)

Inserting value of Collection into Database

By bhadeliaimran

Inserting value of Collection into Database
SQL, C# (C# 3.0, C#), Windows (Windows, WinXP, Win2003), .NET (.NET, .NET 3.5), SQL Server (SQL 2005, SQL Server), Visual Studio (VS2008, Visual Studio), ADO.NET, ASP.NET

Posted: 7 Aug 2008
Updated: 7 Aug 2008
Views: 2,563
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.77 Rating: 3.27 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
1 vote, 14.3%
3
2 votes, 28.6%
4
2 votes, 28.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

The article/code will help the people who require inserting multiple object or list of object same time with single database interaction.

Background

We always require thing like this, when you need to Insert list of object at the same time with single database operation. Here is the code which includes the same functionality by using feature Extension Method" of C# 3.0.

Using the code

This code contains the logic of creating XML by overriding ToString method

public override string ToString()
{
    return string.Format("<UserImage HashCode=\"{0}\" Id=\"{1}\" UserId=\"{2}\" OriginalName=\"{3}\" StorageName=\"{4}\" />",
    this.GetHashCode(), this.Id, this.UserId, this.OriginalName, this.StorageName);
}        

This method will generate the XML representation of the UserImage Class, The UserImage class contains the basic property that require to store image, you can change it accordingly and also dont forgot to change the ToString method

So now you have collection of UserImage, and need to generate XML for all the UserImage which are inside collection.

List<UserImage> imageList = new List<UserImage>();
//Add some images
imageList.Add(new UserImage(1, 786, "Test1.jpg", @"C:\Temp\User_786_1.jpg"));
imageList.Add(new UserImage(2, 786, "Test2.jpg", @"C:\Temp\User_786_2.jpg"));
imageList.Add(new UserImage(3, 786, "Test3.jpg", @"C:\Temp\User_786_3.jpg"));
imageList.Add(new UserImage(4, 786, "Test4.jpg", @"C:\Temp\User_786_4.jpg"));
imageList.Add(new UserImage(5, 786, "Test5.jpg", @"C:\Temp\User_786_5.jpg"));
imageList.Add(new UserImage(6, 786, "Test6.jpg", @"C:\Temp\User_786_6.jpg"));

imageList is ready, we also have ToString method which gives the XML of UserImage. Now what we have to do is... we have to iterate thru collection and get the XML of every object in imageList. For this we user Extension Methods, and here is the ToXMLString extension method, whichi applies on List<UserImage>.

public static string ToXMLString(this List<UserImage> list)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<UserImages>");

    foreach (UserImage image in list)
    {
        sb.Append    (image.ToString());
    }

    sb.Append("</UserImages>");
    return sb.ToString();
}

In the above extension method, we call ToString method of UserImage to generate individual XML string and then Apped the parent tags

so at the end you can have XML just like bellow

<UserImages>
    <UserImage HashCode="17798814" Id="1" UserId="786" OriginalName="Test1.jpg" StorageName="C:\Temp\User_786_1.jpg" /> 
    <UserImage HashCode="38230786" Id="2" UserId="786" OriginalName="Test2.jpg" StorageName="C:\Temp\User_786_2.jpg" /> 
    <UserImage HashCode="64649019" Id="3" UserId="786" OriginalName="Test3.jpg" StorageName="C:\Temp\User_786_3.jpg" /> 
    <UserImage HashCode="15452120" Id="4" UserId="786" OriginalName="Test4.jpg" StorageName="C:\Temp\User_786_4.jpg" /> 
    <UserImage HashCode="56059077" Id="5" UserId="786" OriginalName="Test5.jpg" StorageName="C:\Temp\User_786_5.jpg" /> 
    <UserImage HashCode="36389945" Id="6" UserId="786" OriginalName="Test6.jpg" StorageName="C:\Temp\User_786_6.jpg" /> 
</UserImages>

Now Sql part, we need to pass the string to stored procedure and then in procedure we just need to select values form XML node and can directly get inserted to table.

Here we go,

INSERT INTO @UserImageTable 

SELECT 
    UserImage.value ('@Id[1]', 'VARCHAR(10)') as Id,
    UserImage.value ('@UserId[1]', 'VARCHAR(10)') as UserId,
    UserImage.value ('@OriginalName[1]', 'VARCHAR(100)') as OriginalName,
    UserImage.value ('@StorageName[1]', 'VARCHAR(100)') as StorageName
FROM
@UserImages.nodes('/UserImages/UserImage') v(UserImage)

The @UserImageTable is the temparory table.

DECLARE @UserImageTable AS TABLE
(
    Id BIGINT,
    UserId BIGINT,
    OriginalName VARCHAR(100),
    StorageName VARCHAR(100)
)

Points of Interest

The good thing is Extension Methods which give us freedom to write extenstion of any given class, more over the XML a strong part of SQL Server 2005, however there is the limitation of XML datatype which you can find it here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

bhadeliaimran


Profiles : Code Project, ASP.NET Forums
Blog : Knowledgebase World
Current Company : Softwebsolution INC
User Group : Ahmedabad SQLServer UserGroup
Other : Microsoft Certified Technology Specialist (MCTS)

Occupation: Team Leader
Company: Softweb Solutions
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralLINQ?membermerlin9815:02 8 Aug '08  
GeneralRe: LINQ?memberBhadeliaImran1:07 10 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Aug 2008
Editor:
Copyright 2008 by bhadeliaimran
Everything else Copyright © CodeProject, 1999-2008
Web07 | Advertise on the Code Project