C# - How To Connect To SQLite

Submitted by: 
Language: 
Visitors have accessed this post 328 times.

In this tutorial we will try to connect the application to the SQLite Database using C#. C# syntax is highly expressive, yet it is also simple and easy to learn. C# is faster than dynamically typed languages because things are more clearly defined. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. SQLite is very carefully tested prior to every release and relevant to use in some way. Most of the SQLite source code is devoted purely to testing and verification. So let's do the coding...

Getting Started

First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want.

Here's the link for the Visual Studio https://www.visualstudio.com/.

Here's the link for the SQLite Browser http://sqlitebrowser.org/.

Application Design

We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.

  1. namespace SQlite
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows Form Designer generated code
  24.  
  25. /// <summary>
  26. /// Required method for Designer support - do not modify
  27. /// the contents of this method with the code editor.
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.dtv_data = new System.Windows.Forms.DataGridView();
  32. this.btn_connect = new System.Windows.Forms.Button();
  33. ((System.ComponentModel.ISupportInitialize)(this.dtv_data)).BeginInit();
  34. this.SuspendLayout();
  35. //
  36. // dtv_data
  37. //
  38. this.dtv_data.AllowUserToAddRows = false;
  39. this.dtv_data.AllowUserToDeleteRows = false;
  40. this.dtv_data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  41. this.dtv_data.Location = new System.Drawing.Point(52, 25);
  42. this.dtv_data.Name = "dtv_data";
  43. this.dtv_data.ReadOnly = true;
  44. this.dtv_data.RowTemplate.Height = 28;
  45. this.dtv_data.Size = new System.Drawing.Size(598, 339);
  46. this.dtv_data.TabIndex = 0;
  47. //
  48. // btn_connect
  49. //
  50. this.btn_connect.Font = new System.Drawing.Font("Arial", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  51. this.btn_connect.Location = new System.Drawing.Point(288, 384);
  52. this.btn_connect.Name = "btn_connect";
  53. this.btn_connect.Size = new System.Drawing.Size(151, 63);
  54. this.btn_connect.TabIndex = 1;
  55. this.btn_connect.Text = "Connect";
  56. this.btn_connect.UseVisualStyleBackColor = true;
  57. this.btn_connect.Click += new System.EventHandler(this.Connect);
  58. //
  59. // Form1
  60. //
  61. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  62. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  63. this.ClientSize = new System.Drawing.Size(710, 470);
  64. this.Controls.Add(this.btn_connect);
  65. this.Controls.Add(this.dtv_data);
  66. this.Name = "Form1";
  67. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  68. this.Text = "Connecting to SQLite";
  69. ((System.ComponentModel.ISupportInitialize)(this.dtv_data)).EndInit();
  70. this.ResumeLayout(false);
  71.  
  72. }
  73.  
  74. #endregion
  75.  
  76. private System.Windows.Forms.DataGridView dtv_data;
  77. private System.Windows.Forms.Button btn_connect;
  78. }
  79. }

or also you create the layout by dragging the proper tools to the forms.

Creating the Script

This code contains the function of the application. This code will connect the application to the database when the button is clicked. First all you need to do is to install the components of the SQLIte database, by right clicking in the Main project title in the solution explorer then selecting the Manage NuGet Packages.
tut1

Then go to the browse and search sqlite, after that install it and wait until the process is completed.
tut2

Next go to the Updates and update the needed framework to make sqlite work properly.
tut3

Lastly, we will now create the script to make things work. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.

Note: Make sure to change the path for the database that you use to avoid errors.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SQLite;
  11.  
  12. namespace SQlite
  13. {
  14. public partial class Form1 : Form
  15. {
  16. String connectString;
  17. SQLiteConnection conn;
  18. SQLiteDataAdapter adapter;
  19. DataTable dt;
  20.  
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. connectString = @"Data Source=C:\Users\Computer\Desktop\SQlite\sql.db ;Version=3";
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. private void Connect(object sender, EventArgs e) {
  32. using (conn = new SQLiteConnection(connectString)) {
  33. try
  34. {
  35. conn.Open();
  36. if (conn.State == ConnectionState.Open) {
  37. MessageBox.Show("Connected to database");
  38. dt = new DataTable();
  39. adapter = new SQLiteDataAdapter("SELECT * FROM `data`", conn);
  40. adapter.Fill(dt);
  41. dtv_data.DataSource = dt;
  42. conn.Close();
  43. }
  44. }
  45. catch (Exception ex) {
  46. MessageBox.Show(ex.Message);
  47. }
  48. }
  49. }
  50. }
  51. }

Try to run the application and see if it works.

There you go we successfully connect the application to the SQLite database using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.