Joseph Reyes
Joseph Reyes

Reputation: 79

C# how to connect WPF and SQLITE database easiest way

New to wpf and sqlite. I just want an internal database for my project. I want this project to be able to used by others so I cannot use mysql as a database. So I search for internal databases and decided to try sqlite. Im finding tutorial but they are really confusing. I'm familiar in database I know the queries but now I just need to know what are the necessary things to setup to start the connection between WPF and sqlite. Below is my code(I create a different class for my sqlite connection):

 sqlite class

 using System.Data.SQLite;
 namespace StartMyApps
 {
     class StartMyAppDb_sqlite
     {
       public SQLiteConnection myConn;
       public SQLiteCommand myComm;
       public SQLiteDataReader myReader;

       public void openConnection(string query)
       {
         myConn = new SQLiteConnection("Data Source=StartMyApplication.sqlite;Version=3;");
         myConn.Open();
         myComm = new SQLiteCommand(query, myConn);
         myComm.ExecuteNonQuery();
         myReader = myComm.ExecuteReader();
       }      
    }
 }

 Main class (has a button to trigger the connection and will pass a query)

   private void hide_btn_Click(object sender, RoutedEventArgs e)
    {
        sqliteDB.openConnection("select *from application where app_id='1' and app_name='chrome.exe';");

        bool hasAccount = false;
        while (sqliteDB.myReader.Read())
        {
            hasAccount = true;
        }

        if (hasAccount == false)
        {
            MessageBox.Show("Logged in");
        }

        else if (hasAccount == true)
        {
            MessageBox.Show("Username invalid");
        }
    }

With this code I got an error says

"An unhandled exception of type 'System.DllNotFoundException' occurred in System.Data.SQLite.dll Additional information: Unable to load DLL 'SQLite.Interop.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"

Please help. Any help will be much appreciated THANKS!

Upvotes: 1

Views: 2159

Answers (2)

James Nelson
James Nelson

Reputation: 781

I think this is what your looking for: Unable to load DLL 'SQLite.Interop.dll'

Assuming you have:

  • SQLite installed (correct version for windows)

  • Appropriate references

Upvotes: 1

Dev Kumar
Dev Kumar

Reputation: 76

Follow below steps:

  • Add 2 folders in the project and name it x64 and x86.
  • Add SQLite.interop.dll for x64 and x86 respectively(google for the same)
  • In SQLite.interop.dll properties, set "Build Action" --> Content and "Copy to Output directory" -> Copy Always/Copy if newer.

Upvotes: 0

Related Questions