Tuesday, March 20, 2012

parametized inserts in a loop

Hi,

I have a for next loop that I am using to write some data into a table.

The problem I have is one of the fields is an @.parameter

The relevant bit of VB.net code is:

Dim dbpm_theDateTimeAs System.Data.IDataParameter =New System.Data.SqlClient.SqlParameter

dbConnection.Open()

For intCounterAsInteger = 0To ds.Tables(0).Rows.Count - 1

querystring ="Insert into tblActionsToDo ([TheDT], [username]) VALUES (@.theDateTime, '" & username &"')"

dbpm_theDateTime =New System.Data.SqlClient.SqlParameter

dbpm_theDateTime.ParameterName ="@.theDateTime"

dbpm_theDateTime.Value = tmpstrDT

dbpm_theDateTime.DbType = System.Data.DbType.DateTime

dbCommand.Parameters.Add(dbpm_theDateTime)

dbCommand.CommandText = querystring

rowsAffected = dbCommand.ExecuteNonQuery

Next

The first time through the for next loop, everything works fine. The next time though I get an error that @.theDateTime has already been defined. To be fair, it is true; but how do I get round this? I've tried making the @.parameter a string so that I can manipulate it as "@.parameter" & intcounter, and thus have a uniquely-named varaible for every loop, but it doesn't work.

thanks for any pointers

I fixed this one out on my own.

All I had to do was move everything outside of the loop except for

dbpm_theDateTime.Value = tmpstrDT

and it works just fine :)

No comments:

Post a Comment