• 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
  • 相关阅读:
    Python入门_绘制多个五角形_turtle
    Selenium3+python自动化6-八种元素元素定位(Firebug和firepath)
    MongoDB入门(3)- MongoDB备份与恢复
    MongoDB入门(2)- MongoDB安装
    MongoDB入门(1)- MongoDB简介
    Elastic Search操作入门
    应用Xml.Linq读xml文件
    Struts2入门(1)-第一个Struts2程序
    Hibernate入门(4)- Hibernate数据操作
    Hibernate入门(3)- 持久对象的生命周期介绍
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/6214756.html
Copyright © 2020-2023  润新知