简体   繁体   中英

Datatables in visual studio using c#

I am a beginner with the datasets and data tables. I have created a C# program using visual studio, I have a dataset with two data tables. One of the tables is for the signing up in which the user will create an account to log in through the app. The user information will be stored in the table, eg, username and password. The problem is when the user exit the app all the data in the tables will be removed it is like constructing the app again from the beginning. Is it necessary to have a connection for example to SQL server or Microsoft Access ? because I only have created a local dataset in the visual studio. The following class shows the creation of the data tables.

public sealed class DataAccess
{
    static DataAccess instance = new DataAccess();

    public DataSet ds { get; private set; }
    public DataTable dtemployees { get; private set; }
    public DataTable dttimeline { get; private set; }

    DataAccess()
    {
        this.ds = new DataSet();
        this.dtemployees = new DataTable();
        this.dttimeline = new DataTable();

        ds.Tables.Add("dtemployees");
        dtemployees.Columns.Add("ID", typeof(string));
        dtemployees.Columns.Add("name", typeof(string));
        dtemployees.Columns.Add("password", typeof(string));
        dtemployees.PrimaryKey = new DataColumn[] { dtemployees.Columns["ID"] };

        ds.Tables.Add("dttimeline");
        dttimeline.Columns.Add("ID", typeof(string));
        dttimeline.Columns.Add("name", typeof(string));
        dttimeline.Columns.Add("Date", typeof(string));
        dttimeline.Columns.Add("time", typeof(string));
    }

    public static DataAccess Instance
    {
        get
        {
            return instance;
        }
    }
}

**and the following line shows how did I add data to the tables.

dtemployees.Rows.Add(F2idTB.Text, F2usernameTB.Text, F2passwordTB.Text);

I would recommend writing out to a .txt file for data storage if you are a beginner. You will have to specify a specific format that you want to write in order to read the values back in correctly.

ie

dtEmployees.txt

1;John Wick;myPassword;

2;Mary Jane;myPassword;

You will be responsible for:

  • create a writeToTextFile() method
    • if file does not exist create one
    • if file does exist, clear it and write new data
  • create a readTextFile() method
    • for loop to read each line then split into array by ';' deliminator
    • map values to appropriate variables
    • get next line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM