Quantcast
Channel: CODENIMI » How To
Viewing all articles
Browse latest Browse all 2

Getting Started with Sqlite Database In Visual Studio C#

$
0
0

This tutorial will teach you how to create and connect to an SQLite database in C#. You will also learn how to create, insert delete and update tables and how to execute SQL queries on the database and how to read the returned results.

We’re working in C# we’ll be using the System.Data.SQLite library. This library is not a standard library (packaged with .NET for example) so we’ll need to download it. Because whole Sqlite database is contained within a single dll and for dll download you go for the below link.

http://www.sqlite.org

Put these files in the folder of your project and add an assembly reference to the .dll. Just browse to System.Data.SQLite.dll and select it.

AddAssembly24

Now add using System.Data.SQLite; to the usings and you’re done. You’ve successfully added the SQLite library to you project!

Now Connecting to a Database

Before you can use the database, you’ll need to connect to it.

  1. SQLiteConnection m_dbConnection;
  2. m_dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;");
  3. m_dbConnection.Open();

After we create the connection object, we’ll have to open it. And with every Open() there comes a Close(), so don’t forget to call that after you’re done with your connection.

Creating a table

Suppose that we need to store the name and address of users in our application, we will define the table with the SQL CREATE syntax:

  1. string sql = "create table XYZ (name varchar(20), address varchar(20))";

Now we’ll need to create an SQL command in order to execute it

Insert some records in our table

  1. string sql = "insert into XYZ (name, address) values ('Me', Mumbai)";
  2. SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
  3. command.ExecuteNonQuery();
  4. sql = "insert into XYZ (name, score) values ('Myself', Kolkata)";
  5. command = new SQLiteCommand(sql, m_dbConnection);
  6. command.ExecuteNonQuery();

Database Queries for getting data in table

  1. string sql = "select * from XYZ values by address ";
  2. SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);

 

we’ll use the ExecuteReader() method which returns an SQLiteDataReader object. We’ll use this object to read the results of the query.

  1. SQLiteDataReader reader = command.ExecuteReader();

 

Delete records from database

  1. string sql = "delete from XYZ where name = '" + XYZ.name + "'";
  2. SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
  3. command.ExecuteNonQuery();

Update Records in our Table

  1. string sql = "UPDATE  XYZ SET name = '" + xyz.name + "',";
  2. sql +=  "address = '" + xyz.address + "',";
  3. SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
  4. command.ExecuteNonQuery();

 

This concludes sqlite database using visual studio c# tutorial. Hopefully by now, you have enough information to get started using the sqlite database with visual studio c#.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images