Results 1 to 3 of 3

Thread: [2005] Need help with dataset concept

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Question [2005] Need help with dataset concept

    OK, be nice. I am really having an issue with data updating in ADO.NET. I having been reading Visual Basic 2005 Programmer's Reference; Visual Basic 2005 Cookbook; and Visual Basic 2005 Jumpstart. I have been Googling my arse off. I have been searching this forum. But I still don't get it.

    I have a handle on the INSERT and the DELETE in ADO.NET

    In VB6 I would do this.

    Connect to db at start of program, disconect on exit.

    Here is a sample: It does about all the Add/Update code that I would run across.


    VB Code:
    1. 'IF WE MADE IT HERE, ALL IS OK SO WE CAN APPEND
    2.             '==============================
    3.             rs1 = New ADODB.Recordset
    4.             '===============================
    5.             sql = "SELECT * FROM DATACOLLECTION WHERE [BOGUSDATE] = " & "'" & pdate.Date & "'"
    6.             rs1.Open(sql, Cnxn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
    7.             If Not rs1.BOF And Not rs1.EOF Then
    8.                 rs1.MoveFirst()
    9.                 Do
    10.                     '=============================
    11.                     rsTC = New ADODB.Recordset
    12.                     '=============================
    13.                     If rs1.Fields("shift").Value <> 3 Then
    14.                         sql1 = "SELECT * FROM TIMECARDS WHERE [PAYDATE] = " & "'" & pdate.Date & "'"
    15.                         sql1 = sql1 & " AND [EMPNUM] = " & rs1.Fields("EMPLOYEEID").Value
    16.                     Else
    17.                         sql1 = "SELECT * FROM TIMECARDS WHERE [PAYDATE] = " & "'" & System.DateTime.FromOADate(pdate.ToOADate + 1) & "'"
    18.                         sql1 = sql1 & " AND [EMPNUM] = " & rs1.Fields("EMPLOYEEID").Value
    19.                     End If
    20.                     rsTC.Open(sql1, Cnxn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
    21.                     If Not rsTC.BOF And Not rsTC.EOF Then
    22.                         rsTC.MoveFirst()
    23.                         rsTC.Fields("SHIFTDATE").Value = rs1.Fields("bogusdate").Value
    24.                         rsTC.Fields("PAYDATE").Value = rs1.Fields("PAYDATE").Value
    25.                         rsTC.Fields("PAYCODE").Value = "R"
    26.                         rsTC.Fields("dept").Value = rs1.Fields("DEPT1").Value & ""
    27.                         If IsDBNull(rs1.Fields("DTIME1").Value) Then
    28.                             rsTC.Fields("hours").Value = 0
    29.                         Else
    30.                             rsTC.Fields("hours").Value = rs1.Fields("DTIME1").Value
    31.                         End If
    32.                         rsTC.Fields("dept2").Value = rs1.Fields("dept2").Value & ""
    33.                         If IsDBNull(rs1.Fields("DTIME2").Value) Then
    34.                             rsTC.Fields("HOURS2").Value = 0
    35.                         Else
    36.                             rsTC.Fields("HOURS2").Value = rs1.Fields("DTIME2").Value
    37.                         End If
    38.                         rsTC.Fields("DEPT3").Value = rs1.Fields("DEPT3").Value & ""
    39.                         If IsDBNull(rs1.Fields("DTIME3").Value) Then
    40.                             rsTC.Fields("HOURS3").Value = 0
    41.                         Else
    42.                             rsTC.Fields("HOURS3").Value = rs1.Fields("DTIME3").Value
    43.                         End If
    44.                     Else
    45.                         rsTC.AddNew()
    46.                         rsTC.Fields("empnum").Value = rs1.Fields("EMPLOYEEID").Value
    47.                         rsTC.Fields("SHIFTDATE").Value = rs1.Fields("bogusdate").Value
    48.                         rsTC.Fields("PAYDATE").Value = rs1.Fields("PAYDATE").Value
    49.                         rsTC.Fields("PAYCODE").Value = "R"
    50.                         rsTC.Fields("dept").Value = rs1.Fields("DEPT1").Value & ""
    51.                         If IsDBNull(rs1.Fields("DTIME1").Value) Then
    52.                             rsTC.Fields("hours").Value = 0
    53.                         Else
    54.                             rsTC.Fields("hours").Value = rs1.Fields("DTIME1").Value
    55.                         End If
    56.                         rsTC.Fields("dept2").Value = rs1.Fields("dept2").Value & ""
    57.                         If IsDBNull(rs1.Fields("DTIME2").Value) Then
    58.                             rsTC.Fields("HOURS2").Value = 0
    59.                         Else
    60.                             rsTC.Fields("HOURS2").Value = rs1.Fields("DTIME2").Value
    61.                         End If
    62.                         rsTC.Fields("DEPT3").Value = rs1.Fields("DEPT3").Value & ""
    63.                         If IsDBNull(rs1.Fields("DTIME3").Value) Then
    64.                             rsTC.Fields("HOURS3").Value = 0
    65.                         Else
    66.                             rsTC.Fields("HOURS3").Value = rs1.Fields("DTIME3").Value
    67.                         End If
    68.                         'GO TO EMPLOYEE FILE AND GET DATA
    69.                         '===================================
    70.                         rsEmployee = New ADODB.Recordset
    71.                         '===================================
    72.                         sql2 = "SELECT * FROM EMPFILE WHERE [EMPNUM] = " & rs1.Fields("EMPLOYEEID").Value
    73.                         rsEmployee.Open(sql2, CnxnTechniSQL, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
    74.                         If Not rsEmployee.BOF And Not rsEmployee.EOF Then
    75.                             rsTC.Fields("shift").Value = rsEmployee.Fields("shift").Value
    76.                             rsTC.Fields("WAGE").Value = rsEmployee.Fields("WAGE").Value
    77.                             rsTC.Fields("JOBWAGE").Value = rsEmployee.Fields("JOBWAGE").Value
    78.                         End If
    79.                         rsEmployee.Close()
    80.                         rsEmployee = Nothing
    81.                         '=======================
    82.                     End If
    83.                     rsTC.Update()
    84.                     rsTC.Close()
    85.                     rsTC = Nothing
    86.                     '=================
    87.                     rs1.MoveNext()
    88.                 Loop While Not rs1.EOF

    If someone can provide code that performs the same functionality, I should be make all the necessary coding changes to use it as a template. For some reason, it is just not getting past my thick skull.

  2. #2

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [2005] Need help with dataset concept

    I think I may have stumbled across soemthing. I will try it tomorrow.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Need help with dataset concept

    As tempting as it is to translate that code (NOT) updating data is VERY simple with ADO.NET. You need one Connection, one DataAdapter, one DataTable and four Commands.

    The DataAdapter has four properties (SelectCommand, DeleteCommand, InsertCommnad and UpdateCommand) to which the four Commands are assigned. Note that you only need the ones you're going to use, e.g. if you're only going to retrieve and update data then you only need the SelectCommand and the UpdateCommand.

    Each Command has a Connection property, to which you assign the Connection object.

    When you call the Fill method of the DataAdapter it executes the SelectCommand and populates the specified DataTable.

    When you call the Update method of the DataAdapter it checks the specified DataTable for changes. If it finds any deleted rows it executes the DeleteCommand, if it finds any new rows it executes the InsertCommand and if it finds any modified rows it executes the UpdateCommand.

    Each Command has an SQL statement associated with it through its CommandText property. This could be SQL code or the name of a stored procedure. If that SQL code contains any parameters then you must add to the Command's Parameters collection for each one.

    Now, VS 2005 has additional data access features including Data Sources and TableAdapters, but they are just a specialisation of what I've just described. I suggest that you play with code to begin with and use the appropriate types in a simple scenario. Once you understand the concepts, then I'd suggest moving on to the IDE features so that you know what's going on behind the scenes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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