Search code examples
c#winformsvisual-studio-2019local-database

do i have to install database on client's system?


I am creating a C# Windows Forms application in Visual Studio 2019 for Car Dealing.

My question are:

  1. Should I use local database as only single client on single PC is going to use the app?
  2. Will I have to install database on client's PC?

If there is another way, how can I do it?


Solution

  • As NeutralHandle mentioned, you can save the data into SQLite.

    To use it in Winforms, you can follow the steps.

    1.Install System.Data.SQLite from Nuget

    2.Configure the connection string in App.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    ...
      <connectionStrings>
         <add name="SQLiteDbContext" connectionString="Data Source=MyDatabase.sqlite" providerName="System.Data.SQLite.EF6" />
      </connectionStrings>
    </configuration>
    

    3.Then refer to the code demo.

    SQLiteConnection.CreateFile("MyDatabase.sqlite");
    
    SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite");
    m_dbConnection.Open();
    
    string sql = "create table highscores (name varchar(20), score int)";
    
    SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();
    
    sql = "insert into highscores (name, score) values ('Me', 9001)";
    
    command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();// read
    SQLiteCommand sqlCom = new SQLiteCommand("Select * From highscores", m_dbConnection);
    
    SQLiteDataReader sqlDataReader = sqlCom.ExecuteReader();
    
    int i = 1;
    while (sqlDataReader.Read())
    {
        listBox1.Items.Add(i);
        listBox1.Items.Add(sqlDataReader.GetValue(0));
        listBox1.Items.Add(sqlDataReader.GetValue(1));
        i++;
    }
    
    m_dbConnection.Close();