加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Sqlite删除所有表的数据 C#实现

发布时间:2020-12-12 19:47:17 所属栏目:百科 来源:网络整理
导读:1,查询表名集合 SELECT name FROM sqlite_masterWHERE type='table'ORDER BY name; 2,编程语言遍历删除 public static bool deleteAllData() { bool res = false; SQLiteConnection conn = null; DbTransaction trans = null; try { string sql = "SELECT

1,查询表名集合

SELECT name FROM sqlite_master
WHERE type='table'
ORDER BY name;

2,编程语言遍历删除
 public static bool deleteAllData()
        {
            bool res = false;

            SQLiteConnection conn = null;
            DbTransaction trans = null;
            try
            {
                string sql = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name";
                DataSet ds = new DataSet();
                ds = CommonBll.getDataSetBySql(sql,Constant.SQLITE_FLAG);

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    using (conn = SqliteHelper.GetConnection())
                    {
                        var cmd = new SQLiteCommand(conn);
                        //开始事务
                        trans = conn.BeginTransaction();
                        //while (dr.Read())
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            string dSql = "delete from " + ds.Tables[0].Rows[i][0].ToString();

                            cmd.CommandText = dSql;
                            cmd.ExecuteNonQuery();
                        }
                        //提交事务
                        trans.Commit();

                        Console.ReadLine();
                        //ds.Close();

                        //成功标识
                        res = true;
                    }
                }

            }
            catch (Exception ex)
            {
                res = false;
                trans.Rollback();
            }
            finally
            {
                trans.Dispose();
            }

            return res;
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读