Results 1 to 7 of 7

Thread: Trying to learn some database techniques - deleting records

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    Trying to learn some database techniques - deleting records

    I'd like some help understanding why some of this works. Using the MS Access version of Northwind.mdb and VB2008 in VS2008.

    I am pulling the customer records and displaying them on a form in text boxes and one combobox for the country.

    I am populating the form with the following code
    VB2008 Code:
    1. Public Class frmMain
    2.  
    3.    Private strFilename As String
    4.    Private AccConn As New OleDbConnection
    5.    Private mybindingsource As New BindingSource
    6.    Private AccCmd As OleDbCommand = New OleDbCommand
    7.    Private AccAdapter As OleDbDataAdapter = New OleDbDataAdapter
    8.    Private AccDS As DataSet = New DataSet
    9.  
    10.    Private Sub mnuCustomerView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCustomerView.Click
    11.       pnlCustRecords.Visible = True
    12.       pnlCustRecords.Enabled = True
    13.  
    14.       'setup toolbar
    15.       tsNew.ToolTipText = "Add a New Customer"
    16.       tsFirst.ToolTipText = "First Customer"
    17.       tsNext.ToolTipText = "Next Customer"
    18.       tsPrevious.ToolTipText = "Previous Customer"
    19.       tsLast.ToolTipText = "Last Customer"
    20.       tsNew.Enabled = True
    21.       tsFirst.Enabled = True
    22.       tsNext.Enabled = True
    23.       tsPrevious.Enabled = True
    24.       tsLast.Enabled = True
    25.  
    26.       'setup buttons
    27.       btnSave.Enabled = False
    28.  
    29.       'get data from database
    30.       AccCmd.CommandText = "SELECT CustomerID, CompanyName, ContactName, " _
    31.                               & "ContactTitle, Address, City, Region, " _
    32.                               & "PostalCode, Country, Phone, Fax " _
    33.                               & "FROM Customers " _
    34.                               & "ORDER BY CompanyName"
    35.  
    36.       AccCmd.Connection = AccConn
    37.  
    38.       AccAdapter.SelectCommand = AccCmd
    39.       AccDS.Clear()
    40.       AccAdapter.Fill(AccDS, "Customers")
    41.  
    42.       mybindingsource.DataSource = AccDS.Tables(0)
    43.  
    44.       'assign values to textboxes
    45.       txtCustID.DataBindings.Add("Text", mybindingsource, "CustomerID")
    46.       txtCompany.DataBindings.Add("Text", mybindingsource, "CompanyName")
    47.       txtContact.DataBindings.Add("Text", mybindingsource, "ContactName")
    48.       txtContTitle.DataBindings.Add("Text", mybindingsource, "ContactTitle")
    49.       txtAddress.DataBindings.Add("Text", mybindingsource, "Address")
    50.       txtCity.DataBindings.Add("Text", mybindingsource, "City")
    51.       txtProv.DataBindings.Add("Text", mybindingsource, "Region")
    52.       txtPostalCode.DataBindings.Add("Text", mybindingsource, "PostalCode")
    53.       cboCountry.DataBindings.Add("Text", mybindingsource, "Country")
    54.       txtTel.DataBindings.Add("Text", mybindingsource, "Phone")
    55.       txtFax.DataBindings.Add("Text", mybindingsource, "Fax")
    56.  
    57.       tsPosition.Text = (1 + mybindingsource.Position).ToString
    58.  
    59.  
    60.    End Sub

    So the above code is working fine and pulls the data from the database and allows me to use toolbar buttons to move between records. This was part of what I wanted.

    I want to be able to show a record and then delete it. So I put a record into the customer table that shows in my form. I can then delete it with the code below (I needed to add a record that didn't have orders as there is referential integrity set up in this file at the database level).

    VB2008 Code:
    1. Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click, mnuCustomerDelete.Click
    2.       'deletes current record
    3.       'TODO: manage referential integrity and handle orders and order items
    4.  
    5.       Dim strSQLText As String
    6.       Dim intTotalRecords As Long
    7.  
    8.       strSQLText = "DELETE FROM Customers " _
    9.                               & "WHERE CustomerID = '" & txtCustID.Text & "'"
    10.  
    11.       Dim strSQL As New OleDbCommand(strSQLText, AccConn)
    12.  
    13.       intTotalRecords = strSQL.ExecuteNonQuery
    14.  
    15.       mybindingsource.RemoveCurrent()
    16.       mybindingsource.ResetCurrentItem()
    17.  
    18.  
    19.       mybindingsource.ResetBindings(False)
    20.  
    21.    End Sub

    What I found was that I had to remove it from both the database and the bindingsource separately using the code above. This does seem to work but I am missing the concepts of why I need to do both if a bindingsource is used. I thought a binding source binds the data from the database to the fields.

    So my main questions are:

    1) Is the above delete code correct?
    2) If not, what would you suggest as a better alternative?
    3) Is there anything else I am missing in this attempt to look at some data and then delete a record?

    Thanks for taking the time to read through this rather long post. I appreciate it. rasinc

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Trying to learn some database techniques - deleting records

    Because it is not the job of the database to throw DML "events/notifications" to the front end so they can update their copies of the data.

    Following your logic that would be a performance and synchronization nightmare; for each DML a user does all other users (possibly hundreds) will have to be notified by the database despite the fact that changes have not yet been COMMITed and all other users are in turn making their own changes. In your case, rollback notification will also have to be sent and processed by your app (revert item removed earlier); how are you supposed to do that unless DB resends record that wasn't deleted anymore (why not just requery or don't remove uncommited items in the first place)?

    It would be best to make up your mind whether you will delete via abstration layer (e.g. object performs its job by sending out relevant DML msg to db), or via direct DML to DB (in which case its your responsibility to refresh/maintain front end list). In both cases, sync is only in one direction.
    Last edited by leinad31; Nov 12th, 2009 at 10:43 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    Re: Trying to learn some database techniques - deleting records

    Thanks for the response. I am not sure I really understand everything you said but if I rephrase a little, the code I have written does appear to be disconnected from the database. I have pulled data and am able to move through records on a form.

    I have been able to delete by sending DML instructions to the database (which I assume is your reference "via direct DML to DB"). So I just need to find an adequate way to requery the database after the delete. And if I am going to use this in a multi-user environment I will have to put in some validation of data in some way to confirm that a user is still working with the correct data and that it hasn't been changed since the original query. I assume if two users query the data and try to delete the same record at the same time, I don't really need to let the user know it's not there anymore anyway but should I?

    Have I got this right so far?

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

    Re: Trying to learn some database techniques - deleting records

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    Re: Trying to learn some database techniques - deleting records

    Thanks again jmcilhinney. This article looks familiar but I believe when I looked at it before I didn't really understand what a DataAdapter and DataSet were. I am starting to understand and use of them more so this information is now more pertinent and timely. It helps to remember the names of these things as well.

    So basically in any updating of a database in a multi-user environment, the idea is to check in some way whether the original data is still the same and if so, go ahead and update. If not, then someone else got there first and there has to be a decision made to update/delete or not. I assume this can only be answered by the end users/programmers dependent on the data itself, not based on generally accepted programming guidelines.

    I can see a case where users enter two customers but they are the same customer. One person may be recording a sale for that second customer when the manager decides to merge the orders from the second into the first and then remove the second customer. Here is a case where pessimistic concurrency is probably warranted but if all the updates were checking first, then it could be handled if users expected this type of thing to happen. I am really starting to have a better appreciation for the programmers of database applications I've used over the years. And I believe I still have so much more to learn than I originally anticipated considering my "simple" little example above is only handling one table and I haven't even broached the concepts of transactions hitting multiple tables. I now feel like I am starting to eat an elephant! Life was so much less complicated when I focussed on pulling data from databases instead of putting it in.

    Thanks for the responses leinad31 and jmcilhinney.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Trying to learn some database techniques - deleting records

    Pessimistic/optimistic locking is not the only way to control concurrency. You have to do some research and widen your options.

    You can control concurrency by table design. Minimize UPDATE, DELETE queries, e.g. table designed to accept multiple info regarding transaction, or a history, then obsolete ones are purged at a set schedule. A good sample is running balance, rather than repeatedly updating inventory of single product record you maintain inventory history (ins/outs) so DML beceomes predominantly SELECT+INSERT rather than SELECT+UPDATE, then at month end starting balances for succeeding month are created to minimize records retrieved by SELECT running balance.

    Constraint design (PK/FK) as well as how the steps in transaction are order also make a difference http://www.vbforums.com/showpost.php...51&postcount=4

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    Re: Trying to learn some database techniques - deleting records

    For some of the work I am doing right now, I don't have control over the database itself. So I cannot use Fields to mark records as deleted. I had thought of that for work I was going to start myself, where I would have control over the database. Your other post regarding multiple keys is interesting as well and I will think about this when working on my own db structure.

    Thanks for the info.

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