• We just launched and are currently in beta. Join us as we build and grow the community.

C# - Uploading Image With SQLite

as457

Loot Ninja
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
67
Likes
172
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
In this tutorial we will create a Uploading Image With SQLite using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. It has a designed for improving productivity in the development of Web applications. It has a friendly environment for all new developers. 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/.

Setting up SQLite

SQLite is very carefully tested prior to every release and relevant to use in some way. SQLite is very usable in any environments especially in embedded devices. 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-21_10_50_13-.png


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


Next go to the Updates and update the needed framework to make sqlite work properly.
2018-04-21_10_53_15-uploading_photo_-_microsoft_visual_studio.png

Note: Update only the framework if there is an available new update.

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. Rename the form as Main.cs and then write these codes inside your designer file.
  1. namespace

    Uploading_Photo
  2. {
  3. partial

    class

    Main
  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

    .

    txt_name

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    (

    )

    ;
  29. this

    .

    label1

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  30. this

    .

    pb_photo

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    PictureBox

    (

    )

    ;
  31. this

    .

    label2

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  32. this

    .

    txt_contact

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    (

    )

    ;
  33. this

    .

    btn_save

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Button

    (

    )

    ;
  34. this

    .

    btn_upload

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Button

    (

    )

    ;
  35. (

    (

    System.ComponentModel

    .

    ISupportInitialize

    )

    (

    this

    .

    pb_photo

    )

    )

    .

    BeginInit

    (

    )

    ;
  36. this

    .

    SuspendLayout

    (

    )

    ;
  37. //
  38. // txt_name
  39. //
  40. this

    .

    txt_name

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  41. this

    .

    txt_name

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    134

    , 68

    )

    ;
  42. this

    .

    txt_name

    .

    Name

    =

    "txt_name"

    ;
  43. this

    .

    txt_name

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    237

    , 35

    )

    ;
  44. this

    .

    txt_name

    .

    TabIndex

    =

    0

    ;
  45. //
  46. // label1
  47. //
  48. this

    .

    label1

    .

    AutoSize

    =

    true

    ;
  49. this

    .

    label1

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  50. this

    .

    label1

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    53

    , 71

    )

    ;
  51. this

    .

    label1

    .

    Name

    =

    "label1"

    ;
  52. this

    .

    label1

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    75

    , 27

    )

    ;
  53. this

    .

    label1

    .

    TabIndex

    =

    1

    ;
  54. this

    .

    label1

    .

    Text

    =

    "Name"

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

    .

    pb_photo

    .

    BorderStyle

    =

    System

    .

    Windows

    .

    Forms

    .

    BorderStyle

    .

    FixedSingle

    ;
  59. this

    .

    pb_photo

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    428

    , 30

    )

    ;
  60. this

    .

    pb_photo

    .

    Name

    =

    "pb_photo"

    ;
  61. this

    .

    pb_photo

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    268

    , 272

    )

    ;
  62. this

    .

    pb_photo

    .

    SizeMode

    =

    System

    .

    Windows

    .

    Forms

    .

    PictureBoxSizeMode

    .

    StretchImage

    ;
  63. this

    .

    pb_photo

    .

    TabIndex

    =

    2

    ;
  64. this

    .

    pb_photo

    .

    TabStop

    =

    false

    ;
  65. //
  66. // label2
  67. //
  68. this

    .

    label2

    .

    AutoSize

    =

    true

    ;
  69. this

    .

    label2

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  70. this

    .

    label2

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    33

    , 150

    )

    ;
  71. this

    .

    label2

    .

    Name

    =

    "label2"

    ;
  72. this

    .

    label2

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    95

    , 27

    )

    ;
  73. this

    .

    label2

    .

    TabIndex

    =

    4

    ;
  74. this

    .

    label2

    .

    Text

    =

    "Contact"

    ;
  75. //
  76. // txt_contact
  77. //
  78. this

    .

    txt_contact

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  79. this

    .

    txt_contact

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    134

    , 147

    )

    ;
  80. this

    .

    txt_contact

    .

    Name

    =

    "txt_contact"

    ;
  81. this

    .

    txt_contact

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    237

    , 35

    )

    ;
  82. this

    .

    txt_contact

    .

    TabIndex

    =

    3

    ;
  83. //
  84. // btn_save
  85. //
  86. this

    .

    btn_save

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  87. this

    .

    btn_save

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    184

    , 236

    )

    ;
  88. this

    .

    btn_save

    .

    Name

    =

    "btn_save"

    ;
  89. this

    .

    btn_save

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    121

    , 44

    )

    ;
  90. this

    .

    btn_save

    .

    TabIndex

    =

    5

    ;
  91. this

    .

    btn_save

    .

    Text

    =

    "SAVE"

    ;
  92. this

    .

    btn_save

    .

    UseVisualStyleBackColor

    =

    true

    ;
  93. this

    .

    btn_save

    .

    Click

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Save

    )

    ;
  94. //
  95. // btn_upload
  96. //
  97. this

    .

    btn_upload

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 8F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  98. this

    .

    btn_upload

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    485

    , 320

    )

    ;
  99. this

    .

    btn_upload

    .

    Name

    =

    "btn_upload"

    ;
  100. this

    .

    btn_upload

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    141

    , 44

    )

    ;
  101. this

    .

    btn_upload

    .

    TabIndex

    =

    6

    ;
  102. this

    .

    btn_upload

    .

    Text

    =

    "Browse Image"

    ;
  103. this

    .

    btn_upload

    .

    UseVisualStyleBackColor

    =

    true

    ;
  104. this

    .

    btn_upload

    .

    Click

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Upload

    )

    ;
  105. //
  106. // Main
  107. //
  108. this

    .

    AutoScaleDimensions

    =

    new

    System.Drawing

    .

    SizeF

    (

    9F, 20F)

    ;
  109. this

    .

    AutoScaleMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoScaleMode

    .

    Font

    ;
  110. this

    .

    ClientSize

    =

    new

    System.Drawing

    .

    Size

    (

    722

    , 428

    )

    ;
  111. this

    .

    Controls

    .

    Add

    (

    this

    .

    btn_upload

    )

    ;
  112. this

    .

    Controls

    .

    Add

    (

    this

    .

    btn_save

    )

    ;
  113. this

    .

    Controls

    .

    Add

    (

    this

    .

    label2

    )

    ;
  114. this

    .

    Controls

    .

    Add

    (

    this

    .

    txt_contact

    )

    ;
  115. this

    .

    Controls

    .

    Add

    (

    this

    .

    pb_photo

    )

    ;
  116. this

    .

    Controls

    .

    Add

    (

    this

    .

    label1

    )

    ;
  117. this

    .

    Controls

    .

    Add

    (

    this

    .

    txt_name

    )

    ;
  118. this

    .

    Name

    =

    "Main"

    ;
  119. this

    .

    StartPosition

    =

    System

    .

    Windows

    .

    Forms

    .

    FormStartPosition

    .

    CenterScreen

    ;
  120. this

    .

    Text

    =

    "Main"

    ;
  121. (

    (

    System.ComponentModel

    .

    ISupportInitialize

    )

    (

    this

    .

    pb_photo

    )

    )

    .

    EndInit

    (

    )

    ;
  122. this

    .

    ResumeLayout

    (

    false

    )

    ;
  123. this

    .

    PerformLayout

    (

    )

    ;

  124. }

  125. #endregion

  126. private

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    txt_name;
  127. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    label1;
  128. private

    System

    .

    Windows

    .

    Forms

    .

    PictureBox

    pb_photo;
  129. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    label2;
  130. private

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    txt_contact;
  131. private

    System

    .

    Windows

    .

    Forms

    .

    Button

    btn_save;
  132. private

    System

    .

    Windows

    .

    Forms

    .

    Button

    btn_upload;
  133. }
  134. }

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

Creating the Script

We will now create the script to make things work. To do that go to the csharp script called Main.cs then right click and select view code, this will force you to go to the text editor. Then write these block of codes inside the Class of the form.
  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.IO

    ;
  11. using

    System.Data.SQLite

    ;

  12. namespace

    Uploading_Photo
  13. {
  14. public

    partial

    class

    Main :

    Form
  15. {
  16. SQLiteConnection conn;
  17. SQLiteCommand cmd;
  18. String

    connectString;
  19. String

    image;

  20. public

    Main(

    )
  21. {
  22. InitializeComponent(

    )

    ;
  23. connectString =

    @"Data Source="

    +

    Application.

    StartupPath

    +

    @"\upload.db;version=3"

    ;
  24. GeneratDatabase(

    )

    ;
  25. }

  26. private

    void

    Save(

    object

    sender, EventArgs e)

    {
  27. byte

    [

    ]

    data =

    null

    ;

  28. if

    (

    txt_name.

    Text

    !=

    ""

    ||

    txt_contact.

    Text

    !=

    ""

    )
  29. {
  30. try
  31. {
  32. data =

    File.

    ReadAllBytes

    (

    image)

    ;

  33. conn =

    new

    SQLiteConnection(

    connectString)

    ;
  34. cmd =

    new

    SQLiteCommand(

    )

    ;
  35. cmd.

    CommandText

    =

    @"INSERT INTO member (name, contact, photo) VALUES(@name, @contact, @img)"

    ;
  36. cmd.

    Connection

    =

    conn;
  37. cmd.

    Parameters

    .

    Add

    (

    new

    SQLiteParameter(

    "@name"

    , txt_name.

    Text

    )

    )

    ;
  38. cmd.

    Parameters

    .

    Add

    (

    new

    SQLiteParameter(

    "@contact"

    , txt_contact.

    Text

    )

    )

    ;
  39. cmd.

    Parameters

    .

    Add

    (

    "@img"

    , DbType.

    Binary

    , data.

    Length

    )

    ;
  40. cmd.

    Parameters

    [

    "@img"

    ]

    .

    Value

    =

    data;
  41. conn.

    Open

    (

    )

    ;

  42. int

    i =

    cmd.

    ExecuteNonQuery

    (

    )

    ;

  43. if

    (

    i ==

    1

    )
  44. {
  45. MessageBox.

    Show

    (

    "Successfully Created!"

    )

    ;
  46. txt_name.

    Text

    =

    ""

    ;
  47. txt_contact.

    Text

    =

    ""

    ;
  48. pb_photo.

    Image

    =

    null

    ;
  49. image =

    ""

    ;
  50. }

  51. conn.

    Close

    (

    )

    ;
  52. }
  53. catch

    (

    Exception ex)
  54. {
  55. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  56. }

  57. }
  58. else
  59. {
  60. MessageBox.

    Show

    (

    "Required Field!"

    )

    ;
  61. }
  62. }


  63. private

    void

    GeneratDatabase(

    )

    {
  64. String

    path =

    Application.

    StartupPath

    +

    @"\upload.db"

    ;
  65. if

    (

    !

    File.

    Exists

    (

    path)

    )

    {
  66. conn =

    new

    SQLiteConnection(

    connectString)

    ;
  67. conn.

    Open

    (

    )

    ;
  68. string

    sql =

    "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, contact TEXT, photo BLOB)"

    ;
  69. cmd =

    new

    SQLiteCommand(

    sql, conn)

    ;
  70. cmd.

    ExecuteNonQuery

    (

    )

    ;
  71. conn.

    Close

    (

    )

    ;
  72. }
  73. }

  74. private

    void

    Upload(

    object

    sender, EventArgs e)

    {
  75. OpenFileDialog dialog =

    new

    OpenFileDialog(

    )

    ;
  76. dialog.

    Filter

    =

    "Choose Image(*.jpg;*.png;*.gif;) | *.jpg;*.png;*.gif;"

    ;
  77. if

    (

    dialog.

    ShowDialog

    (

    )

    ==

    DialogResult.

    OK

    )
  78. {
  79. pb_photo.

    Image

    =

    Image.

    FromFile

    (

    dialog.

    FileName

    )

    ;
  80. image =

    dialog.

    FileName

    ;
  81. }
  82. else

    {
  83. pb_photo.

    Image

    =

    null

    ;
  84. image =

    ""

    ;
  85. }
  86. }

  87. }
  88. }

Try to run the application and see if it works.

There you go we successfully created a Uploading Image With SQLite 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!!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top