已经封装好的函数,即可调用
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
在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