Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, asked before but didn't get exactly what i was looking for soooooo
Any of you vb.net experts (with some vb6 experience) got a few minutes to convert this VB6 code to VB.net? It would help me a lot to see how this is done in .net

I have a lot of need for something like the code below that will scan a table and make corrections to the database, user error, whatever.

From what i've read, you'd need one sub/function to read all the records and another for the update - and each of these would need to be coded specifically for each table

The code below was just tossed together to get the point across (IE the GUID function I know exists in .net - where in vb6 i had a function that generated the code)

Many thanks in advance!

Global DB As New ADODB.Connection
Global RS As New Recordset
Sub Main
    OpenDB "SQLServer", "UserName", "Password", "Database", DB
    OpenRecordset rsXRef, "Select * from XRef", DB
    ' --------- This is the point in question
    With rsXRef
        While Not .EOF                        ' Loop through all the records
            If Len(RTrim(!GUID)) < 5 Then  ' If an edit is required...
                !GUID = GUID                  ' Make the update
            End If
            .Update                           ' and update the record
            .MoveNext                         ' move to the next record and repeat...
        Wend
    End With
    ' --------- 
End Sub
Function GUID
    GUID = "SomeRandomGUID"
End Function
Function OpenDB(SQLServer, User, Password, Database, dbConnection As ADODB.Connection) As Boolean
    dbConnection.ConnectionString = "Provider='SQLOLEDB.1' ;User id='" & User & "' ;password='" & Password & "' ;Persist Security info='True' ; Data Source='" & SQLServer & "'; Initial Catalog='" & Database & "'; Connect Timeout=2;"
    dbConnection.Open
    OpenDB = True
End Function
Public Function OpenRecordset(RS As Recordset, SQL As String, Conn As ADODB.Connection) As Boolean
    OpenRecordset = True
    With RS 
        On Error Resume Next
        If .State = adStateOpen Then
            .Close
        End If
        .CursorType = adOpenDynamic
        .LockType = adLockOptimistic
        .CursorLocation = adUseClient
        .Open SQL, Conn
    End With
    Exit Function
End Function
Posted
Comments
bayotle 4-Jul-13 0:58am    
Looks like something to look into, i'll dig around and see what i can pull up....

------------

Yeah, looked it up, looks like it's gonna be a bit of a learning curve, thanks for the pointer!
---------
Got it working, again, thanks but OMG, 36+ lines to do what i could do in probably 5!
All i can say is after being a programmer for almost 40 years, .net sux!!!!! It's back to vb6 after this project!
This is 1000x worse than cobol, all the repetition kills it....

1 solution

This is simple code for conversion.

You can use a DataAdapter to fill the dataset with records from the table that you need to update.

Loop through the records like done in the vb code and update the data in the dataset.

Then after the loop, just call the Update method of Dataset and the changes will be posted to database.

I hope this is what you where looking for and that you will be knowing how to convert the OpenDB function coz that is simply connecting to database.
 
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