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
  • 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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now