in

System.Data.SQLite

An open-source, enhanced version of the SQLite database engine for Windows

VB.net Visual Basic (Read Insert Update Delete Other)

Last post 01-14-2008 2:39 PM by Templarian. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 01-13-2008 2:04 PM

    VB.net Visual Basic (Read Insert Update Delete Other)

    This should answer any questions you have about the basics of VB.net and SQLite.

    Insert/Update/Delete Values: 

    Dim f As New OpenFileDialog
    f.ShowDialog()
    Dim SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    SQLcommand.CommandText = "INSERT INTO Item (type) VALUES ('something')"
    SQLcommand.ExecuteNonQuery()
    SQLcommand.Dispose()
    SQLconnect.Close()

    Read Values:

    Dim f As New OpenFileDialog
    f.ShowDialog()
    Dim SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    SQLcommand.CommandText = "SELECT * FROM Item"
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
    While SQLreader.Read()
        TextBox1.Text &= String.Format("ID = {0}, MYVALUE = {1}", SQLreader(0), SQLreader(1)) & vbCrLf
    End While
    SQLcommand.Dispose()
    SQLconnect.Close()

    Convert BlobToImage (with how to use it):

    Private Function BlobToImage(ByVal blob)
        Dim mStream As New System.IO.MemoryStream
        Dim pData() As Byte = DirectCast(blob, Byte())
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length))
        Dim bm As Bitmap = New Bitmap(mStream, False)
        mStream.Dispose()
        Return bm
    End Function

    Dim f As New OpenFileDialog
    f.ShowDialog()
    Dim SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    SQLcommand.CommandText = "SELECT * FROM item WHERE id = 1"
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
    While SQLreader.Read()
        'Where 1 is the Second Column in the Table
        PictureBox1.Image = BlobToImage(SQLreader(1))
    End While
    SQLcommand.Dispose()
    SQLconnect.Close()

    Convert ImageToBlog (with how to use it):

    Public Function ImageToBlob(ByVal filePath As String) As Byte()
        Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
        Dim br As BinaryReader = New BinaryReader(fs)
        Dim bm() As Byte = br.ReadBytes(fs.Length)
        br.Close()
        fs.Close()
        Return bm
    End Function

    Dim f As New OpenFileDialog
    f.ShowDialog()
    Dim SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    SQLcommand.CommandText = "INSERT INTO TileSet_" & TextBox1.Text & " (id,image) VALUES('1',@image)"
    Dim photo() As Byte = ImageToBlob("C:\img.png")
    Dim SQLparm As New SQLiteParameter("@image", photo)
    SQLparm.DbType = DbType.Binary
    SQLparm.Value = photo
    SQLcommand.Parameters.Add(sqlpar)
    SQLcommand.ExecuteNonQuery()
    SQLcommand.Dispose()
    SQLconnect.Close()

    Put Names of Tables in a LIST:

    Dim f As New OpenFileDialog
    f.ShowDialog()
    Dim SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & f.FileName & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    Dim SchemaTable = SQLconnect.GetSchema(SQLiteMetaDataCollectionNames.Tables)
    For int As Integer = 0 To SchemaTable.Rows.Count - 1
        If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "table" Then
            ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
        End If
    Next
    SQLcommand.Dispose()
    SQLconnect.Close()

    I'm gonna keep editing this.

  • 01-14-2008 2:29 PM In reply to

    Re: VB.net Visual Basic (Get Values From DB)

    Hi, i am developing VB.Net smart device application using .Net 2003 , i can run smiple form application in amulator successfuly, now i want to open sqlite file and read its data to show in grid, when i include code for connection open, there comes expection error message. i m including reffering to SQLite.Net.dll and my sytex is correct, i am wondering, how to open connection for sqlite file. can you please guide me little what i am missing.

  • 01-14-2008 2:39 PM In reply to

    Re: VB.net Visual Basic (Get Values From DB)

    The second code section should answer your question. each time it goes through the loop is a row. In the while loop the variables are spit out, in the order you put them in "SELECT var1, var2, ect..." I used * so all the columns are returned for each row.

    //edit, Look Below Notice how I define an absolute path fromt he storage card (my pocket pc is dead so i forget how the folders are set up but basically start at the root of the device or storage card (just noticed an error in my reader the variable file doesn't even do anything i deleted it off the top version.

    //Removed the line from the top two Dim file As String = f.FileName (the example is basically what you should be doing, but i forget the names for the folders on the pocket pc).

    im SQLconnect As New SQLite.SQLiteConnection()
    Dim SQLcommand As SQLiteCommand
    SQLconnect.ConnectionString = "Data Source=" & "Storage Card\something" & ";"
    SQLconnect.Open()
    SQLcommand = SQLconnect.CreateCommand
    SQLcommand.CommandText = "SELECT * FROM Item"
    Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
    While SQLreader.Read()
        TextBox1.Text &= String.Format("ID = {0}, MYVALUE = {1}", SQLreader(0), SQLreader(1)) & vbCrLf
    End While
    SQLconnect.Close()

Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems