C# - Uploading Image With SQLite

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

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.
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.
tut3Note: 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.  
  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.txt_name = new System.Windows.Forms.TextBox();
  32. this.label1 = new System.Windows.Forms.Label();
  33. this.pb_photo = new System.Windows.Forms.PictureBox();
  34. this.label2 = new System.Windows.Forms.Label();
  35. this.txt_contact = new System.Windows.Forms.TextBox();
  36. this.btn_save = new System.Windows.Forms.Button();
  37. this.btn_upload = new System.Windows.Forms.Button();
  38. ((System.ComponentModel.ISupportInitialize)(this.pb_photo)).BeginInit();
  39. this.SuspendLayout();
  40. //
  41. // txt_name
  42. //
  43. this.txt_name.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  44. this.txt_name.Location = new System.Drawing.Point(134, 68);
  45. this.txt_name.Name = "txt_name";
  46. this.txt_name.Size = new System.Drawing.Size(237, 35);
  47. this.txt_name.TabIndex = 0;
  48. //
  49. // label1
  50. //
  51. this.label1.AutoSize = true;
  52. this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  53. this.label1.Location = new System.Drawing.Point(53, 71);
  54. this.label1.Name = "label1";
  55. this.label1.Size = new System.Drawing.Size(75, 27);
  56. this.label1.TabIndex = 1;
  57. this.label1.Text = "Name";
  58. //
  59. // pb_photo
  60. //
  61. this.pb_photo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  62. this.pb_photo.Location = new System.Drawing.Point(428, 30);
  63. this.pb_photo.Name = "pb_photo";
  64. this.pb_photo.Size = new System.Drawing.Size(268, 272);
  65. this.pb_photo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  66. this.pb_photo.TabIndex = 2;
  67. this.pb_photo.TabStop = false;
  68. //
  69. // label2
  70. //
  71. this.label2.AutoSize = true;
  72. this.label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  73. this.label2.Location = new System.Drawing.Point(33, 150);
  74. this.label2.Name = "label2";
  75. this.label2.Size = new System.Drawing.Size(95, 27);
  76. this.label2.TabIndex = 4;
  77. this.label2.Text = "Contact";
  78. //
  79. // txt_contact
  80. //
  81. this.txt_contact.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  82. this.txt_contact.Location = new System.Drawing.Point(134, 147);
  83. this.txt_contact.Name = "txt_contact";
  84. this.txt_contact.Size = new System.Drawing.Size(237, 35);
  85. this.txt_contact.TabIndex = 3;
  86. //
  87. // btn_save
  88. //
  89. this.btn_save.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  90. this.btn_save.Location = new System.Drawing.Point(184, 236);
  91. this.btn_save.Name = "btn_save";
  92. this.btn_save.Size = new System.Drawing.Size(121, 44);
  93. this.btn_save.TabIndex = 5;
  94. this.btn_save.Text = "SAVE";
  95. this.btn_save.UseVisualStyleBackColor = true;
  96. this.btn_save.Click += new System.EventHandler(this.Save);
  97. //
  98. // btn_upload
  99. //
  100. this.btn_upload.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  101. this.btn_upload.Location = new System.Drawing.Point(485, 320);
  102. this.btn_upload.Name = "btn_upload";
  103. this.btn_upload.Size = new System.Drawing.Size(141, 44);
  104. this.btn_upload.TabIndex = 6;
  105. this.btn_upload.Text = "Browse Image";
  106. this.btn_upload.UseVisualStyleBackColor = true;
  107. this.btn_upload.Click += new System.EventHandler(this.Upload);
  108. //
  109. // Main
  110. //
  111. this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
  112. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  113. this.ClientSize = new System.Drawing.Size(722, 428);
  114. this.Controls.Add(this.btn_upload);
  115. this.Controls.Add(this.btn_save);
  116. this.Controls.Add(this.label2);
  117. this.Controls.Add(this.txt_contact);
  118. this.Controls.Add(this.pb_photo);
  119. this.Controls.Add(this.label1);
  120. this.Controls.Add(this.txt_name);
  121. this.Name = "Main";
  122. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  123. this.Text = "Main";
  124. ((System.ComponentModel.ISupportInitialize)(this.pb_photo)).EndInit();
  125. this.ResumeLayout(false);
  126. this.PerformLayout();
  127.  
  128. }
  129.  
  130. #endregion
  131.  
  132. private System.Windows.Forms.TextBox txt_name;
  133. private System.Windows.Forms.Label label1;
  134. private System.Windows.Forms.PictureBox pb_photo;
  135. private System.Windows.Forms.Label label2;
  136. private System.Windows.Forms.TextBox txt_contact;
  137. private System.Windows.Forms.Button btn_save;
  138. private System.Windows.Forms.Button btn_upload;
  139. }
  140. }

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.  
  13. namespace Uploading_Photo
  14. {
  15. public partial class Main : Form
  16. {
  17. SQLiteConnection conn;
  18. SQLiteCommand cmd;
  19. String connectString;
  20. String image;
  21.  
  22. public Main()
  23. {
  24. InitializeComponent();
  25. connectString = @"Data Source=" + Application.StartupPath + @"\upload.db;version=3";
  26. GeneratDatabase();
  27. }
  28.  
  29. private void Save(object sender, EventArgs e) {
  30. byte[] data = null;
  31.  
  32. if (txt_name.Text != "" || txt_contact.Text != "" )
  33. {
  34. try
  35. {
  36. data = File.ReadAllBytes(image);
  37.  
  38. conn = new SQLiteConnection(connectString);
  39. cmd = new SQLiteCommand();
  40. cmd.CommandText = @"INSERT INTO member (name, contact, photo) VALUES(@name, @contact, @img)";
  41. cmd.Connection = conn;
  42. cmd.Parameters.Add(new SQLiteParameter("@name", txt_name.Text));
  43. cmd.Parameters.Add(new SQLiteParameter("@contact", txt_contact.Text));
  44. cmd.Parameters.Add("@img", DbType.Binary, data.Length);
  45. cmd.Parameters["@img"].Value = data;
  46. conn.Open();
  47.  
  48. int i = cmd.ExecuteNonQuery();
  49.  
  50. if (i == 1)
  51. {
  52. MessageBox.Show("Successfully Created!");
  53. txt_name.Text = "";
  54. txt_contact.Text = "";
  55. pb_photo.Image = null;
  56. image = "";
  57. }
  58.  
  59. conn.Close();
  60. }
  61. catch (Exception ex)
  62. {
  63. MessageBox.Show(ex.Message);
  64. }
  65.  
  66. }
  67. else
  68. {
  69. MessageBox.Show("Required Field!");
  70. }
  71. }
  72.  
  73.  
  74. private void GeneratDatabase() {
  75. String path = Application.StartupPath + @"\upload.db";
  76. if (!File.Exists(path)) {
  77. conn = new SQLiteConnection(connectString);
  78. conn.Open();
  79. string sql = "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, contact TEXT, photo BLOB)";
  80. cmd = new SQLiteCommand(sql, conn);
  81. cmd.ExecuteNonQuery();
  82. conn.Close();
  83. }
  84. }
  85.  
  86. private void Upload(object sender, EventArgs e) {
  87. OpenFileDialog dialog = new OpenFileDialog();
  88. dialog.Filter = "Choose Image(*.jpg;*.png;*.gif;) | *.jpg;*.png;*.gif;";
  89. if (dialog.ShowDialog() == DialogResult.OK)
  90. {
  91. pb_photo.Image = Image.FromFile(dialog.FileName);
  92. image = dialog.FileName;
  93. }
  94. else {
  95. pb_photo.Image = null;
  96. image = "";
  97. }
  98. }
  99.  
  100. }
  101. }

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


Comments

Thanks for the SQLite tutorial for Visual Studio.

Have used Visual Studio a little for SQLite projects and struggled to understand the flow. Thanks for making it easy. Please keep the SQLite tutorials coming:)

I'm glad I could help..

Great help, thank you

I'm glad that I could help...

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.