Advertisement
Guest User

Untitled

a guest
Jul 4th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SQLite;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. namespace PwdManager
  10. {
  11.     class DBManager
  12.     {
  13.         private static string connStr = "./pwdmanager.db";
  14.         //创建数据库
  15.         public static void CreateDB()
  16.         {
  17.             SQLiteConnection.CreateFile("./pwdmanager.db");
  18.         }
  19.  
  20.         public static string getConnStr()
  21.         {
  22.             if (!File.Exists("./pwdmanager.db"))
  23.             {
  24.                 CreateDB();
  25.                 initDB();
  26.             }
  27.             SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
  28.             connstr.DataSource = connStr;
  29.             return connstr.ToString();
  30.         }
  31.  
  32.         //数据库初始化
  33.         public static bool initDB()
  34.         {
  35.             ExecuteSql("create table user(username varchar(20) primary key, password varchar(20));");
  36.             ExecuteSql("create table records(name varchar(20) primary key, username varchar(100), account varchar(100), userpassword varchar(100), webaddr varchar(100), picpath varchar(100));");
  37.             if (ExecuteSql("insert into user values('qian_f', 'qian_f');") != 1)
  38.             {
  39.                 return false;
  40.             }
  41.             else
  42.             {
  43.                 return true;
  44.             }
  45.         }
  46.  
  47.         //执行Sql语句
  48.         private static int ExecuteSql(string sqlStr)
  49.         {
  50.             using (SQLiteConnection conn = new SQLiteConnection(getConnStr()))
  51.             {
  52.                 try
  53.                 {
  54.                     conn.Open();
  55.                     SQLiteCommand comm = conn.CreateCommand();
  56.                     comm.CommandText = sqlStr;
  57.                     comm.CommandType = System.Data.CommandType.Text;
  58.                     return comm.ExecuteNonQuery();
  59.                 }
  60.                 catch (Exception ex)
  61.                 {
  62.                     MessageBox.Show(ex.Message.ToString());
  63.                     return -1;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         //sql查询语句
  69.         private static SQLiteDataReader ExecQuery(string queryStr)
  70.         {
  71.             using (SQLiteConnection conn = new SQLiteConnection(getConnStr()))
  72.             {
  73.                 conn.Open();
  74.                 SQLiteCommand comm = conn.CreateCommand();
  75.                 comm.CommandText = queryStr;
  76.                 comm.CommandType = System.Data.CommandType.Text;
  77.                 return comm.ExecuteReader();
  78.             }
  79.         }
  80.  
  81.         //插入记录
  82.         public static bool InsetRecord(string name, string username, string account, string password, string webaddr)
  83.         {
  84.             string str = "insert into records values('" + name + "', '" + username + "', '" + account + "', '" + password + "', '" + webaddr + "', '');";
  85.             if (ExecuteSql(str) != 1)
  86.             {
  87.                 MessageBox.Show("添加记录失败!");
  88.                 return false;
  89.             }
  90.             else return true;
  91.         }
  92.  
  93.         //删除记录
  94.         public static bool DelRecord(string name)
  95.         {
  96.             string str = "delete from records where name = '" + name + "';";
  97.             if (ExecuteSql(str) != 1)
  98.             {
  99.                 MessageBox.Show("删除记录失败!");
  100.                 return true;
  101.             }
  102.             else
  103.             {
  104.                 return false;
  105.             }
  106.         }
  107.  
  108.         //修改记录
  109.         public static bool UpdateRecord(string name, string username, string account, string password, string webaddr)
  110.         {
  111.             string str = "update records set username='" + username + "', account='" + account + "', userpassword='" + password + "', webaddr='" + webaddr + "' where name='" + name + "';";
  112.             if (ExecuteSql(str) != 1)
  113.             {
  114.                 MessageBox.Show("修改记录失败!");
  115.                 return false;
  116.             }
  117.             else return true;        
  118.         }
  119.    
  120.         //校验登录用户和密码
  121.         public static bool CheckLogin(string name, string password)
  122.         {
  123.             using (SQLiteConnection conn = new SQLiteConnection(getConnStr()))
  124.             {
  125.                 conn.Open();
  126.                 string query = "select password from user where username='" + name + "';";
  127.                 SQLiteCommand comm = conn.CreateCommand();
  128.                 comm.CommandText = query;
  129.                 comm.CommandType = System.Data.CommandType.Text;
  130.                 SQLiteDataReader reader = comm.ExecuteReader();
  131.                 if (reader.HasRows == false)
  132.                 {
  133.                     return false;
  134.                 }
  135.                 reader.Read();
  136.                 if (reader[0].ToString().Equals(password))
  137.                 {
  138.                     return true;
  139.                 }
  140.                 else
  141.                 {
  142.                     return false;
  143.                 }
  144.             }
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement