zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    第二章作业第2题--苏志华
    小学生四则运算应用软件(一)
    YOLO1至YOLOV3方法讲解
    C++ STL中的二分查找
    C++ 中的prioriy_queue 优先级队列 转
    C++ 中的容器(栈、堆、队列) 转
    从尾到头打印链表
    替换空格
    C++中vector<vector<int> >
    对称平方数
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/6214756.html
Copyright © 2011-2022 走看看