How To Use Sqlite On Windows
Hither I will explain how to employ and connect to Sqlite in a Windows application.
Why we use Sqlite in C#
We utilise Sqlite because all ther other databases that we use mostly require a server and Sqlite is a database that we can embed within our system. Then we don't require any server for a Sqlite database. Most of mobile and small databases don't crave a server and if we want to make a standalone application for i computer that doesn't depend on another server and then Sqlite is perfect for united states of america.
For more information virtually Sqlite you can go to this link: Sqlite.
How to connect Sqlite with C#. Basically C# doesn't support Sqlite itself then we need a third-political party DLL to connect to a Sqlite database.
There is a link, from this link you can get a DLL file link and at this link y'all go to the download department and download the latest version. The download start link is a Cipher file so you lot extract this file to your application in the debug folder.
Step 1: Select a new project
Open your Visual Studio and select new project and in Visual C# select "Windows Forms Awarding" and provide the name as Sqlite and click on OK.
Footstep 2: Extract DLL file
Right-click on your application and select "Open up folder in your window application" and then go to:
BIN -> Debug and extract your application hither.
Afterwards this over again right-click on your application and select "Add reference" and become to:
Browser -> BIN -> Debug and select DLL file (Sqlite Net.dll).
Stride 3: Form
Drag and drib a button and provide the name equally "connect Sqlite".
Step four: Lawmaking
Double-click on the button and write the following code.
Add a namespace
using Finisar.SQLite;
You employ sample code from this link:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Cartoon;
using System.Text;
using Organisation.Windows.Forms;
using System.Data.SqlClient;
using System.Text;
using Organization.IO;
namespace First_Csharp_app
{
public fractional class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Choose_Btn_click(object sender, EventArgs e)
{
// [snip] - Equally C# is purely object-oriented the post-obit lines must be put into a grade:
// Nosotros apply these three SQLite objects:
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connexion:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=Truthful;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE test (id integer master key, text varchar(100));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new tabular array:
sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (i, 'Test Text 1');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our tabular array ?
// First lets build a SQL-Query over again:
sqlite_cmd.CommandText = "SELECT * FROM test";
// At present the SQLiteCommand object tin give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the upshot lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
//System.Panel.WriteLine( sqlite_datareader["text"] );
string myreader = sqlite_datareader.GetString(0);
messageBox.Show(myreader);
}
// We are ready, now lets cleanup and close our connectedness:
sqlite_conn.Close();
}
}
}
Footstep 5: Output
Run your awarding and click on the button.
Step 6: Database
Now if you over again correct-click on your awarding and select "Open binder in your window application" and then go to:
BIN -> Debug then here you lot volition run across the database so for Sqlite at that place is no need for a server.
Step vii: Database Table
If you want to see this database then there is a unproblematic mode to encounter the table and everything in the database then go to Mozila and click at the above and go to "Add-ons".
And at the search blazon "Sqlite" hither you will see "Sqlite Manager 0.7.7 " then install this.
After installation go again to the top at Mozila and there click on "web developer -> Sqlite Director".
Hither you get to the button (open) and browse your database then you will meet the table in your database.
Source: https://www.c-sharpcorner.com/UploadFile/5d065a/how-to-use-and-connect-sqlite-in-a-window-application/
Posted by: terrytherend.blogspot.com
0 Response to "How To Use Sqlite On Windows"
Post a Comment