Results 1 to 15 of 15

Thread: vb.net how to edit field value like vb6?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,725

    vb.net how to edit field value like vb6?

    vb6:
    Code:
    rs.open "select * from abc where id>1",conn
    while not rs.eof
    rs("field2")=rs("id")*2
    rs.update
    rs.movenext
    wend
    There are too many lines of code, is there an easier way?

    Code:
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim TableName As String
            TableName = "ExeList"
            Dim query As String = "SELECT * FROM " & TableName & " WHERE ID<10"
            Dim dataSetA As New DataSet()
            Using connection As New OleDbConnection(connectionString)
                Dim adapter As New OleDbDataAdapter(query, connection)
    
    
                adapter.Fill(dataSetA, TableName)
            End Using
          
            Dim DataTable As DataTable = dataSetA.Tables(TableName)
            Dim FieldId As Long
            For Each Field As DataColumn In DataTable.Columns
                FieldId = FieldId + 1
                Debug.Print("Field[" & FieldId.ToString & "]=" & Field.ToString)
            Next
            For Each row As DataRow In DataTable.Rows
                Dim id As Integer = CInt(row("ID"))
                Debug.Print("id=" + id.ToString)
                If id = 1 Or id = 2 Or id = 3 Then
                  
                    row("INFO") = (id * 10).ToString
                End If
            Next
           
    
            Using connection As New OleDbConnection(connectionString)
                Dim adapter As New OleDbDataAdapter(query, connection)
                Dim builder As New OleDbCommandBuilder(adapter)
    
                adapter.Update(dataSetA, TableName)
            End Using
        End Sub
    Last edited by xiaoyao; Dec 4th, 2023 at 08:59 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,452

    Re: vb.net how to edit field value like vb6?

    DataTables columns have an Expression property that you could probably use if it’s one row one field equals another field in the same row, multiplied by 2 or something similar.

  3. #3

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,424

    Re: vb.net how to edit field value like vb6?

    Quote Originally Posted by xiaoyao View Post
    vb6:
    Code:
    rs.open "select * from abc where id>1",conn
    while not rs.eof
    rs("field2")=rs("id")*2
    rs.update
    rs.movenext
    wend
    There are too many lines of code, is there an easier way?

    Code:
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim TableName As String
            TableName = "ExeList"
            Dim query As String = "SELECT * FROM " & TableName & " WHERE ID<10"
            Dim dataSetA As New DataSet()
            Using connection As New OleDbConnection(connectionString)
                Dim adapter As New OleDbDataAdapter(query, connection)
    
    
                adapter.Fill(dataSetA, TableName)
            End Using
          
            Dim DataTable As DataTable = dataSetA.Tables(TableName)
            Dim FieldId As Long
            For Each Field As DataColumn In DataTable.Columns
                FieldId = FieldId + 1
                Debug.Print("Field[" & FieldId.ToString & "]=" & Field.ToString)
            Next
            For Each row As DataRow In DataTable.Rows
                Dim id As Integer = CInt(row("ID"))
                Debug.Print("id=" + id.ToString)
                If id = 1 Or id = 2 Or id = 3 Then
                  
                    row("INFO") = (id * 10).ToString
                End If
            Next
           
    
            Using connection As New OleDbConnection(connectionString)
                Dim adapter As New OleDbDataAdapter(query, connection)
                Dim builder As New OleDbCommandBuilder(adapter)
    
                adapter.Update(dataSetA, TableName)
            End Using
        End Sub
    If you are using a vaguely modern version of .Net then you could also consider something like the Entity Framework instead of using DataTables etc.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,504

    Re: vb.net how to edit field value like vb6?

    Create your connection and adaptor once ...

    Code:
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim TableName As String
            TableName = "ExeList"
            Dim query As String = "SELECT * FROM " & TableName & " WHERE ID<10"
            Dim dataSetA As New DataSet()
            Using connection As New OleDbConnection(connectionString)
                Dim adapter As New OleDbDataAdapter(query, connection)
                Dim builder As New OleDbCommandBuilder(adapter)
    
    
                adapter.Fill(dataSetA, TableName)
    
          
                Dim DataTable As DataTable = dataSetA.Tables(TableName)
                Dim FieldId As Long
                For Each Field As DataColumn In DataTable.Columns
                    FieldId = FieldId + 1
                    Debug.Print("Field[" & FieldId.ToString & "]=" & Field.ToString)
                Next
                For Each row As DataRow In DataTable.Rows
                    Dim id As Integer = CInt(row("ID"))
                    Debug.Print("id=" + id.ToString)
                    If id = 1 Or id = 2 Or id = 3 Then
                      
                        row("INFO") = (id * 10).ToString
                    End If
                Next
               
    
                adapter.Update(dataSetA, TableName)
            End Using
        End Sub
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: vb.net how to edit field value like vb6?

    jmc has decent topics on VB.NET database or data related programming here code bank submissions
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,902

    Re: vb.net how to edit field value like vb6?

    One thing that hasn't been covered is that VB6 and VB.NET have very different relationships to a database. VB6 tended to keep an active connection to the database. You could look at a row, fiddle with the row, update the row, then move on to the next row, all within the actual database. In .NET, the expectation is that you open a connection, get your data, close the connection, then work with the data. This has several advantages, especially in a multi-user environment, but it does mean that there are some things in .NET that take more code than what was possible in VB6. You gain in isolation, you lose the ability to make certain changes. It's a tradeoff.
    My usual boring signature: Nothing

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,673

    Re: vb.net how to edit field value like vb6?

    The VB6 example doesn't appear to be doing anything with the data other than updating the field2's value equal to id's value multiplied by 2. If that is the case, why not simply run a single update statement and call it a day:
    Code:
    ' Dim connectionString = "change me"
    Using connection As New OleDbConnection(connectionString)
        connection.Open()
    
        Dim sql As String = "UPDATE abc SET field2 = id * 2 WHERE id > 1"
        Using command As New OleDbCommand(sql, connection)
            command.ExecuteNonQuery()
        End Using
    End Using
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,902

    Re: vb.net how to edit field value like vb6?

    Yeah, that would be even quicker.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,725

    Re: vb.net how to edit field value like vb6?

    Code:
      Dim DataTable As DataTable = dataSetA.Tables(TableName)
               *******
    
                adapter.Update(dataSetA, TableName)
    VB6 is used to operate the database. You don't have to use any forms.
    For example, open and query through two tables. Records obtained and updated directly.
    If.net asks you to indicate the name of the table, do you specify one or two?

    For example, you write a class, have an event, and then call it in the form.
    Add handle, and you have to write the code of the event manually, which is very troublesome.
    Vb6 Just select the event name in the drop-down box to generate the code directly.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,673

    Re: vb.net how to edit field value like vb6?

    All of what you're saying is available in VB.NET, what is it exactly that you want to do?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,902

    Re: vb.net how to edit field value like vb6?

    Yes, you CAN make it more difficult if you want to. Sometimes you even have to. Other times, you really don't.

    In VS there are a variety of features to add event handlers. I don't use all of them. I may not even know about all of them. You can also write out event handlers and use AddHandler to hook them up. All kinds of different options. Just choose the one that works best for you.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,725

    Re: vb.net how to edit field value like vb6?

    I got a.NET database operation class, which is much easier than the original one.


    Code:
            Mdb.OpenAccess(Application.StartupPath & "\ABCdb.mdb")
    
            Mdb.GetRs("select * from [01ALLpc] where id=0")
            Mdb.AddNew()
            Mdb.Item("PcName") = "aa"
            Mdb.Item("UserName") = "tom"
    
            Mdb.AddNew()
            Mdb.Item("PcName") = "bb"
            Mdb.Item("UserName") = "Lily"
    
            Mdb.Update()

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,725

    Re: vb.net how to edit field value like vb6?

    LINQ to SQL provides a nice, concise way to model the data layer of an application. Once the data model is defined, it can be queried, inserted, updated, and deleted easily and efficiently

    https://asp-blogs.azurewebsites.net/...-to-sql-part-1

    Code:
    Dim db as New LINQtoSQLDataContext 
    
    Dim oArticle as New article // table name 
    oArticle.Body = "Some Text" 
    oArticle.Title = "Some Title" 
    oArticle.Author = "Some Author" 
    
    db.articles.Add(oArticle) // this is the line
    db.articles.InsertOnSubmit(oArticle) 
    db.SubmitChanges()
    Last edited by xiaoyao; Dec 8th, 2023 at 08:03 PM.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,725

    Re: vb.net how to edit field value like vb6?

    rs'[user]="abc" rs.user="abc" rs! user="abc" rs("user")="abc" If these four formats are supported, it must be the most perfect. rs.new rs.dim The main reason is that many keywords in VB6 are restricted, and a case will be converted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width