Connecting Tech Pros Worldwide Forums | Help | Site Map

Updating a database in VB.Net

Newbie
 
Join Date: Mar 2009
Posts: 16
#1: Aug 18 '09
I have a sql database that contains several thousand records that need to be numbered sequentially from 1 through the end. I have tried several different ways and nothing seems to hit the database. I've included my attempts and would greatly appreciate any help.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim cmdSave As New SqlClient.SqlCommand
  3.         'Dim iRC As Integer
  4.         Dim dsD As DataSet
  5.         Dim adD As SqlClient.SqlDataAdapter
  6.         Dim rowD As DataRow
  7.         Dim i As Integer = 1
  8.         Dim sD As String = "select * from table"
  9.         Dim sU As String = ""
  10.         Dim dsMax As DataSet
  11.         Dim adMax As SqlClient.SqlDataAdapter
  12.         Dim sMax As String = "select max(recid) + 1 as maxnum from table"
  13.  
  14.         dsD = New DataSet
  15.         adD = New SqlClient.SqlDataAdapter(sD, gdbRecordSet)
  16.         adD.Fill(dsD, "stuff")
  17.  
  18.         dsMax = New DataSet
  19.         adMax = New SqlClient.SqlDataAdapter(sMax, gdbRecordSet)
  20.         adMax.Fill(dsMax, "max")
  21.  
  22.         Dim maxnum As Double = dsMax.Tables("max").Rows(0)(0)
  23.  
  24.         For Each rowD In dsD.Tables("stuff").Rows
  25.             'rowD.AcceptChanges()
  26.             'rowD("recid") = i
  27.             'rowD.SetModified()
  28.             rowD.BeginEdit()
  29.             rowD("recid") = i
  30.             rowD.EndEdit()
  31.  
  32.             If dsD.HasChanges Then
  33.                 dsD.AcceptChanges()
  34.                 'adD.Update(dsD, "stuff")
  35.                 'adD.Update(dsD.GetChanges())
  36.             End If
  37.             'sU = "update table set recid = '" & i & "' where  recid > '" & i & "'
  38.            i = i + 1
  39.             'cmdSave.Connection = gdbRecordSet
  40.             'cmdSave.CommandText = sU
  41.             'cmdSave.CommandType = CommandType.Text
  42.             'iRC = cmdSave.ExecuteNonQuery
  43.         Next
  44.     End Sub

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,783
#2: Aug 18 '09

re: Updating a database in VB.Net


TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.ly.
Reply

Tags
sql, tables, updating