Z
Z
zakkav2016-12-03 13:54:04
SQL
zakkav, 2016-12-03 13:54:04

How to create a database from a C# text file?

I am doing a coursework on creating an ATM. According to the terms, passwords, names and all data are stored in a plain text file. I found such an example on one site, I can’t figure out how to extract data from here now

`public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            //Определяем подключение
            OleDbConnection StrCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Exp;Extended Properties=text");
            //Строка для выборки данных
            string  Select1 = "SELECT * FROM [Dictionary1.txt]";
            //Создание объекта Command
            OleDbCommand comand1 =new OleDbCommand(Select1, StrCon);
            //Определяем объект Adapter для взаимодействия с источником данных
            OleDbDataAdapter adapter1 =new OleDbDataAdapter(comand1);
            //Определяем объект DataSet
            DataSet AllTables =new DataSet();
            //Открываем подключение
            StrCon.Open();
            //Заполняем DataSet таблицей из источника данных
            adapter1.Fill(AllTables);
            //Заполняем обект datagridview для отображения данных на форме
            dataGridView1.DataSource = AllTables.Tables[0];
            StrCon.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                StreamWriter sw = new StreamWriter(@"D:\Exp\Dictionary1.txt", false, Encoding.Default);
                //Добавление имен столбцов
                for(int j = 0; j<dataGridView1.ColumnCount; j++)
                {
                    sw.Write(dataGridView1.Columns[j].HeaderText);
                    if( j < dataGridView1.ColumnCount-1) 
                        sw.Write(",");
                }
                sw.WriteLine();
                for (int i= 0; i<dataGridView1.RowCount;i++)
                {
                    for (int j = 0; j< dataGridView1.ColumnCount;j++)
                    {
                        sw.Write(dataGridView1.Rows[i].Cells[j].Value);
                        if (j < dataGridView1.ColumnCount-1) 
                            sw.Write(",");
                    }
                    sw.WriteLine();
                }
                sw.Flush();
                sw.Close();
            }
        catch (Exception ex)
            {
            MessageBox.Show(ex.Message);
        }
        }
    }`

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2016-12-03
@zakkav

Using the OleDbConnection connection string, a connection is established to a certain database. In your case it is a text file. Data is written to a text file in such a way that the columns are separated by tabs, commas, semicolons, or other ASCII characters. Such files are conventionally called CSV .
DataSet is a data set (so translated from English), obtained from the database using SELECT.
These are all the lines read from the text file, each divided into its own columns. In a GUI program, this data is usually projected line by line (line=record) onto some GUI element for display on the screen.
The user, working with the interface, activates the command to enter a new record, each of which must be saved to the database using INSERT.
To update the cells, say record number 2, you need to use UPDATE ... WHERE id=2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question