C # study notes (c) - SQLITE database

Sqlite using methods in C #:

[Installation] Sqlite

first step:
References three documents
sqlite-netFx40-binary-bundle-x64-2010-1.0.111.0.zip
(Or SQLite -netFx40-binary-bundle-Win32- 2010 - 1.0 . 94.0 .zip)    
 // Note use with a bundle package, or else will be prompted to lack SQLite.Interop.dll, with their own computer to download the version with .NET matching bag, 
more packets are available on the official website (HTTP: // system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki) to download 
1 .System.Data.SQLite.dll
 2 .System .Data.SQLite.EF6.dll
 3 .System.Data.SQLite.Linq.dll

// code references the DLL version needs to be consistent with the target platform version (platform configuration to run the program target platform to keep consistent) keeping in mind the objectives of the framework, NET version.

Step two:
We need to bag System.Data.SQLite.dll and generator placed in the same directory (Debug) 

head Add to add:
a using System.Data.SQLite; // use SQLite database file header will contain
using System.IO ; // do file operations file, File.Exists function requires
using System.Data.SQLiteClient; // need ExecuteReader function
step three:
[create] Sqlite database SQLiteConnection.CreateFile (
" testDB.sqlite " );

first determine whether there is a database file

bool dbExist = File.Exists ( "DB.sqlite" ); // File.Exists function by determining whether the database file already exists, BOOL return value is true or false (Turl FALSE)
IF (== dbExist to false) // if not exist, create



Sqlite database initialization []
// create a connection string CONN ,,, Version representation Sqlite version 
SQLiteConnection conn = new new SQLiteConnection ( " the Data Source = testDB.sqlite; Version = 3; " );
 // set the database password 
conn.SetPassword ( " 123456 " );
 // open database 
conn.Open ();
 // Create a data 'login', login: Create table USERNAME, and fill in the fields ID, nAME, PASS (including field names, field types format, the initial contents field) 
String Login = " Create User Table (ID int (. 8) default (91 is), name VARCHAR (16) default ( 'ADMIN'), password int (16) default (96333)) " ;
 // create a command and includes the command content 
SQLiteCommand = cmd new newSQLiteCommand (the Login, conn);            
 // execute command 
cmd.ExecuteNonQuery ();
 // release resources 
conn.Close ();

[Insert data into the database]
 Conn SQLiteConnection = new new SQLiteConnection ( " the Data = testDB.sqlite the Source; Version =. 3; Password = 123456; " );
 // Note phrase Password = 123456; open the database with a password must bring the parameters and corresponding values 
conn. the Open ();
 String user1 = " insert into User (ID, name, password) values (2, 'Mike', 87654321) " ;    // use the function insert data insert into 
SQLiteCommand cmd = new new SQLiteCommand (user1, Conn);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();

[Insert Variable Data]

            // create a registration table in the database, and insert the variable data 
            String u_name, U_ID, u_pass;
             // U_ID = Convert.ToInt32 (textBox1.Text); read ID, read-only digital 
            U_ID = the this .textBox1.Text;                               // reads ID 
            u_name = the this .textBox2.Text;                        // read name string 
            u_pass = the this .textBox3.Text;                          // read the password 
            / * *************** ********************* build the database *************************** ********************* * / 
            SQLiteConnection.CreateFile ( " LOGINDB.sqlite ");
            SQLiteConnection conn = new SQLiteConnection("Data Source=LOGINDB.sqlite;Version=3");
            conn.SetPassword("123456");
            conn.Open();
            string login = "create table user (id,name, password)";
            SQLiteCommand cmd = new SQLiteCommand(login, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            / * ********************************* inserted data ************* ***************************************** * / 
             conn = new new SQLiteConnection ( " the Data = LOGINDB.sqlite the Source; Version =. 3; password = 123456; " );
             // Note phrase password = 123456; open the database with a password must bring this parameter and the corresponding values 
            conn.Open ();
             String Cr = " User INTO INSERT (ID, name, password) values ( ' " + + U_ID " ', ' " + + u_name " ', ' " + u_pass + " ') " ;
            //( '" + u_id + "', '" + u_name + "','" + u_pass + "')为固定表达式
            cmd = new SQLiteCommand(cr, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            cmd.Dispose();

Find and deliver the data in the database

 // read just whether the data submitted already exists in the database to determine whether the submission is successful 
            String YZ1 = "" ;
             String YZ2 = "" ;
             String YZ3 = "" ;
             a using (conn = new new SQLiteConnection ( " the Data Source = DB.sqlite ; Version =. 3; Password = 123456 " ))
            {
                conn.Open();
                using ( cmd = new SQLiteCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = string.Format("select  *  from user  where id ='" + inid + "'  and  name = '" + inname + "'  and  password='" + inpass + "' " );  
                    using ( dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (dr.Read())
                        {   
                            yz1 = dr["id"].ToString();
                            yz2 = dr["name"].ToString();
                            yz3 = dr["password"].ToString();
                            textBox4.Text = YZ1;    // this extra step-outs, just look at this demo reads and assignment operator 
                            textBox5.Text = YZ2;
                            textBox6.Text = yz3;
                             MessageBox.Show ( " submit success " );
                        }
                        the else  
                            MessageBox.Show ( " Submission failed, please try again! " );
                    }

                }
            }

 

Guess you like

Origin www.cnblogs.com/gougouwang/p/11669570.html