Nice Tutorial
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:)
namespace Uploading_Photo { partial class Main { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { ((System.ComponentModel.ISupportInitialize)(this.pb_photo)).BeginInit(); this.SuspendLayout(); // // txt_name // this.txt_name.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.txt_name.Name = "txt_name"; this.txt_name.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.Name = "label1"; this.label1.TabIndex = 1; this.label1.Text = "Name"; // // pb_photo // this.pb_photo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pb_photo.Name = "pb_photo"; this.pb_photo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pb_photo.TabIndex = 2; this.pb_photo.TabStop = false; // // label2 // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label2.Name = "label2"; this.label2.TabIndex = 4; this.label2.Text = "Contact"; // // txt_contact // this.txt_contact.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.txt_contact.Name = "txt_contact"; this.txt_contact.TabIndex = 3; // // btn_save // this.btn_save.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btn_save.Name = "btn_save"; this.btn_save.TabIndex = 5; this.btn_save.Text = "SAVE"; this.btn_save.UseVisualStyleBackColor = true; // // btn_upload // this.btn_upload.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.btn_upload.Name = "btn_upload"; this.btn_upload.TabIndex = 6; this.btn_upload.Text = "Browse Image"; this.btn_upload.UseVisualStyleBackColor = true; // // Main // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.btn_upload); this.Controls.Add(this.btn_save); this.Controls.Add(this.label2); this.Controls.Add(this.txt_contact); this.Controls.Add(this.pb_photo); this.Controls.Add(this.label1); this.Controls.Add(this.txt_name); this.Name = "Main"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Main"; ((System.ComponentModel.ISupportInitialize)(this.pb_photo)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txt_name; private System.Windows.Forms.Label label1; private System.Windows.Forms.PictureBox pb_photo; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txt_contact; private System.Windows.Forms.Button btn_save; private System.Windows.Forms.Button btn_upload; } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Data.SQLite; namespace Uploading_Photo { public partial class Main : Form { SQLiteConnection conn; SQLiteCommand cmd; String connectString; String image; public Main() { InitializeComponent(); connectString = @"Data Source=" + Application.StartupPath + @"\upload.db;version=3"; GeneratDatabase(); } private void Save(object sender, EventArgs e) { byte[] data = null; if (txt_name.Text != "" || txt_contact.Text != "" ) { try { data = File.ReadAllBytes(image); cmd.CommandText = @"INSERT INTO member (name, contact, photo) VALUES(@name, @contact, @img)"; cmd.Connection = conn; cmd.Parameters.Add("@img", DbType.Binary, data.Length); cmd.Parameters["@img"].Value = data; conn.Open(); int i = cmd.ExecuteNonQuery(); if (i == 1) { MessageBox.Show("Successfully Created!"); txt_name.Text = ""; txt_contact.Text = ""; pb_photo.Image = null; image = ""; } conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("Required Field!"); } } private void GeneratDatabase() { String path = Application.StartupPath + @"\upload.db"; if (!File.Exists(path)) { conn.Open(); string sql = "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, contact TEXT, photo BLOB)"; cmd.ExecuteNonQuery(); conn.Close(); } } private void Upload(object sender, EventArgs e) { dialog.Filter = "Choose Image(*.jpg;*.png;*.gif;) | *.jpg;*.png;*.gif;"; if (dialog.ShowDialog() == DialogResult.OK) { pb_photo.Image = Image.FromFile(dialog.FileName); image = dialog.FileName; } else { pb_photo.Image = null; image = ""; } } } }