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


#region 最新數據表信息顯示事件
        /// <summary>
        /// 最新數據表信息顯示事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showNewSqliteInfo_Click(object sender, EventArgs e)
        {
            if (newDB)
            {
                connectionString = string.Format(@"Data Source={0};Version=3;", ndb_Path);
                using (SQLiteConnection conn = new SQLiteConnection(connectionString))
                {
                    conn.Open();
                    DataTable schemaTable = conn.GetSchema("TABLES");
                    // 移除數據表中特定的列
                    schemaTable.Columns.Remove("TABLE_CATALOG");
                    // 設定特定列的序號
                    schemaTable.Columns["TABLE_NAME"].SetOrdinal(1);
                    this.new_dataGridView1.DataSource = schemaTable;
                    newClickState = false;
                }
            }
            else
            {
                MessageBox.Show("您未選擇數據庫!!!");
            }              
        }
        #endregion

 

在winform窗體中點擊表格單元格獲取表名,然后獲取該表中字段名稱信息

#region 獲取每個新表中字段的信息雙擊事件
        /// <summary>
        /// 獲取每個新表中字段的信息雙擊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void new_dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                try
                {
                    using (SQLiteConnection conn = new SQLiteConnection(connectionString))
                    {
                        conn.Open();
                        DataTable table = conn.GetSchema("TABLES");
                        if (table != null && table.Rows.Count > 0)
                        {
                            string tableName = this.new_dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                            newTableName = tableName;
                            DataTable schemaTable = GetReaderSchema(tableName, conn);
                            newClickState = true;
                            this.new_dataGridView1.DataSource = schemaTable;                       
                        }
                    }
                }
                catch (Exception msg)
                {
                    throw msg;
                }               
            }         
        }
        #endregion
#region 獲取相應數據庫中表的信息
        /// <summary>
        /// 獲取相應數據庫中表的信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="connection"></param>
        /// <returns></returns>
        private DataTable GetReaderSchema(string tableName, SQLiteConnection connection)
        {
            DataTable schemaTable = null;
            IDbCommand cmd = new SQLiteCommand();
            cmd.CommandText = string.Format("select * from [{0}]", tableName);
            cmd.Connection = connection;
            using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
            {        
                schemaTable = reader.GetSchemaTable();
            }
            return schemaTable;
        }
        #endregion

 


免責聲明!

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



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