locked
Using .dat files

    Question

  • Hi,
    Im creating a custom program for use in my lawn care business and want to use .dat files instead of a database for storage and retrieval of my data.
    I have 4 seperate forms in which I have display and information, creating customer accounts and billing, along with invoicing. With each form, data that pertains to that form is to be displayed and shown to the user via either a "Find" button I have on the form or with Form_Load() . How can I go about doing that using a .dat file as my source of data? Any known issues I need to know about in using dat files or any special encoding I need to do?
    I have a general idea of how to go about creating the file, it's just the syntax for display (retrieving data) and storage (saving to the dat file) that gets me.
    I know it may be easier for me to use a database for just that, and I know how to do that and could, but for my use and the computers it is going to be ran on, I don't want  to have to install sqlsever or anything extra with the program. Trying to keep it lean and use as little space as possible.
    I am using VB Express 2008
    Monday, July 06, 2009 12:26 AM

Answers

  • Might look into using an XML file, reading that into a DataSet/DataTable.

    Writing to and reading from the XML is fairly straightforward.

    here is an example. Create a form with a datagridview, 3 textboxes and 4 buttons. You can see the button names from the subs


    Imports System.Xml
    Public Class Form1
        Dim dsPeople As New DataSet("dsPeople")
        Dim dtPeople As New DataTable("dtPeople")
        Dim filename As String = ""
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(500, 500)
            'add table to dataset
            dtPeople = dsPeople.Tables.Add("dtPeople")
            'create table columns
            dtPeople.Columns.Add("First Name", GetType(String))
            dtPeople.Columns.Add("Last Name", GetType(String))
            dtPeople.Columns.Add("Age", GetType(Integer))
            'create filename string with username
            filename = "C:\Documents and Settings\AVILAJ\Desktop\Test Folder\" & System.Environment.UserName & ".xml"
            dsPeople.ReadXml(filename)
            DataGridView1.DataSource = dtPeople.DefaultView
            DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            DataGridView1.AllowUserToDeleteRows = True
            DataGridView1.Size = New Size(450, 450)
    
        End Sub
    
        Private Sub btnAddData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddData.Click
            'add data from textboxes to table and update xml file
            dtPeople.Rows.Add(New Object() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
            dsPeople.WriteXml(filename, XmlWriteMode.WriteSchema)
        End Sub
    
        
        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            'delete row from DataGridView and update xml file
            If DataGridView1.SelectedRows.Count > 0 Then
                DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
            Else
                MessageBox.Show("You must select a row")
            End If
            dsPeople.WriteXml(filename, XmlWriteMode.WriteSchema)
        End Sub
    
        Private Sub btnOpenXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXML.Click
            Try
                dsPeople.ReadXml(filename)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    
        Private Sub btnSaveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
            dsPeople.WriteXml(filename, XmlWriteMode.IgnoreSchema)
        End Sub
    
    End Class
    Monday, July 06, 2009 12:45 AM
  • A .dat file doesn't have any particular formatting to it and it's generally proprietary with respect to the developer or vendor.

    What you're describing though sounds like a database implementation and you could use either an Access or SQL Server Compact Edition database w/o needing to distribute extra components.

    You can certainly use XML, but it's more of a data transport mechanism than it is a database implementation.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Monday, July 06, 2009 2:31 PM

All replies

  • Might look into using an XML file, reading that into a DataSet/DataTable.

    Writing to and reading from the XML is fairly straightforward.

    here is an example. Create a form with a datagridview, 3 textboxes and 4 buttons. You can see the button names from the subs


    Imports System.Xml
    Public Class Form1
        Dim dsPeople As New DataSet("dsPeople")
        Dim dtPeople As New DataTable("dtPeople")
        Dim filename As String = ""
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Size = New Size(500, 500)
            'add table to dataset
            dtPeople = dsPeople.Tables.Add("dtPeople")
            'create table columns
            dtPeople.Columns.Add("First Name", GetType(String))
            dtPeople.Columns.Add("Last Name", GetType(String))
            dtPeople.Columns.Add("Age", GetType(Integer))
            'create filename string with username
            filename = "C:\Documents and Settings\AVILAJ\Desktop\Test Folder\" & System.Environment.UserName & ".xml"
            dsPeople.ReadXml(filename)
            DataGridView1.DataSource = dtPeople.DefaultView
            DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            DataGridView1.AllowUserToDeleteRows = True
            DataGridView1.Size = New Size(450, 450)
    
        End Sub
    
        Private Sub btnAddData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddData.Click
            'add data from textboxes to table and update xml file
            dtPeople.Rows.Add(New Object() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
            dsPeople.WriteXml(filename, XmlWriteMode.WriteSchema)
        End Sub
    
        
        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            'delete row from DataGridView and update xml file
            If DataGridView1.SelectedRows.Count > 0 Then
                DataGridView1.Rows.Remove(DataGridView1.SelectedRows(0))
            Else
                MessageBox.Show("You must select a row")
            End If
            dsPeople.WriteXml(filename, XmlWriteMode.WriteSchema)
        End Sub
    
        Private Sub btnOpenXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenXML.Click
            Try
                dsPeople.ReadXml(filename)
            Catch ex As Exception
                MessageBox.Show(Err.Description)
            End Try
        End Sub
    
        Private Sub btnSaveXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveXML.Click
            dsPeople.WriteXml(filename, XmlWriteMode.IgnoreSchema)
        End Sub
    
    End Class
    Monday, July 06, 2009 12:45 AM
  • jwavila , thanks for the reply. I will try that out and come back here and let you know how it does for me. From the looks of it, looks more promising than using a dat file. Of course, I haven't tested either as of yet, so I will refrain from making too many judgements at this point.

    Again, thanks.
    Monday, July 06, 2009 1:40 AM
  • A .dat file doesn't have any particular formatting to it and it's generally proprietary with respect to the developer or vendor.

    What you're describing though sounds like a database implementation and you could use either an Access or SQL Server Compact Edition database w/o needing to distribute extra components.

    You can certainly use XML, but it's more of a data transport mechanism than it is a database implementation.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Monday, July 06, 2009 2:31 PM
  • Thanks for the reply Paul,

    I have been testing and toiling around with different ideas on where to go with this. I messed around with Xml a little and it is limited in some respects to what I want to do. Mainly wanting to write the information that is typed in my text box(s) to a file in binary format. I am not sure if Xml can do that plus, even if it can, I don't know how to go about doing that.
    In a previous program I did, which was some-what similar to this, I used an Sql database. Easy to use and implement but I thought I would try my hand at using a .dat file this time around since I never tried that in the other program.
    Although using a database with Sql woud be easier, I must admit that my learning attempts with a .dat file and saving the information in binary format are taking precedance at this point.
    I'm some-what new to VB Express ( within the last year and half or so ) and I find myself making attempts to do things which may be harder on me. Mostly because I have this "learning" bug and can't get rid of it. I truely enjoy VB Express but I must admit, I sure would like to have the functionality of full version of Visual studio at my finger tips. Some of the things I want to do I can't do in VBE.

    Which brings me to say that a whole new "can of worms" opened up when I started working on this project. Wanting to create reports ( for invoices ) in VBE, wanting to create and use dll's (which I didn't do in my other program) and anything else that my mind can think of.
    I created a few dll's for this project and am using them, which was a step in the right direction for me. Only other serious hurdles at the moment are binary writing and report creation in VBE.

    Which brings me to ask this question:

    Does anybody know if there is a way to order a VS trial version disk?
    I looked at VS 2008 and wanted to get the trial version but the download is 3 gigs and I can't do that off this laptop. I didn't see any links for odering a trial disk on the page.
    Tuesday, July 07, 2009 4:55 PM
  • In doing some research, I have learned how to do binary mode operations in the last 20 minutes or so:

    FilePut
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
       System.EventArgs) Handles Button1.Click
          Dim MyString As String
          Dim ReadString As String
          Dim iFr As Short
          iFr = FreeFile()
          ReadString = CurDir() & "\customers.dat"
          FileOpen(iFr, 1, OpenMode.Binary)
          MyString = "new information"
          FilePut(MyString)
          FileClose(iFr)
       End Sub
    And FileGet:

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
       System.EventArgs) Handles Button1.Click
            Dim MyString As String
            Dim ReadString As String
            Dim iFr As Short
            iFr = FreeFile()
            ReadString = CurDir() & "\customers.dat"
            FileOpen(iFr, ReadString, OpenMode.Binary)
            MyString = New String(" "c, 15)
            FileGet(iFr, MyString)
            FileClose(iFr)
            MsgBox(MyString)
       End Sub
    Thanks to the people of this forum, it has helped me to broaden my knowledge.
    Tuesday, July 07, 2009 5:26 PM

  • Which brings me to ask this question:

    Does anybody know if there is a way to order a VS trial version disk?
    I looked at VS 2008 and wanted to get the trial version but the download is 3 gigs and I can't do that off this laptop. I didn't see any links for odering a trial disk on the page.

    You could try calling the Microsoft Store (1-877-696-7786) to see if they know how to get you a copy on media.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, July 07, 2009 6:00 PM
  • Thanks Paul, I will call them and see what they say.
    Tuesday, July 07, 2009 10:54 PM
  • I find myself in real agreement with Paul on many things in this thread. Number 1 is there is no payment for re-inventing the wheel other than teaching yourself and then all you've taught yourself is how to play with outdated methods.

           "ReadString = CurDir() & "\customers.dat"
            FileOpen(iFr, ReadString, OpenMode.Binary)
            MyString = New String(" "c, 15)
            FileGet(iFr, MyString)
            FileClose(iFr)"

    Those methods don't teach you anything if they were out of date before you used them, and they were.
    Renee
    Wednesday, July 08, 2009 2:17 AM
  • Although some of the things I have learned may be outdated, to some degree, I find that learning them is useful to the extent that knowledge is good.
    Thursday, July 09, 2009 6:22 PM
  • Of course RR the trouble with an analysis like that, is that it has norestrictions placed on the information. Towhit, pornography meets all of your stated criteria. but you would likely find little agreement about the vaue of pornography.
    Renee
    Thursday, July 09, 2009 7:54 PM