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
Comment Utility
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
Comment Utility
For more sophisticated database with c# read:
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 25 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now