Blue_Dreams
Blue_Dreams

Reputation: 49

How to add SQLite to Data Source Wizard in Visual Studio 2022?

Is it possible to add SQLite in the Data Source Wizard in Visual Studio 2022?

When I run the Data Source Configuration Wizard:

Microsoft Access Database File
Microsoft ODBC Data Source
Microsoft SQL Server
Microsoft SQL Server Database File
<other>

How do I add SQLite to this list as a Data Source?

Tried searching extensively through the web.

Upvotes: 4

Views: 7989

Answers (1)

CamS
CamS

Reputation: 26

So to be honest I have not found a way to add it with to the Data Source but the easiest way to do it that I found was:

using System.Data.SQLite;

namespace younamespace {
  class YourClass {
    public void Foo() {
      string db_name = "databasename.ext"; //ext is either .db or .sqlite3
      string connectionString = $"{Path.Combine(Environment.CurrentDirectory, db_name)}";
      SQLiteConnection myDatabase = new SQLiteConnection(connectionString);
      myDatabase.Open();
      //VS will give an error to the '.Open()' method and a quick fix to solve the issue
      SQLiteCommand sqlCmd = myDatabase.CreateCommand();
      sqlCmd.CommandText = @"insert sql command here";
      sqlCmd.CommandType = CommandType.Text;
    
      SQLiteDataReader reader = sqlCmd.ExecuteReader();
      while(reader.Read()) {
        datatype varName = reader["tableHeading"];
        //...etc
      }
      myDatabase.Close();
    }
  }
}

This worked for me I hope it works for you!

You will just need to remember this every time you use it

You can make it more flexable by Creating an SQLite class

using System.Data.SQLite;
namespace yournamespace {
  class SQLiteClass {
    private SQLiteConnection connection; 
    public SQLiteClass() {
      string dbName = "name.ext";
      string connectionString = $"{Path.Combine(Environment.CurrentDirectory, dbName)}";
      this.connection = new SQLiteConnection(connectionString);
     }

     public returnType runCmd(string sCmd) {
       this.connection.Open();
       SQLiteCommand sqlCmd = this.connection.CreateCommand();
       sqlCmd.CommandText = sCmd;
       sqlCmd.CommandType = CommandType.Text;
    
      SQLiteDataReader reader = sqlCmd.ExecuteReader();
      returnType result = new returnType();
      while(reader.Read()) {
        //...etc
      }
      this.connection.Close();
      return result;
    }
  }
}

Upvotes: 0

Related Questions