Reputation: 773
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).
Upvotes: 2
Views: 2130
Reputation: 1
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;
}
}
}
Upvotes: 0
Reputation: 2941
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.
Upvotes: 0
Reputation: 62256
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.
Upvotes: 1
Reputation: 2880
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.
Upvotes: 2