• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

C# - How To Connect To SQLite

skeemaa

Red Team Specialist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
168
Likes
154
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected

    override

    void

    Dispose(

    bool

    disposing)
  14. {
  15. if

    (

    disposing &&

    (

    components !=

    null

    )

    )
  16. {
  17. components.

    Dispose

    (

    )

    ;
  18. }
  19. base

    .

    Dispose

    (

    disposing)

    ;
  20. }

  21. #region Windows Form Designer generated code

  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private

    void

    InitializeComponent(

    )
  27. {
  28. this

    .

    dtv_data

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    DataGridView

    (

    )

    ;
  29. this

    .

    btn_connect

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Button

    (

    )

    ;
  30. (

    (

    System.ComponentModel

    .

    ISupportInitialize

    )

    (

    this

    .

    dtv_data

    )

    )

    .

    BeginInit

    (

    )

    ;
  31. this

    .

    SuspendLayout

    (

    )

    ;
  32. //
  33. // dtv_data
  34. //
  35. this

    .

    dtv_data

    .

    AllowUserToAddRows

    =

    false

    ;
  36. this

    .

    dtv_data

    .

    AllowUserToDeleteRows

    =

    false

    ;
  37. this

    .

    dtv_data

    .

    ColumnHeadersHeightSizeMode

    =

    System

    .

    Windows

    .

    Forms

    .

    DataGridViewColumnHeadersHeightSizeMode

    .

    AutoSize

    ;
  38. this

    .

    dtv_data

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    52

    , 25

    )

    ;
  39. this

    .

    dtv_data

    .

    Name

    =

    "dtv_data"

    ;
  40. this

    .

    dtv_data

    .

    ReadOnly

    =

    true

    ;
  41. this

    .

    dtv_data

    .

    RowTemplate

    .

    Height

    =

    28

    ;
  42. this

    .

    dtv_data

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    598

    , 339

    )

    ;
  43. this

    .

    dtv_data

    .

    TabIndex

    =

    0

    ;
  44. //
  45. // btn_connect
  46. //
  47. this

    .

    btn_connect

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 16F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  48. this

    .

    btn_connect

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    288

    , 384

    )

    ;
  49. this

    .

    btn_connect

    .

    Name

    =

    "btn_connect"

    ;
  50. this

    .

    btn_connect

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    151

    , 63

    )

    ;
  51. this

    .

    btn_connect

    .

    TabIndex

    =

    1

    ;
  52. this

    .

    btn_connect

    .

    Text

    =

    "Connect"

    ;
  53. this

    .

    btn_connect

    .

    UseVisualStyleBackColor

    =

    true

    ;
  54. this

    .

    btn_connect

    .

    Click

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Connect

    )

    ;
  55. //
  56. // Form1
  57. //
  58. this

    .

    AutoScaleDimensions

    =

    new

    System.Drawing

    .

    SizeF

    (

    9F, 20F)

    ;
  59. this

    .

    AutoScaleMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoScaleMode

    .

    Font

    ;
  60. this

    .

    ClientSize

    =

    new

    System.Drawing

    .

    Size

    (

    710

    , 470

    )

    ;
  61. this

    .

    Controls

    .

    Add

    (

    this

    .

    btn_connect

    )

    ;
  62. this

    .

    Controls

    .

    Add

    (

    this

    .

    dtv_data

    )

    ;
  63. this

    .

    Name

    =

    "Form1"

    ;
  64. this

    .

    StartPosition

    =

    System

    .

    Windows

    .

    Forms

    .

    FormStartPosition

    .

    CenterScreen

    ;
  65. this

    .

    Text

    =

    "Connecting to SQLite"

    ;
  66. (

    (

    System.ComponentModel

    .

    ISupportInitialize

    )

    (

    this

    .

    dtv_data

    )

    )

    .

    EndInit

    (

    )

    ;
  67. this

    .

    ResumeLayout

    (

    false

    )

    ;

  68. }

  69. #endregion

  70. private

    System

    .

    Windows

    .

    Forms

    .

    DataGridView

    dtv_data;
  71. private

    System

    .

    Windows

    .

    Forms

    .

    Button

    btn_connect;
  72. }
  73. }

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.
2018-04-09_14_51_49-.png


Then go to the browse and search sqlite, after that install it and wait until the process is completed.
2018-04-09_14_46_02-sqlite_-_microsoft_visual_studio.png


Next go to the Updates and update the needed framework to make sqlite work properly.
2018-04-09_14_47_44-sqlite_-_microsoft_visual_studio.png


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. namespace

    SQlite
  12. {
  13. public

    partial

    class

    Form1 :

    Form
  14. {
  15. String

    connectString;
  16. SQLiteConnection conn;
  17. SQLiteDataAdapter adapter;
  18. DataTable dt;

  19. public

    Form1(

    )
  20. {
  21. InitializeComponent(

    )

    ;
  22. connectString =

    @"Data Source=C:\Users\Computer\Desktop\SQlite\sql.db ;Version=3"

    ;
  23. }





  24. private

    void

    Connect(

    object

    sender, EventArgs e)

    {
  25. using

    (

    conn =

    new

    SQLiteConnection(

    connectString)

    )

    {
  26. try
  27. {
  28. conn.

    Open

    (

    )

    ;
  29. if

    (

    conn.

    State

    ==

    ConnectionState.

    Open

    )

    {
  30. MessageBox.

    Show

    (

    "Connected to database"

    )

    ;
  31. dt =

    new

    DataTable(

    )

    ;
  32. adapter =

    new

    SQLiteDataAdapter(

    "SELECT * FROM `data`"

    , conn)

    ;
  33. adapter.

    Fill

    (

    dt)

    ;
  34. dtv_data.

    DataSource

    =

    dt;
  35. conn.

    Close

    (

    )

    ;
  36. }
  37. }
  38. catch

    (

    Exception ex)

    {
  39. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  40. }
  41. }
  42. }
  43. }
  44. }

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!!!

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,512

356,407

356,429

Top
Raidforums