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;
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章