Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have same table name with indexable, it's like

Table1
Table2
Table3
Table4
Table5

and I want to use this command in vb.net like

For i= 0 to 5

New SqlCommand("Insert into Table(i) (Column1,Column2) Values(1,2,3...)

Next

How is this possible to make this on every loop changing table index and saving different tables?

Pls. help this.

What I have tried:

Pls. help I need to solve this , Thanks in advance!
Posted
Updated 27-Sep-17 8:34am

1 solution

Try:
VB
Dim cmd As SqlCommand = SqlCommand("INSERT INTO TABLE" + i.ToString() + " Column1,Column2) VALUES(@C1, @C2)", connnection)
cmd.Parameters.AddWithValue("@C1", valuesForColumn1)
cmd.Parameters.AddWithValue("@C2", valuesForColumn2)
cmd.ExecuteNonQuery()
 
Share this answer
 
Comments
Jörgen Andersson 28-Sep-17 5:36am    
Um, don't teach people to use AddWithValue, it can lead to plan cache pollution and type inference errors.
More on that here: http://www.sqlpassion.at/archive/2015/07/20/how-to-pollute-your-plan-cache-with-parameterized-sql-statements/
And here: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
Richard Deeming 28-Sep-17 12:35pm    
It's still a better option than using string concatenation! :)
Jörgen Andersson 28-Sep-17 13:54pm    
Oh yes indeed.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900