(i) Connect to database & query Database (example: User Login window)
Private voidBtnlogin_click (Objectsender, EventArgs e) { //Login Detection Try { //connecting to a database stringDbPath ="Data source="+ directory.getcurrentdirectory () +@"\data\orders.db; version=3; password=07762828216;"; Sqliteconnection Conn=Newsqliteconnection (DbPath); Conn. Open (); //query whether there is any corresponding data stringSql_select ="Select COUNT (0) from users where username = '"+ TxbUsername.Text.Trim () +"' and password = '"+ TxbPassword.Text.Trim () +"'"; Sqlitecommand cmd=NewSqlitecommand (Sql_select,conn); Sqlitedatareader Reader=cmd. ExecuteReader (); if(reader. Read ()) {stringresult = reader. GetValue (0). ToString (); if(Result = ="0") {MessageBox.Show ("user name or password is wrong, please re-enter! "); } Else{MessageBox.Show ("Login Successful! "); }} reader. Close (); Conn. Close (); } Catch(Exception ex) {MessageBox.Show ("failed to connect to database:"+Ex. Message); } }
VS2017 C # WinForm project using SQLite (ii)