C# 獲取SqLite數據庫表信息以及獲取表內字段信息


已經封裝好的函數,即可調用
 1        #region 最新數據表信息顯示事件
 2         /// <summary>
 3         /// 最新數據表信息顯示事件
 4         /// </summary>
 5         /// <param name="sender"></param>
 6         /// <param name="e"></param>
 7         private void showNewSqliteInfo_Click(object sender, EventArgs e)
 8         {
 9             if (newDB)
10             {
11                 connectionString = string.Format(@"Data Source={0};Version=3;", ndb_Path);
12                 using (SQLiteConnection conn = new SQLiteConnection(connectionString))
13                 {
14                     conn.Open();
15                     DataTable schemaTable = conn.GetSchema("TABLES");
16                     // 移除數據表中特定的列
17                     schemaTable.Columns.Remove("TABLE_CATALOG");
18                     // 設定特定列的序號
19                     schemaTable.Columns["TABLE_NAME"].SetOrdinal(1);
20                     this.new_dataGridView1.DataSource = schemaTable;
21                     newClickState = false;
22                 }
23             }
24             else
25             {
26                 MessageBox.Show("您未選擇數據庫!!!");
27             }              
28         }
29         #endregion     
View Code
在winform窗體中點擊表格單元格獲取表名,然后獲取該表中字段名稱信息
 1 #region 獲取每個新表中字段的信息雙擊事件
 2         /// <summary>
 3         /// 獲取每個新表中字段的信息雙擊事件
 4         /// </summary>
 5         /// <param name="sender"></param>
 6         /// <param name="e"></param>
 7         private void new_dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 8         {
 9             if (e.ColumnIndex == 1)
10             {
11                 try
12                 {
13                     using (SQLiteConnection conn = new SQLiteConnection(connectionString))
14                     {
15                         conn.Open();
16                         DataTable table = conn.GetSchema("TABLES");
17                         if (table != null && table.Rows.Count > 0)
18                         {
19                             string tableName = this.new_dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
20                             newTableName = tableName;
21                             DataTable schemaTable = GetReaderSchema(tableName, conn);
22                             newClickState = true;
23                             this.new_dataGridView1.DataSource = schemaTable;                       
24                         }
25                     }
26                 }
27                 catch (Exception msg)
28                 {
29                     throw msg;
30                 }               
31             }         
32         }
33         #endregion
34 #region 獲取相應數據庫中表的信息
35         /// <summary>
36         /// 獲取相應數據庫中表的信息
37         /// </summary>
38         /// <param name="tableName"></param>
39         /// <param name="connection"></param>
40         /// <returns></returns>
41         private DataTable GetReaderSchema(string tableName, SQLiteConnection connection)
42         {
43             DataTable schemaTable = null;
44             IDbCommand cmd = new SQLiteCommand();
45             cmd.CommandText = string.Format("select * from [{0}]", tableName);
46             cmd.Connection = connection;
47             using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
48             {        
49                 schemaTable = reader.GetSchemaTable();
50             }
51             return schemaTable;
52         }
53         #endregion
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM