Click here to Skip to main content
15,886,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to loop through a dataset and save it in SQL server using VB.net.
But i am able to save only the first row in the dataset. Whats wrong?



VB
con.Open()

Dim cmd1 As SqlCommand

For Each Drr As DataRow In DtSet.Tables(0).Rows
    cmd1 = New SqlCommand("INSERT INTO Students (student_fullname, student_branch, student_scholarship) VALUES ('" & Drr(1).ToString & "', '" & Drr(2).ToString & "', '" & Drr(3).ToString & "')", con)
    cmd1.ExecuteNonQuery()
Next
MsgBox("Successfully Saved")
con.Close()
Posted
Comments
PIEBALDconsult 19-Nov-14 13:28pm    
That is nearly the worst way to do that on many levels.

Is there supposed to be one table with many rows? Or many tables with one row each?
Richard Deeming 19-Nov-14 13:43pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Thomas Rones 19-Feb-18 10:22am    
Besides SQL injection, are there any reasons (performance?) not to use a concatenated string?

Irrespective of your question, Do you know that this implementation is susceptible of SQL Injection[^]?

I would suggest you to rather use Stored Procedure[^] or Parameterized Queries[^] in SQL.
 
Share this answer
 
Comments
PIEBALDconsult 19-Nov-14 15:25pm    
No reason for stored procedures.
Manas Bhardwaj 19-Nov-14 15:29pm    
Personally, I would any day use a SP instead of inline queries in C# code. That's another topic how that SP would be implemented. Accepting data in one go or if is it called everytime in a loop.

Having said that, ORMs are more a buzz these days. And would be more handy to have L2S or EF do that for you.
Maciej Los 19-Nov-14 16:02pm    
Why?
[no name] 19-Nov-14 19:37pm    
*lol* I agree. Nobody is willing to use the minimal SQL9x but everybody is willing to make MSQL-specific stored proc....
PIEBALDconsult 19-Nov-14 19:44pm    
Indeed, and the inexperienced keep touting them as the bestest thing since 16-bit operating systems, that they fix all problems just by looking at them.
A plague upon the land they are.
Try something more like this (what I show may not be exactly correct for your needs). If you need to loop through more tables in the set then add another loop.

C#
con.Open()
 
Dim cmd1 As SqlCommand
    cmd1 = New SqlCommand("INSERT INTO Students (student_fullname, student_branch, student_scholarship) VALUES (@student_fullname, @student_branch, @student_scholarship)", con)

cmd1.Parameters.Add ( new SqlParameter ( @student_fullname    , nothing ) )
cmd1.Parameters.Add ( new SqlParameter ( @student_branch      , nothing ) )
cmd1.Parameters.Add ( new SqlParameter ( @student_scholarship , nothing ) ) 

For Each Drr As DataRow In DtSet.Tables(0).Rows
    cmd1.Parameters ( @student_fullname    ).Value = Drr(1) 
    cmd1.Parameters ( @student_branch      ).Value = Drr(2) 
    cmd1.Parameters ( @student_scholarship ).Value = Drr(3) 

    cmd1.ExecuteNonQuery()
Next
MsgBox("Successfully Saved")
con.Close()
 
Share this answer
 

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