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!