How to Connect or Show Data from SQLite in VB.Net or C#

As you know that SQLite free to use in any platform. But here we will learn How to Use SQlite Database in VB.net and we will also learn how to show SQLite Data in VB.Net or C#

But In Vb.net if we want to call data from SQLite then we have to use “System.Data.SQLite.dll” file or add as reference in VB.Net. Now when we insert this dll file, we have first import following class in your programming.

SQLIte Connect With VB.Net

VB.Net :

Imports System.Data.SQLite

C# :
using System.Data.SQLite;

After that we will proceed with following points:

  1. How to Connect SQLite in VB.Net
  2. Show Data from SQLite in VB.Net through Query
  3. Show SQlite Data in ListBox in VB.Net
  4. Show SQLite Data in Listview in VB.Net

When you add this reference to your vb project then you have to first learn what is the SQLite ConnectionString in VB.Net

First, there is following table we created in Sqlite3 Database.

Create Table in SQLite Database
SQLite Database with Tables


After that we will learn point wise described bellow.


(1). How to Connect SQLite in VB.Net :

For any database connection in any programming language require ConnectionString, so first we will write code for connection String:

VB:
Dim SQLiteConn As New SQLiteConnection

SQLiteConn.ConnectionString = "Data Source=MyPath\MysqliteFile.db; Integrated Security=true"

SQLiteConn.Open()



C#:
SQLiteConnection SQLiteConn = new SQLiteConnection();

SQLiteConn.ConnectionString = @"Data Source=MyPath\MysqliteFile.db; Integrated Security=true";

SQLiteConn.Open();


This way you can connect SQLite with VB.Net.


(2). Show Data from SQLite in VB.Net through Query :

Now, Lets try to show data from Local SQLite Database’s Tables through query in VB.Net. Suppose we want to show only single record of one field then we have to write following code:

VB:
Dim SQLitecnStr As String ="Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true"
Dim SQLiteConn As New SQLiteConnection
Dim SQLitecmd As New SQLiteCommand
Dim SQLiteReader As SQLiteDataReader

SQLiteConn.ConnectionString = SQLitecnStr
SQLiteConn.Open()

SQLitecmd.Connection = SQLiteConn
SQLitecmd.CommandText = "SELECT * from MySQLiteTable LIMIT 1;"
SQLiteReader = SQLitecmd.ExecuteReader()
	
Msgbox(SQLiteReader("MyName").ToString)

SQLiteReader.Close()
SQLiteConn.Close()


C#:
string SQLitecnStr = @"Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true";
SQLiteConnection SQLiteConn = new SQLiteConnection();
SQLiteCommand SQLitecmd = new SQLiteCommand();
SQLiteDataReader SQLiteReader;
	
SQLiteConn.ConnectionString = SQLitecnStr;
SQLiteConn.Open();
	
SQLitecmd.Connection = SQLiteConn;
SQLitecmd.CommandText = "SELECT * from MySQLiteTable LIMIT 1;";
SQLiteReader = SQLitecmd.ExecuteReader();
	
MessageBox.Show(SQLiteReader["MyName"].ToString(), "SQLite Connection in VB.Net");
	          
SQLiteReader.Close();
SQLiteConn.Close();


When we execute this code, the output will present in MessageBox.

Show MessageBox in VB.Net


(3). Show SQLite Data in ListBox in VB.Net :

Now, Suppose you want to show SQlite’s Table’s particular Fields records or show Data in ListBox in VB.Net or Listing particular Field in List then here we use while loop without using ‘LIMIT’ in Query just same as bellow:

SQLite Select Query Example


VB:
MyListbox1.items.clear()
Dim SQLitecnStr As String ="Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true"
Dim SQLiteConn As New SQLiteConnection
Dim SQLitecmd As New SQLiteCommand
Dim SQLiteReader As SQLiteDataReader
SQLiteConn.ConnectionString = SQLitecnStr
SQLiteConn.Open()

SQLitecmd.Connection = SQLiteConn
SQLitecmd.CommandText = "SELECT * from MySQLiteTable;"
SQLiteReader = SQLitecmd.ExecuteReader()
 
While SQLiteReader.Read()
    MyListbox1.Items.Add(SQLiteReader("MyMono").ToString)
End While

SQLiteReader.Close()
SQLiteConn.Close()


C#:
MyListbox1.Items.Clear();
string SQLitecnStr = @"Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true";
SQLiteConnection SQLiteConn = new SQLiteConnection();
SQLiteCommand SQLitecmd = new SQLiteCommand();
SQLiteDataReader SQLiteReader;

SQLiteConn.ConnectionString = SQLitecnStr;
SQLiteConn.Open();

SQLitecmd.Connection = SQLiteConn;
SQLitecmd.CommandText = "SELECT * from MySQLiteTable;";
SQLiteReader = SQLitecmd.ExecuteReader();

while (SQLiteReader.Read())
    {
       MyListbox1.Items.Add(SQLiteReader["MyMono"].ToString());
    }

 SQLiteReader.Close();
 SQLiteConn.Close();


After executing this code, we will find following output.

Show SQLite Data in Listbox in VB.Net


(4) Show SQLite Data in ListView in VB.Net :

Now, Lets Learn about important point that we use in every simple programming process in vb.net or any other programming Languages. Here we will talk about ListView (Showing Multiple Rows with multiple columns) with containing SQLite Records in VB.Net

Here we learn how to Use SQLite in ListView in VB.Net. So we have to follow the code like:

VB:
MyLstView.Items.Clear()
Dim SQLitecnStr As String ="Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true"
Dim SQLiteConn As New SQLiteConnection
Dim SQLitecmd As New SQLiteCommand
Dim SQLiteReader As SQLiteDataReader

SQLiteConn.ConnectionString = SQLitecnStr
SQLiteConn.Open()

SQLitecmd.Connection = SQLiteConn
SQLitecmd.CommandText = "SELECT * from MySQLiteTable;"
SQLiteReader = SQLitecmd.ExecuteReader()

While SQLiteReader.Read()
    Dim Lstv As New ListViewItem
    Lstv.Text = SQLiteReader("MySrno").ToString
    Lstv.SubItems.Add(SQLiteReader("MyName").ToString)
    Lstv.SubItems.Add(SQLiteReader("MyMono").ToString)
    MyLstView.Items.Add(Lstv)
End While

SQLiteReader.Close()
SQLiteConn.Close()


C#:
MyLstView.Items.Clear();
string SQLitecnStr = @"Data Source=C:\MYSQLiteDBForVB.db; Integrated Security=true";
SQLiteConnection SQLiteConn = new SQLiteConnection();
SQLiteCommand SQLitecmd = new SQLiteCommand();
SQLiteDataReader SQLiteReader;

SQLiteConn.ConnectionString = SQLitecnStr;
SQLiteConn.Open();

SQLitecmd.Connection = SQLiteConn;
SQLitecmd.CommandText = "SELECT * from MySQLiteTable;";
SQLiteReader = SQLitecmd.ExecuteReader();

while (SQLiteReader.Read())
     {
        ListViewItem Lsvw = new ListViewItem();
        Lsvw.Text = SQLiteReader["MySrno"].ToString();
        Lsvw.SubItems.Add(SQLiteReader["MyName"].ToString());
        Lsvw.SubItems.Add(SQLiteReader["MyMono"].ToString());
        MyLstView.Items.Add(Lsvw);
     }

SQLiteReader.Close();
SQLiteConn.Close();


So, the output will displayed like below to showing SQLite Records in ListView in VB.Net.

 
SQLite Record in Listview in VB.Net



Here are the above given details with step by step points for Showing Data of SQLite in VB.Net. Try to implement more your project with above given examples.

0 comments: