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

 

posted @ 2016-12-23 14:45  浮云的等待  阅读(7181)  评论(0编辑  收藏  举报