programming a private database file with c#

Posted on 2004-08-13
Last Modified: 2012-06-21
I want to make my program save some sort of data (could mostly be in table form) in special files for that program , I don't want to use a SQL server. Something else , what if I want the program to create a new file for each group and not have a prepared file already made by access or something . Can I make that file of a type dedicated to my program or it is a must that a file is a comma delimited text file. I am a new programmer and don't know where exactly to begin . Your aid and tips for me will be my move point and I can know the way through.
Thanks alot for your help.
Question by:drtoto82
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 55

Accepted Solution

Jaime Olivares earned 25 total points
ID: 11793785
A comma delitited file is a simple and effective solution for most situations where you have a small file that you can rewrite entirely every time, or when you will append but not update existing registers.
Another option is to use fixed-lenght registers and fields (not required in comma separated values), it will give you the facility to update any existing register, although your file will be greater due to "padding" blank characters for each field.
Both are the simplest solution for private database handling.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 11793828
For more sophisticated database with c# read:
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 25 total points
ID: 11794334
If the amount of data is relatively small and you are comfortable with having all of it stored in memory at once, then consider using a DataSet.  The DataSet class can handle multiple tables and relationships between those tables.  It is very easy to add/remove tables, columns, and rows.  Iterating through a tables rows can be accomplished with a simple For Each statement.  You can use Binary Serialization to save and reload your datasets to and from files.

Here is a simple example of a DataSet Table bound to a DataGrid.  When run you can add columns and enter data into the datagrid.  The data can then be saved and reloaded via the appropriately labeld buttons.

You do not need a DataGrid to work with a DataSet.  You can manipulate the data exclusively through code.  The main point of this example is that it is easy to serialize/deserialize a DataSet.

Sorry, I am a VB.Net programmer but the code should be very similar in C#.

Let me know if you want to see some examples of data being added to a table via code or would like to see an iteration through all records in a table.



Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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 Panel1 As System.Windows.Forms.Panel
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.AutoScroll = True
        Me.Panel1.Location = New System.Drawing.Point(8, 8)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(560, 248)
        Me.Panel1.TabIndex = 0
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(560, 248)
        Me.DataGrid1.TabIndex = 0
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(16, 264)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Add Column"
        Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Button2.Location = New System.Drawing.Point(104, 264)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Save Data"
        Me.Button3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.Button3.Location = New System.Drawing.Point(192, 264)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(80, 24)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Load Data"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(576, 294)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region

    Private ds As New DataSet("myData")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ds.Tables.Add(New DataTable("myTable"))
        DataGrid1.DataSource = ds.Tables("myTable")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ds.Tables("myTable").Columns.Add(New DataColumn)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SaveFileDialog1.Filter = "My Data Set Files (*.mds)|*.mds"
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            Dim fs As New FileStream(SaveFileDialog1.FileName, FileMode.Create)
            Dim formatter As New BinaryFormatter

                formatter.Serialize(fs, ds)
            Catch x As SerializationException
                MsgBox("Failed to serialize. Reason: " & x.Message)
            End Try
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        OpenFileDialog1.Filter = "My Data Set Files (*.mds)|*.mds"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim fs As New FileStream(OpenFileDialog1.FileName, FileMode.OpenOrCreate)
            Dim formatter As New BinaryFormatter

                ds = DirectCast(formatter.Deserialize(fs), DataSet)
                DataGrid1.DataSource = ds.Tables("myTable")
            Catch x As SerializationException
                MsgBox("Failed to Deserialize. Reason: " & x.Message)
            End Try
        End If
    End Sub

End Class


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Filename to be appended with DateTimeof Download 3 69
Delphi: barcode reading on android platform 1 74
AvlTree-Node Data type 4 26
Mutiple Folder view (5 Folder view) 3 53
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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