• 备份sqlite操作


            /// <summary>
            
    /// 执行语句
            
    /// </summary>
            
    /// <param name="dbPath"></param>
            
    /// <param name="sql"></param>
            static void ExecSql(string dbPath, string sql)
            {
                ExecSql(dbPath, sql, 
    null);
            }

            
    /// <summary>
            
    /// 执行语句
            
    /// </summary>
            
    /// <param name="dbPath"></param>
            
    /// <param name="sql"></param>
            
    /// <param name="prams"></param>
            static void ExecSql(string dbPath, string sql, SQLiteParameter[] prams)
            {
                
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath))
                {
                    conn.Open();
                    
    using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                    {
                        
    if (prams != null)
                        {
                            
    foreach (SQLiteParameter parameter in prams)
                            {
                                cmd.Parameters.Add(parameter);
                            }
                        }
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    }
                    conn.Close();
                    conn.Dispose();
                }
            }

            
    /// <summary>
            
    /// 执行查询
            
    /// </summary>
            
    /// <param name="dbPath"></param>
            
    /// <param name="sql"></param>
            
    /// <param name="fn"></param>
            static void ReadSql(string dbPath, string sql, Action<SQLiteDataReader> fn)
            {
                ReadSql(dbPath, sql, fn, 
    null);
            }

            
    /// <summary>
            
    /// 执行查询
            
    /// </summary>
            
    /// <param name="dbPath"></param>
            
    /// <param name="sql"></param>
            
    /// <param name="fn"></param>
            
    /// <param name="prams"></param>
            static void ReadSql(string dbPath, string sql, Action<SQLiteDataReader> fn, SQLiteParameter[] prams)
            {
                
    using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath))
                {
                    conn.Open();
                    
    using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                    {
                        
    if (prams != null)
                        {
                            
    foreach (SQLiteParameter parameter in prams)
                            {
                                cmd.Parameters.Add(parameter);
                            }
                        }
                        
    using (SQLiteDataReader rs = cmd.ExecuteReader())
                        {                        
                            
    while (rs.Read())
                            {
                                fn(rs);
                            }
                            rs.Close();
                            rs.Dispose();
                        }
                        cmd.Dispose();
                    }
                    conn.Close();
                    conn.Dispose();
                }
            }
  • 相关阅读:
    C# 高效字符串连接 StringBuilder介绍
    C#图解教程
    C#中string类型是值类型还是引用类型?
    UML类图10分钟快速入门
    C#设计模式--单例模式
    计算机是如何启动的?
    2018年计划
    转:SQL进阶之变量、事务、存储过程与触发器
    2020/02/06,武汉
    2020/02/06,渐渐,from eason for you for her
  • 原文地址:https://www.cnblogs.com/jiang_zheng/p/sqlite.html
Copyright © 2020-2023  润新知