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
  • 相关阅读:
    [笔记]Oracle遇到的问题及解决的办法
    [转载] linux三款好用网络监控软件(bwmng 、iftop、iptraf)
    IE无法打开internet站点已终止操作的解决办法 (转)
    怎样去掉桌面图标和字的蓝色阴影
    ASP.NET下载文件(转载)
    获取iframe内容IE下的延时 (转载)
    文件上传入数据库&从数据库中下载文件(转载)
    asp.net 4.0 A potentially dangerous Request.Form value was detected fr(转载)
    location.search
    IE6下的CSS BUG枚举 (转)
  • 原文地址:https://www.cnblogs.com/GmrBrian/p/6214756.html
Copyright © 2011-2022 走看看