2

I am new to C# and am trying to get a good start.

I am able to make a simple form program with some buttons and stuff. But here is my question:

How - or what is best practice to store user inputs in a program?

Let's say that I create a simple program where user can input a text line via textbox and "add" it to a list via button. When the user closes and open the program again it should have remembered every line that he has entered. (Like a journal).

How is this best accomplished? I have searched google but it hasn't helped me at all. Am I supposed to use a database? Save and read to a text file? (ini / xml?)

And does this simple program need to be installed? Or can it work as an executable exe file - and still save/read on the users computer?

Best regards (sorry for my english).

Kiran
  • 20,167
  • 11
  • 67
  • 99
user1281991
  • 763
  • 1
  • 12
  • 34

4 Answers4

2

The simplest option by far is to use an XML file. I wouldn't try ini file unless you really need something specific.

A text file is a good option if you know you will only ever need to store data from a single text input area.

XML will allow you to store and retrieve data directly from your objects through serialization.

No - a simple executable does not need an installer - but if you dont create an installer then you cannot add things like shortcuts etc easily.

dice
  • 2,820
  • 1
  • 23
  • 34
  • 1
    Awesome! Thank you very much. That's what I needed. Now Google can help me ;) BTW, my first time here on StackOverflow - not my last :) – user1281991 Mar 20 '12 at 21:33
1

The easiest and most scallable way for these kind of, is use of some embeded database. I, personally, would go for Sqlite. In download section you can find a binaries for .NET too.

There are plenty other possible options, but this is just what I would choice having in my hands an information provided from the question.

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    Thank you very much. When you write embeded database, does that mean, that when my program is build, and a user runs it, the database runs within my program? So the user doesn't need to install a separate database on his system? – user1281991 Mar 20 '12 at 21:35
  • @user1281991: it's doesn't actually **always** mean that this databse is `in-process`, but, at least, in case of `Sqlite`, you do not need nothing else then [.NET Sqlite driver](http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki) and no services or some special setup. – Tigran Mar 20 '12 at 21:50
0
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        class ConnectionManager
        {
            public static SqlConnection getConnection()
            {
                try {
                    String conn = ConfigurationManager.ConnectionStrings["Test"].ToString();
                    SqlConnection sc = new SqlConnection(conn);
                    return sc;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return null;
                }
            }
        }
    }


 private DataTable getData()
        {
            try
            {
                SqlConnection conn = ConnectionManager.getConnection();
                conn.Open();
                String sql = "SELECT * FROM Appliance_Manufacturers";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                return dt;
            }catch(Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }

        private bool addManufacture(String name)
        {

            try
            {
                SqlConnection con = ConnectionManager.getConnection();
                con.Open();
                string query = "INSERT INTO Appliance_Manufacturers (Manufacturer) VALUES('" + name + "')";
                SqlCommand cmd = new SqlCommand(query, con);
                int status = cmd.ExecuteNonQuery();
                con.Close();
                return (status == 1);
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }
    }

intvaxis

0

If you are beginner, you have got the right idea.
You can store data in a text/xml file or in database.
You could go for XML as @dice pointed out but beginning programming to XML can get daunting.
I would suggest going for text file storage and get a feel how things work.

Here's a great article to start off with IO coding. Later on change this

string[] lines = {"First line", "Second line", "Third line"};

to point to the user input.

Null Head
  • 2,877
  • 13
  • 61
  • 83