?
Solved

Use a file as a simple database

Posted on 2004-11-11
23
Medium Priority
?
222 Views
Last Modified: 2010-04-23
Hi folks,

I need to use a class that stored in a file a database-like data. It's very simple, just 2 or three columns with not more that 300 or 400 rows. I just need to search one value in one column and return the entire row, add rows, delete rows and modify rows.

I'm pretty sure that must be a lot of components developed that do this but I'm not lucky today finding them. Can anyone helpme with this issue?

P.D: Would be better XML format than plain text.

Thanks, SoMoS.
0
Comment
Question by:Ignacio Soler Garcia
  • 10
  • 9
  • 4
23 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12553875
Why don't you just use a datatable and serialize that to xml ?
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12553891
I don't know what is a datatable... Please, go into it.

Thanks.
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 1200 total points
ID: 12554169
this example is a bit crude but it works.

<Serializable()> Public Class myowndata
    Public mydatatable As DataTable

    Sub New()
        mydatatable = New DataTable
        mydatatable.Columns.Add("ID", GetType(System.Int32))
        mydatatable.Columns.Add("col1", GetType(System.String))
        mydatatable.Columns.Add("col2", GetType(System.String))
    End Sub

    Public Sub SaveXml(ByVal Path As String)
        Dim fStream As System.IO.FileStream
        Dim myFormat As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
        Try
            fStream = New System.IO.FileStream(Path, IO.FileMode.Create, IO.FileAccess.Write)
            myFormat.Serialize(fStream, Me)
            fStream.Close()
        Catch ex As Exception
            MsgBox("Error saving: " & ex.Message)
        Finally
            If Not (fStream Is Nothing) Then fStream.Close()
        End Try
    End Sub

    Public Function LoadXML(ByVal Path As String) As myowndata
        Dim fStream As System.IO.FileStream
        Dim myFormat As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
        Dim tmp As myowndata
        Try
            fStream = New System.IO.FileStream(Path, IO.FileMode.Open, _
                IO.FileAccess.Read)
            tmp = CType(myFormat.Deserialize(fStream), myowndata)
        Catch ex As Exception
            MsgBox("Error opening: " & ex.Message)
        Finally
            If Not (fStream Is Nothing) Then fStream.Close()
        End Try
        Return tmp

    End Function
End Class

Public Class Form20
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.StatusBar1 = New System.Windows.Forms.StatusBar
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0, 244)
        Me.StatusBar1.Name = "StatusBar1"
        Me.StatusBar1.Size = New System.Drawing.Size(292, 22)
        Me.StatusBar1.TabIndex = 0
        Me.StatusBar1.Text = "StatusBar1"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(176, 88)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(176, 136)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(176, 176)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(88, 24)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Button3"
        '
        'Form20
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.StatusBar1)
        Me.Name = "Form20"
        Me.Text = "Form20"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim dt As myowndata


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dt = New myowndata
        Dim dr As DataRow
        dr = dt.mydatatable.NewRow()
        dr.Item(0) = 1
        dr.Item(1) = "test1"
        dr.Item(2) = "test1"
        dt.mydatatable.Rows.Add(dr)

        Dim dr2 As DataRow
        dr2 = dt.mydatatable.NewRow()
        dr2.Item(0) = 2
        dr2.Item(1) = "test2"
        dr2.Item(2) = "test2"
        dt.mydatatable.Rows.Add(dr2)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        dt = New myowndata
        dt = dt.LoadXML("c:\ttt.xml")
        MsgBox(dt.mydatatable.Rows(0).Item(0))
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        dt.SaveXml("c:\ttt.xml")
    End Sub
End Class
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:planocz
ID: 12554308
HI Ron,
In Public Class myowndata what Imports are you using
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12554401
I'm not using any imports there, where do you have the problem ?
0
 
LVL 27

Expert Comment

by:planocz
ID: 12554430
System.Runtime.Serialization.Formatters.Soap.SoapFormatter

is not defined
0
 
LVL 27

Expert Comment

by:planocz
ID: 12554442
I found it .I had to add the reference Thanks.
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12554921
Ok, that's nice but I don't see the facilities to look for a value in a row, select a full row, insert one or delete one ... I'm looking more for a DLL that implements this if exist to avoid my own coding, of course.
0
 
LVL 27

Expert Comment

by:planocz
ID: 12554960
HAHA now are we getting lazy? No time to code?
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12554993
It's a really big project to finish before december 31. I don't want to introduce extra work into the project, just being focussed on the main problem. I'm pretty sure that this problem is solvent by others hundreds of times, so I will do the unsolved first :)

SoMoS
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12555086
Hi Somos,

Since this class uses a datatable, all the functionality you want is already there by default.
Just take a minute to look at the methods and properties of the datatable class

to get an entire row

dt.mydatatable.Rows(0)

to delete a row

dt.mydatatable.Rows(0).delete

to insert a row

dt.mydatatable.Rows.add("yourrow")

to look for a value in a row

dim dv as new dataview
dv = dt.mydatatable.defaultview
dv.sort = col1
dv.Find("test1")










0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12564047
Ok, no more infor at this moment, I will try the solution latter ...I hope it's not a problem to assign the points after the test. Maybe monday or twesday.

The question is not abandoned!!!

SoMoS
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12582258
Ok, the solution seems to work fine but I replaced all the XML serialization because the SOAP formater makes non human-readable XML files. Instead I wrapped the DataTable into a DataSet and used the dataset wrtiXML method. More or less Is like this.

There is no way to do a serialization of a DataTable alone? It gives me a exception because the DataTable has an interface non serializable or something like this ...
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582355
Hi Somos,

A couple of thing I don't understand in your comment

'SOAP formater makes non human-readable XML '  what do you mean by that , because what the soapformatter makes is a readable xml file, if you would use a binaryformatter than it wouldn't be readable.

'There is no way to do a serialization of a DataTable alone'

My class just serializes the datatable, nothing else ?
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12582480
Yep, I wrote on a confused way:

Non human readable really means 'non human understandable' because the SOAP formatter inserts a lot of stuff not related directly with the data inside the datatable. I need to let the users to open the xml file with notepad and modify the data manually if they want to.

On the other line 'There is no way to do a serialization of a DataTable alone' means that I haven't found the way to do a XML serialization (not SOAP) of a DataTable without being into a DataSet (alone) because the interface exception stuff I explained on the last post.

I hope that now is more clear but feel free to ask me more, of course :)

SoMoS
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582505
Yep, it is clear now ;-)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582517
Although it sounds scarry to let your users open an xml file in notepad to modify the data manually, wouldn't it be much saver to write a small keyentry form to do that ?
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12582587
Not really scarry, its a very simple file, that's how it looks like:

<Filetable>
  <Table1>
    <Start_x0020_IP>127.0.0.1</Start_x0020_IP>
    <End_x0020_IP>127.0.0.2</End_x0020_IP>
  </Table1>
  <Table1>
    <Start_x0020_IP>127.123.231.1</Start_x0020_IP>
    <End_x0020_IP>127.44.32.2</End_x0020_IP>
  </Table1>
  <Table1>
    <Start_x0020_IP>127.22.22.1</Start_x0020_IP>
    <End_x0020_IP>127.0.0.2</End_x0020_IP>
  </Table1>
  <Table1>
    <Start_x0020_IP>47.0.2.1</Start_x0020_IP>
    <End_x0020_IP>147.4.4.2</End_x0020_IP>
  </Table1>
</Filetable>

When I try to read the file I do it inside a try ... catch and assume that any exception is because a dumb ass has wroken the file structure.

Maybe I'm missing something that makes this way scary?

SoMoS
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582622
Maybe not scary for us, but if I show this XML to my users, some of them will faint, others will cry, but most will just FUBAR the file ;-)
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12582671
WUHAWUHAWUHAWUHAW. Don't worry, I use to develop industrial software for tecnical trained employees. They have the mental abilities to build a table in their memory from the data coming from the XML file (not much more) and modify it.LOL.

SoMoS
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582687
Lucky you ;-)
0
 
LVL 5

Author Comment

by:Ignacio Soler Garcia
ID: 12582873
Yes, I know, not always was like now ... I also had users that can't read their floppy because ... TA TANNN ... no floppy was inserted and the floppy and thing like this ... ahhh ... they were 2 nice years working as lawyer helpdesk ... :)
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12582908
Well, I still have users who ask where the ANY KEY is, because the computer says "press any key to continue". Lol
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question