How to use multiple databases with SQLite and C# in the same application

Edouard Durand 1 Reputation point
2021-12-05T15:08:14.77+00:00

Hello,

I am using C# .NET WinForms and SQLite to develop an application that manages databases (read and write).
My application allows me to load a .db database and read that database.
If I open this application twice, so I have the HMI twice on my screen and I can therefore load a different database in each HMI, but the problem is that the two HMIs will always use the last one. database loaded, whereas I would have liked each HMI to use its own database.

I would like to know how the two apps can use different database from each other?

For example, if I open the same application A twice, I would like that:
application A (1st launch) uses the DB_A database
application A (2nd launch) uses the DB_B database

How to do this in C # with SQLite?

For example, to connect to a database I am using the following code:

SQLiteConnection connection = new SQLiteConnection ("Data Source = C:\\DATABASE\data1.db; Version = 3; New = False; Compress = True;");

Thank you.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,800 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,943 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,266 Reputation points Microsoft Vendor
    2021-12-07T02:26:59.243+00:00

    @Edouard Durand , as others said, we could store the time value in Application.settings so that we can check if the app is opened at the first time or at the second time.

    First, we could set the following settings in your app.

    155447-image.png

    Second, here is a code example you could refer to.

    private void Form1_Load(object sender, EventArgs e)  
            {  
                int time = Properties.Settings.Default.Time;  
                if(time==1)  
                {  
                    SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=Database1.sqlite");  
                    m_dbConnection.Open();  
                    SQLiteCommand sqlCom = new SQLiteCommand("Select * From highscores", m_dbConnection);  
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlCom);  
                    DataSet set = new DataSet();  
                    adapter.Fill(set);  
                    dataGridView1.DataSource=set.Tables[0];  
                    m_dbConnection.Close();  
                    Properties.Settings.Default.Time=time+1;  
                    Properties.Settings.Default.Save();  
                }  
                else if(time==2)  
                {  
                    SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=Database2.sqlite");  
                    m_dbConnection.Open();  
                    SQLiteCommand sqlCom = new SQLiteCommand("Select * From Stuinfo", m_dbConnection);  
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlCom);  
                    DataSet set = new DataSet();  
                    adapter.Fill(set);  
                    dataGridView1.DataSource=set.Tables[0];  
                    m_dbConnection.Close();  
                    Properties.Settings.Default.Time=time+1;  
                    Properties.Settings.Default.Save();  
                }  
                else  
                {  
                    MessageBox.Show("You have opened the app 2 times, so you can not open it again");  
                    Properties.Settings.Default.Time=1;  
                    Properties.Settings.Default.Save();  
                }  
            }  
    

    Finally, you could get the following result:

    155485-12.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments