zoukankan      html  css  js  c++  java
  • sqlLite的数据库操作类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SQLite;
    namespace FaceAttendance
    {
        class DBHelper
        {
            public SQLiteConnection conn;
            public DBHelper()
            {
                ConnectDatabase();
            }
            private void ConnectDatabase()
            {
                conn = new SQLiteConnection("Data Source=..\..\..\faceinfo.db3; verson = 3;"); //创建数据库实例,指定文件位置  
                conn.Open(); //打开数据库,若文件不存在会自动创建
                if (conn == null) throw new Exception("数据库连接失败。请检查数据库文件是否存在。");
            }
            public void insertperson(int Id,string name,string sex,string position,string faceinfo,string des)
            {
                string sql = null;
                if (des ==null)
                {
                    sql = "insert into info values(" + 14061143 + ",'" + name + "','" + sex + "','" + position + "'," + des + ");";
                }
                else
                {
                    sql = "insert into info values(" + 14061143 + ",'" + name + "','" + sex + "','" + position + "','" + des + "');";
                }
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                command.ExecuteNonQuery();
                command.Dispose();
            }
            public void insertAttendance(int Id,string time)
            {
                string sql = "insert into signinfo values("+Id+",'"+time+"');";
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                command.ExecuteNonQuery();
                command.Dispose();
            }
            public List<peopleinfo> getpeopleinfo()
            {
                List<peopleinfo> faceRlt = new List<peopleinfo>();
                string sql = "SELECT * FROM info;";
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                SQLiteDataReader reader = command.ExecuteReader();
                int j = 0;
                while (reader.Read())
                {
                    j++;
                    peopleinfo people = new peopleinfo();
                    people.Id = reader.GetInt32(0);
                    people.name = (string)reader["NAME"];
                    people.sex = (string)reader["SEX"];
                    people.position = (string)reader["JOB"];
                    string fs = (string)reader["faceinfo"];
                    string[] fsa = fs.Split(';');
                    for (int i = 0; i < 128; i++)
                        people.faceinfo[i] = (float)Convert.ToDouble(fsa[i]);
                    people.des = (string)reader["MSG"];
                    faceRlt.Add(people);
                }
                command.Dispose();
                reader.Dispose();
                return faceRlt;
            }
            public void insertPersonRecord(string name,string time)
            {
                string sql = "insert into personRecord values(' " + name + "','" + time + "');";
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                command.ExecuteNonQuery();
                command.Dispose();
            }
            public peopleinfo getPersonInfo(int Id)
            {
                string sql = "SELECT * FROM info WHERE Id = "+Id;
                SQLiteCommand command = new SQLiteCommand(sql, conn);
                SQLiteDataReader reader = command.ExecuteReader();
                reader.Read();
                peopleinfo people = new peopleinfo();
                people.Id = reader.GetInt32(0);
                people.name = (string)reader["NAME"];
                people.sex = (string)reader["SEX"];
                people.position = (string)reader["JOB"];
                people.des = (string)reader["MSG"];
                return people;
            }
        }
    }
  • 相关阅读:
    C# MessageBox.Show()超时后 自动关闭
    WPF中的依赖属性和附加属性
    c# 获取当前活动窗口句柄,获取窗口大小及位置
    c# 数组间相互转换 int[] string[] object[]
    WPF中判断DataGrid增加复选框和头部全选,以及判断哪一行的checkbox被选中
    WPF DataGridTemplateColumn添加按钮和按钮事件获取行参数
    WPF 自定义分页控件 ---- DataPager
    WPF的MVVM模式给ComboBox绑定数据和读取
    DataBinding 绑定计算表达式
    c# 调用方法超时直接返回的功能
  • 原文地址:https://www.cnblogs.com/codeDog123/p/6539618.html
Copyright © 2011-2022 走看看