Operation list of features:
- Function 1: Read all table / index / view
- Function 2: to read table data
Function 1: Read all table / index / view
Each SQLite database has called sqlit_master table, which stores a data structure of the database (table structure, view of the structure, the index structure). Therefore, by reading sqlit_master it will be able to obtain all the information table.
Gets the table name
SELECT name FROM sqlite_master WHERE TYPE='table' ORDER BY name
Gets the index
SELECT name FROM sqlite_master WHERE TYPE='index' ORDER BY name
Get View
SELECT name FROM sqlite_master WHERE TYPE='view' ORDER BY name
To get the table name, for example, the complete code
1 public DataSet GetTableNames(string path) { 2 string strSQL = "SELECT name FROM sqlite_master WHERE TYPE='table' ORDER BY name"; 3 DataSet ds = null; 4 try { 5 SQLiteConnection conn = new SQLiteConnection(path); 6 SQLiteCommand cmd = new SQLiteCommand(strSQL, conn); 7 SQLiteDataAdapter reciever = new SQLiteDataAdapter(cmd); 8 ds = new DataSet(); 9 reciever.Fill(ds); 10 return ds; 11 } catch { 12 MessageBox.Show("There is no data table"); 13 } 14 return ds; 15 }
16 DataSet dbnames = GetTableNames(DBPath);
Note that at this time is returned ds element contains a number only, all the table names in column vector form is stored in a table (i.e. ds only element).
Reading the table of code number
int tablecount = dbnames.Tables[0].Rows.Count;
Read the index for the X table name
string tablename = dbnames.Table[0].Rows[X].ItemArray[0].ToString();//X starts from 0
Function 2: to read table data
1 public DataTable GetDataTable(string strSQL, string path){ 2 DataTable dt = null; 3 try { 4 SQLiteConnection conn = new SQLiteConnection(path); 5 SQLiteCommand cmd = new SQLiteCommand(strSQL,conn); 6 SQLiteDataAdapter reciever = new SQLiteDataAdapter(cmd); 7 dt = new DataTable(); 8 reciever.Fill(dt); 9 return dt; 10 } catch{ 11 MessageBox.Show("There is no such a datatable"); 12 } 13 return dt; 14 }
Wherein strSQL is acquired instruction data in the file table db
string sSQL = "SELECT * FROM item_compound;";
Here the data table called " item_compound ."
Road Path to file
public static string DBPath = string.Format(@"Data Source={0}", Application.StartupPath + @"\CCUS_supstr_temp.db");//the path of .db file
Here db file named " CCUS_supstr_temp.db ."