SQLite Create and delete changes

Source: Internet
Author: User
Tags sqlite password protection

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SQLite;usingSystem.Data.Common;usingSystem.IO;namespacesqlitedemo{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }                        stringSqlitepath = Application.startuppath +"/test.db"; System.Data.SQLite.SQLiteConnection Conn=NewSystem.Data.SQLite.SQLiteConnection (); System.Data.SQLite.SQLiteCommand cmd=NewSystem.Data.SQLite.SQLiteCommand (); stringSQL; /// <summary>        ///Create a database/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidBtncreat_click (Objectsender, EventArgs e) {            if(File.exists (Sqlitepath)) {MessageBox.Show ("database already exists","Error", Messageboxbuttons.ok,messageboxicon.error); }            Else            {                Try                {                       //Create a database fileSystem.Data.SQLite.SQLiteConnection.CreateFile (Sqlitepath); }                Catch(Exception ex) {MessageBox.Show ("failed to create database"+Ex.                 ToString ()); }             }         }                Private voidBtnconnect_click (Objectsender, EventArgs e) {            //connecting to a database                        Try{System.Data.SQLite.SQLiteConnectionStringBuilder connstr=NewSystem.Data.SQLite.SQLiteConnectionStringBuilder (); ConnStr. DataSource=Sqlitepath; //ConnStr. Password = "admin";//set Password, SQLite ADO. NET implements the database password protectionConn. ConnectionString =ConnStr.                ToString (); Conn.                Open (); Cmd. Connection=Conn; }                   Catch(Exception ex) {MessageBox.Show ("connecting to a database"+Ex.            ToString ()); }        }        Private voidBtncreattable_click (Objectsender, EventArgs e) {            Try            {                //Create a tablesql ="CREATE TABLE Test (username varchar, password varchar )"; Cmd.commandtext=SQL; Cmd. Connection=Conn; Cmd.            ExecuteNonQuery (); }            Catch(Exception ex) {MessageBox.Show ("failed to create table"+Ex.             ToString ()); }        }        Private voidbtnAdd_Click (Objectsender, EventArgs e) {            Try{SQL="INSERT INTO Test (Username,password) VALUES ('"+"123"+"', '"+"456"+"')"; Cmd.commandtext=SQL; Cmd.            ExecuteNonQuery (); }            Catch(Exception ex) {MessageBox.Show ("Increase Failure"+Ex.            ToString ()); }        }        Private voidBtnselect_click (Objectsender, EventArgs e) {            Try{listView1.Items.Clear (); SQL="SELECT * FROM Test"; Cmd.commandtext=SQL; System.Data.SQLite.SQLiteDataReader Reader=cmd.                ExecuteReader ();  while(reader. Read ()) {ListViewItem LVI=NewListViewItem (); Lvi. Text= (ListView1.Items.Count +1).                    ToString (); Lvi. SubItems.Add (reader. GetString (0)); Lvi. SubItems.Add (reader. GetString (1));                LISTVIEW1.ITEMS.ADD (LVI); } reader.            Close (); }            Catch(Exception ex) {MessageBox.Show ("Read failed"+Ex.            ToString ()); }        }        Private voidBtndeleteall_click (Objectsender, EventArgs e) {SQL="Delete from Test"; Cmd.commandtext=SQL; Cmd.        ExecuteNonQuery (); }        Private voidBtndelete_click (Objectsender, EventArgs e) {SQL="DELETE from test WHERE username = '"+textbox1.text.trim () +"'"; Cmd.commandtext=SQL; Cmd.        ExecuteNonQuery (); }          }}

SQLite Create and delete changes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.