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

前端之家收集整理的这篇文章主要介绍了Sqlite删除所有表的数据 C#实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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;
        }
原文链接:https://www.f2er.com/sqlite/199769.html

猜你在找的Sqlite相关文章