Last active
March 3, 2017 02:31
-
-
Save tadeubdev/da09e62e1a04e0a48f0156c9b75cf764 to your computer and use it in GitHub Desktop.
How to connect to SQLite Database file in .vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Imports System.Data.SQLite | |
| Public Class Database | |
| Private Cmd As SQLiteCommand | |
| Private Conn As SQLiteConnection | |
| Private Reader As SQLiteDataReader | |
| Private ResponseData As Dictionary(Of String, String) = Nothing | |
| Private ExMessage As String | |
| Public Function Find(FileDB As String, SQLQuery As String) As Dictionary(Of String, String) | |
| Try | |
| ResponseData = New Dictionary(Of String, String) | |
| Connect(FileDB) | |
| Cmd.CommandText = SQLQuery | |
| Reader = Cmd.ExecuteReader() | |
| While Reader.Read() | |
| If Reader.FieldCount.Equals(2) Then | |
| If Reader(1) Is Nothing Or IsDBNull(Reader(1)) Then | |
| Continue While | |
| End If | |
| ResponseData.Add(Reader.GetString(0), Reader.GetString(1)) | |
| Else | |
| For Index As Integer = 0 To Reader.FieldCount - 1 | |
| ResponseData.Add(Reader.GetName(Index), Reader(Index).ToString) | |
| Next | |
| Exit While | |
| End If | |
| End While | |
| Return ResponseData | |
| Catch ex As Exception | |
| ExMessage = String.Format("Erro ao buscar informações no banco:{0}{1}{0}{2}{0}{3}{0}{0}{4}", vbCrLf, SQLQuery, FileDB, ex.Message, ex.StackTrace) | |
| Throw New Exception(ExMessage) | |
| End Try | |
| End Function | |
| Public Sub Update(FileDB As String, SQLQuery As String, Rows As Dictionary(Of String, String)) | |
| Connect(FileDB) | |
| Cmd.CommandText = SQLQuery | |
| Try | |
| For Each DataItem In Rows | |
| Cmd.Parameters.AddWithValue(DataItem.Key, DataItem.Value) | |
| Next | |
| Cmd.ExecuteNonQuery() | |
| Catch ex As Exception | |
| ExMessage = String.Format("Erro ao atualizar informações no banco:{0}{1]{0}{2}{0}{3}", vbCrLf, SQLQuery, FileDB, ex.Message) | |
| Throw New Exception(ExMessage) | |
| End Try | |
| End Sub | |
| Private Sub Connect(FileDB As String) | |
| Try | |
| Conn = New SQLiteConnection(String.Format("Data Source={0};", FileDB)) | |
| Conn.Open() | |
| Cmd = New SQLiteCommand(Conn) | |
| Catch ex As Exception | |
| ExMessage = String.Format("Erro ao conectar ao banco:{0}{1]{0}{2}", vbCrLf, FileDB, ex.Message) | |
| Throw New Exception(ExMessage) | |
| End Try | |
| End Sub | |
| End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment