简体   繁体   中英

SQLite in vb 2010

I have, for over a month, tried in vain to resolve this. I am using SQLite and trying to write a simple check to see if i can connect to the database. I found some code here that was supposed to do just that.

    Public Function ConnExist(strDB) As Boolean
    Dim SQLconnect As New SQLite.SQLiteConnection()

    Try
        Using Query As New SQLiteCommand()
            SQLconnect.ConnectionString = "DataSource=" & strDB & ";Version=3;New=False;Compress=True;"
            SQLconnect.Open()

            With Query
                .Connection = SQLconnect
                .CommandText = "SELECT * FROM tbltest"
            End With

            Query.ExecuteNonQuery()
            SQLconnect.Close()
        End Using
        'SQLconnect.ConnectionString = "Data Source=" & strDB & ";Version=3;New=False"
        'SQLconnect.Open()
        'SQLconnect.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        'Return False
    End Try

    Return True
End Function

I know the database is at the location specified. On the SQLconnect.Open() part it errors out and tells me datasource cannot be empty. I opened the database using DB Browser and it is not corrupt. What am I doing wrong?

This is how I build my connection string for SQLite.

Dim constr As String = "Data Source=""" & FullFilePath & """;Pooling=true;FailIfMissing=false"

Think you are just missing the Double Quotes around the path.

    Public Function ConnExist(strDB) As Boolean
    Dim cs As String
    Dim cnn As New SQLiteConnection
    Dim cmd As New SQLiteCommand

    cs = "Data Source=" & strDB & ";Version=3;New=False;"

    If (IO.File.Exists(strDB)) Then
        cnn = New SQLiteConnection(cs)
        Try
            cnn.Open()
            cmd = cnn.CreateCommand()
            cmd.CommandText = "SELECT * FROM tblSettings"
            cmd.ExecuteNonQuery()
            cnn.Close()
            cmd.Dispose()
            cnn.Dispose()
        Catch ex As Exception
            Return False
            Exit Function
        End Try
    Else
        Return False
        Exit Function
    End If

    Return True

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM