• C# SQLite数据库操作


    WinCE项目开发   VS2008

    自己写的SQLite数据库管理类代码如下:

    SQLiteManager.cs 

    using System.Data;
    using System.Data.SQLite;
    using System.IO;
    using System.Reflection;
    
    namespace TeachHMI
    {
        public class SQLiteManager
        {
            public SQLiteManager(string DataBaseName,string DataTableName)
            {
                this.dbName = DataBaseName;
                this.dtName = DataTableName;
            }
    
            //数据库文件名
            string dbName;
            //数据表名
            string dtName;
            string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            //数据库连接语句
            string ConnStr;
            //数据库连接
            public SQLiteConnection conn;
            //SQLiteCommand实例,用于对SQLite发出指令
            SQLiteCommand command;
            //SQLiteDataAdapter实例,用于将数据库数据填充至DataSet
            SQLiteDataAdapter adapter;
            //SQLiteDataReader实例,用于读取数据表中数据
            SQLiteDataReader reader;
    
            /// <summary>
            /// 数据库连接
            /// </summary>
            public void InitializeSQLite()
            {
                this.ConnStr = "Data Source=" + appPath + "\" + dbName + ";Pooling=true;FailIfMissing=false";
                conn = new SQLiteConnection(ConnStr);
                //打开连接
                conn.Open();
            }
    
            /// <summary>
            /// 创建数据表
            /// </summary>
            /// <param name="CreateCommand"></param>
            public void CreateTable(string CreateCommand)
            {
                command = new SQLiteCommand(conn);
                command.CommandText = "CREATE TABLE IF NOT EXISTS " + dtName + CreateCommand;
                command.ExecuteNonQuery();
            }
    
            /// <summary>
            /// 执行SQL命令,返回受影响的行数
            /// </summary>
            /// <param name="Command">命令语句</param>
            public void ExeNonQueryCmd(string Command)
            {
                command = new SQLiteCommand(conn);
                command.CommandText = Command;
                command.ExecuteNonQuery();
            }
    
            /// <summary>
            /// 执行SQL命令,返回SQLiteDataReader
            /// </summary>
            /// <param name="Command"></param>
            /// <returns></returns>
            public SQLiteDataReader ExeReaderCmd(string Command)
            {
                command = new SQLiteCommand(conn);
                command.CommandText = Command;
                reader = command.ExecuteReader();  //返回类型为SQLiteDataReader
                return reader;
            }
    
            /// <summary>
            /// 填充DataSet
            /// </summary>
            /// <param name="ds"></param>
            public void DataFill(DataSet ds)
            {
                adapter = new SQLiteDataAdapter("SELECT * FROM " + dtName, conn);
                adapter.Fill(ds, dtName);
            }
        }
    }

    读取数据表中某单元格内数据:

    public static SQLiteManager SQLlog;
    public static SQLiteDataReader Reader;
    int Num = int.Parse(dataGrid1[dataGrid1.CurrentRowIndex, 0].ToString());
    string SearchCmd = "SELECT * FROM InfoLog WHERE 序号=" + Num + "";
    Reader = SQLlog.ExeReaderCmd(SearchCmd);
    Reader.Read();
    listView1.Items[0].SubItems[1].Text = Reader.GetString(7);  //得到数据表中序号为 Num 的这条记录中索引为 7 的列中数据
    listView1.Items[1].SubItems[1].Text = Reader.GetString(8);
    Reader.Close();
    插入指令:INSERT INTO UserInfo VALUES 1,小李
    更新指令:UPDATE UserInfo SET 用户ID=1
    删除指令:DELETE FROM UserInfo WHERE 用户ID=2,姓名='小李'
  • 相关阅读:
    程序员学习提高必看的一篇文章
    SpringAOP拦截器的代理机制
    springboot03_RabbitMQ
    Docker_02
    Docker_01
    Redis_02
    Redis_01
    关于Linux下内存和Swap
    密码学DAY2
    密码学DAY1_02
  • 原文地址:https://www.cnblogs.com/npucloud/p/5630676.html
Copyright © 2020-2023  润新知