I was trying to insert items in a listview in a database. If I try to insert every records seperately, it takes long if there are more records (even more than 5).
Im currently using this code:
For Each ls As ListViewItem In ListItems.Items
strSQL = String.Format("insert into tbltrans (transid,itemcode,itemname,qty,price,[total],btw) values ('{0}','{1}','{2}',{3},{4},'{5}','{6}')", CStr(txtTransId.Text), CStr(ls.Tag), ls.SubItems(0).Text, CDbl(ls.SubItems(1).Text), CDbl(ls.SubItems(2).Text), CDbl(ls.SubItems(3).Text), ((ls.SubItems(5).Text)))
objDal.ExecuteQuery(strSQL)
Next
So, what I want to do is execute all of the items in one sql query.
I tried this, but didn’t work:
strSQL = "insert into tbltrans (transid,itemcode,itemname,qty,price,[total],btw) values "
For Each ls As ListViewItem In ListItems.Items
strSQL += tring.Format("('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", CStr(txtTransId.Text), CStr(ls.Tag), ls.SubItems(0).Text, CDbl(ls.SubItems(1).Text), CDbl(ls.SubItems(2).Text), CDbl(ls.SubItems(3).Text), ((ls.SubItems(5).Text)))
Next
objdal.executequery(strSQL)
It says that it’s missing semicolon(;) at the end of the statement, I tried adding them in the records and also (strsql & “;”), then it gives syntax error.
can anyone help please?
You can do a little better by re-using the same command/connection object and same (not re-constructed) sql string like this (I had to guess at column types and lengths):