如何在Visual Studio 2022中将SQLite添加到数据源向导?

ds97pgxw  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(556)

是否可以在Visual Studio 2022的数据源向导中添加SQLite?
当我运行数据源配置向导时:

  • 我选择来源类型:数据库
  • 然后选择源模型:DataSet
  • 然后选择Data Connection:New Connection,这将启动Choose Data Source窗体,该窗体列出了可供选择的数据源:

Microsoft Access数据库文件
Microsoft ODBC数据源
Microsoft SQL Server
Microsoft SQL Server数据库文件
<其他>
如何将SQLite作为数据源添加到此列表?
已尝试在网络上进行广泛搜索。

h4cxqtbf

h4cxqtbf1#

因此,老实说,我还没有找到将其添加到数据源的方法,但我找到的最简单的方法是:

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();
    }
  }
}

这对我很管用,我希望对你也管用!
每次使用它时,您只需记住这一点
您可以通过创建一个SQLite类来使其更灵活

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;
    }
  }
}

相关问题