为什么在引用中没有显示System.Data.Sqlite?

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

我使用以下命令安装了SQLite NuGet包:

PM> NuGet\Install-Package SQLite -Version 3.13.0

Tentative de collecte d'informations de dépendance pour le package 'SQLite.3.13.0' du projet 'FindDoublons', ciblant '.NETFramework,Version=v4.7.2'
La collecte des informations de dépendance a pris 2 ms
Tentative de résolution de dépendances pour le package 'SQLite.3.13.0' avec DependencyBehavior 'Lowest'
La résolution des informations de dépendance a pris 0 ms
Actions en cours de résolution pour installer le package 'SQLite.3.13.0'
Actions résolues pour installer le package 'SQLite.3.13.0'
Récupération du package 'SQLite 3.13.0' dans 'nuget.org'.
Ajout du package 'SQLite.3.13.0' au dossier 'C:\Users\berna\source\repos\FindDoublons\packages'
Package 'SQLite.3.13.0' ajouté au dossier 'C:\Users\berna\source\repos\FindDoublons\packages'
Package 'SQLite.3.13.0' ajouté à 'packages.config'
Installation réussie de « SQLite 3.13.0 » sur FindDoublons
L'exécution des actions de nuget a pris 665 ms
Temps écoulé : 00:00:00.9417323
PM>

我在Windows 11上使用Visual Studio 2019的VB.Net程序中使用它。但当我在Project Properties中显示References时,我看不到System.Data.Sqlite.Net程序集:

References面板中没有显示System.Data.Sqlite正常吗?我可以做些什么来解锁我的阻塞情况?

dwbf0jvd

dwbf0jvd1#

改为下载/安装NuGet包System.Data.SQLite

添加导入

  • Imports System.Data.SQLite
    打开解决方案资源管理器
  • 在VS菜单中,点击[查看]
  • 选择解决方案浏览器
    查看参考资料
  • 在解决方案资源管理器中,展开“引用”

以下是一些示例代码

Imports System.Data.SQLite
Imports System.IO

Module HelperSQLite
    Private _connectionString As String = String.Empty
    Private _databaseFilename As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.sqlite")

    Sub New()
        'set value
        _connectionString = $"Data Source={_databaseFilename};Version=3;"
    End Sub

    Public Property DatabaseFilename As String
        Get
            Return _databaseFilename
        End Get
        Set(value As String)
            _databaseFilename = value
            _connectionString = $"Data Source={value};Version=3;"
        End Set
    End Property

    Public Sub CreateDatabaseAndTable()
        If Not File.Exists(DatabaseFilename) Then
            SQLiteConnection.CreateFile(DatabaseFilename)

            Dim sqlText As String = "CREATE TABLE Employee(
                                    ID INTEGER Constraint PK_Employee_ID PRIMARY KEY AUTOINCREMENT,
                                    FirstName TEXT,
                                    LastName TEXT
                                    );"

            Debug.WriteLine($"_connectionString (CreateDatabaseAndTable): '{_connectionString}'")

            Using con As SQLiteConnection = New SQLiteConnection(_connectionString)
                'open
                con.Open()
                Using cmd As SQLiteCommand = New SQLiteCommand(sqlText, con)
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End If
    End Sub

    Public Function GetData() As DataTable
        Dim dt As DataTable = New DataTable()
        Dim sqlText As String = "SELECT ID, FirstName, LastName FROM Employee;"

        Debug.WriteLine($"_connectionString (GetData): '{_connectionString}'")

        Using con As SQLiteConnection = New SQLiteConnection(_connectionString)
            'open
            con.Open()

            Using da As SQLiteDataAdapter = New SQLiteDataAdapter(sqlText, con)
                'get data
                da.Fill(dt)
                Return dt
            End Using
        End Using
    End Function

    Public Function GetDataAsString() As String
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()

        Dim dt As DataTable = GetData()

        For Each row As DataRow In dt.Rows
            sb.AppendLine($"ID: {row.Field(Of Int64)("ID")} FirstName: '{row.Field(Of String)("FirstName")}' LastName: '{row.Field(Of String)("LastName")}'")
        Next

        Return sb.ToString()
    End Function

    Public Function InsertData(firstName As String, lastName As String) As Integer
        Dim sqlText As String = "INSERT INTO Employee(FirstName, LastName) VALUES(@firstName, @lastName);"

        Debug.WriteLine($"_connectionString (InsertData): '{_connectionString}'")

        Using con As SQLiteConnection = New SQLiteConnection(_connectionString)
            'open
            con.Open()

            Using cmd As SQLiteCommand = New SQLiteCommand(sqlText, con)
                If Not String.IsNullOrEmpty(firstName) Then
                    cmd.Parameters.Add("@firstName", DbType.String).Value = firstName
                Else
                    cmd.Parameters.Add("@firstName", DbType.String).Value = DBNull.Value
                End If

                If Not String.IsNullOrEmpty(lastName) Then
                    cmd.Parameters.Add("@lastName", DbType.String).Value = lastName
                Else
                    cmd.Parameters.Add("@lastName", DbType.String).Value = DBNull.Value
                End If

                'execute
                Return cmd.ExecuteNonQuery()
            End Using
        End Using
    End Function
End Module

用法-创建数据库和表:

CreateDatabaseAndTable()

使用情况-插入数据:

InsertData("Bob", "Smith")
InsertData("John", "Doe")

使用情况-选择:

Dim results As String = GetDataAsString()
Debug.WriteLine(results)

资源

相关问题