Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Re: Multiple inserts question

Reply
Thread Tools

Re: Multiple inserts question

 
 
Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
Guest
Posts: n/a
 
      01-22-2007
Considering ADO.NET has built-in connection pooling, recreating the database
connection each time through the loop is actually a lot more efficient than
you might think.
So if it isn't broken, perhaps you shouldn't fix it...
But if you really need to buy some performance you might want to look into
batching your commands together so there is only one round trip to the
database instead of gv.rows.count-1.

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net


"Yankee Imperialist Dog" <>
wrote in message news:BD23646F-5211-4932-AF8C-...
> Given that i'm passing a GridView to a function (i have a reason).
> Is this the best way to do multiple inserts?
> The following works, but it's seems there should be a better way then to
> keep recreating the connection and command variables?
> Any Ideas?
> private function fn_MyInsert(ByVal gv As GridView, ByVal iStratID As
> Integer) As Boolean
> For i As Integer = 0 To gv.Rows.Count - 1
> ' Response.Write(i.ToString)
> Dim chk As CheckBox = gv.Rows(i).FindControl("chk_Included")
> If chk.Checked Then
> Dim sqlcon2 As New SqlConnection(constr)
> Dim sqlcmd2 As SqlCommand = sqlcon2.CreateCommand
> sqlcmd2.CommandType = Data.CommandType.StoredProcedure
> sqlcmd2.CommandText = "sp_AddPortfolioToStrategy"
> sqlcmd2.Parameters.AddWithValue("@iStratID", iStratID)
> sqlcmd2.Parameters.AddWithValue("@iPorID",
> gv.DataKeys(i).Value)
> sqlcon2.Open()
> sqlcmd2.ExecuteNonQuery()
> sqlcon2.Close()
> sqlcmd2.Dispose()
> sqlcon2 = Nothing
> End If
> Next
> RETURN True
> End Function
> --
> Share The Knowledge. I need all the help I can get and so do you!


 
Reply With Quote
 
 
 
 
Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
Guest
Posts: n/a
 
      01-22-2007
Batch updates are detailed here:
http://blogs.msdn.com/dataaccess/arc...19/420065.aspx
http://msdn2.microsoft.com/en-us/library/ms810297.aspx
http://weblogs.asp.net/despos/archiv...14/131714.aspx
http://www.developer.com/db/article.php/3494396

--
I hope this helps,
Steve C. Orr,
MCSD, MVP, CSM, ASPInsider
http://SteveOrr.net


"Yankee Imperialist Dog" <>
wrote in message news:3D6EB09B-F93D-49A2-9389-...
> Thanks for the reply, it is very much appreciated and it appears to be a
> pointer to the answer, but it still leaves a question: How does one go
> about
> "batching commands together?"
> (given thay are sent to SQL server 2005, usinf dot.net 2.0)
>
> Thanks
> --
> Share The Knowledge. I need all the help I can get and so do you!
>
>
> "Steve C. Orr [MCSD, MVP, CSM, ASP Inside" wrote:
>
>> Considering ADO.NET has built-in connection pooling, recreating the
>> database
>> connection each time through the loop is actually a lot more efficient
>> than
>> you might think.
>> So if it isn't broken, perhaps you shouldn't fix it...
>> But if you really need to buy some performance you might want to look
>> into
>> batching your commands together so there is only one round trip to the
>> database instead of gv.rows.count-1.
>>
>> --
>> I hope this helps,
>> Steve C. Orr,
>> MCSD, MVP, CSM, ASPInsider
>> http://SteveOrr.net
>>
>>
>> "Yankee Imperialist Dog" <>
>> wrote in message
>> news:BD23646F-5211-4932-AF8C-...
>> > Given that i'm passing a GridView to a function (i have a reason).
>> > Is this the best way to do multiple inserts?
>> > The following works, but it's seems there should be a better way then
>> > to
>> > keep recreating the connection and command variables?
>> > Any Ideas?
>> > private function fn_MyInsert(ByVal gv As GridView, ByVal iStratID As
>> > Integer) As Boolean
>> > For i As Integer = 0 To gv.Rows.Count - 1
>> > ' Response.Write(i.ToString)
>> > Dim chk As CheckBox = gv.Rows(i).FindControl("chk_Included")
>> > If chk.Checked Then
>> > Dim sqlcon2 As New SqlConnection(constr)
>> > Dim sqlcmd2 As SqlCommand = sqlcon2.CreateCommand
>> > sqlcmd2.CommandType = Data.CommandType.StoredProcedure
>> > sqlcmd2.CommandText = "sp_AddPortfolioToStrategy"
>> > sqlcmd2.Parameters.AddWithValue("@iStratID", iStratID)
>> > sqlcmd2.Parameters.AddWithValue("@iPorID",
>> > gv.DataKeys(i).Value)
>> > sqlcon2.Open()
>> > sqlcmd2.ExecuteNonQuery()
>> > sqlcon2.Close()
>> > sqlcmd2.Dispose()
>> > sqlcon2 = Nothing
>> > End If
>> > Next
>> > RETURN True
>> > End Function
>> > --
>> > Share The Knowledge. I need all the help I can get and so do you!

>>
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about parallel processing for bulk inserts into DB harshu010 Python 0 05-28-2008 10:45 AM
DBI:mysql how to do multiple inserts/modification? Tomasz Chmielewski Perl Misc 3 02-13-2008 08:08 PM
multiple record inserts Jim ASP .Net 3 01-18-2008 06:42 PM
Common question about inserts Machina3317 DVD Video 1 07-13-2004 09:55 PM
newbie question: onchange select box inserts image to textarea lawrence Javascript 2 06-02-2004 03:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57