SQLiteDatabase.GetDataTable C# (CSharp) Method

GetDataTable() public method

Allows the programmer to run a query against the Database.
public GetDataTable ( string sql ) : DataTable
sql string The SQL to run
return DataTable
    public DataTable GetDataTable(string sql)
    {
        DataTable dt = new DataTable();
        try
        {
            SQLiteConnection cnn = new SQLiteConnection(dbConnection);
            cnn.Open();
            SQLiteCommand mycommand = new SQLiteCommand(cnn);
            mycommand.CommandText = sql;
            SQLiteDataReader reader = mycommand.ExecuteReader();
            dt.Load(reader);
            reader.Close();
            cnn.Close();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return dt;
    }

Usage Example

Example #1
0
        static void Main(string[] args)
        {
            string dbPath  = string.Empty;
            string csvPath = string.Empty;

            if (args.Length > 0)
            {
                dbPath  = args[0];
                csvPath = args[1];
            }
            else
            {
                dbPath  = @"E:\WorkSpace\client_Manghuangji_Trunk_Android\Assets\R\Inside\database\game.bytes";//args[0];
                csvPath = "../../../ExportCSV";
            }

            Console.WriteLine("dbPath:" + dbPath);
            Console.WriteLine("csvPath:" + csvPath);

            if (!File.Exists(dbPath))
            {
                Console.WriteLine("not found " + dbPath);
                Console.ReadKey();
            }

            SQLiteDatabase db = new SQLiteDatabase(dbPath);
            DataTable      dataTable;
            String         query = "SELECT name FROM sqlite_master " + "WHERE type = 'table'" + "ORDER BY 1";

            dataTable = db.GetDataTable(query);

            ArrayList dataTableList = new ArrayList();

            foreach (DataRow row in dataTable.Rows)
            {
                dataTableList.Add(row.ItemArray[0].ToString());
            }

            string exportDirName = csvPath;

            if (Directory.Exists(exportDirName))
            {
                Directory.Delete(exportDirName, true);
            }
            Directory.CreateDirectory(exportDirName);

            for (int tableIndex = 0; tableIndex < dataTableList.Count; tableIndex++)
            {
                Console.WriteLine(string.Format("{0}/{1}:{2}", tableIndex, dataTableList.Count, dataTableList[tableIndex]));

                DataTable tmpDataTable = db.GetDataTable("select * from " + dataTableList[tableIndex]);

                ExportToCSV(exportDirName, tmpDataTable);
            }

            Console.WriteLine("ExportSqliteToCVS Finish");
            Console.ReadKey();
        }
All Usage Examples Of SQLiteDatabase::GetDataTable