Use a file as a simple database

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.
LVL 5
Ignacio Soler GarciaSolution Architech & Technical LeadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
Why don't you just use a datatable and serialize that to xml ?
0
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
I don't know what is a datatable... Please, go into it.

Thanks.
0
RonaldBiemansCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

planoczCommented:
HI Ron,
In Public Class myowndata what Imports are you using
0
RonaldBiemansCommented:
I'm not using any imports there, where do you have the problem ?
0
planoczCommented:
System.Runtime.Serialization.Formatters.Soap.SoapFormatter

is not defined
0
planoczCommented:
I found it .I had to add the reference Thanks.
0
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
planoczCommented:
HAHA now are we getting lazy? No time to code?
0
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
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
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
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
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
Yep, it is clear now ;-)
0
RonaldBiemansCommented:
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
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
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
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
Lucky you ;-)
0
Ignacio Soler GarciaSolution Architech & Technical LeadAuthor Commented:
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
RonaldBiemansCommented:
Well, I still have users who ask where the ANY KEY is, because the computer says "press any key to continue". Lol
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.