• C# 获取SqLite数据库表信息以及获取表内字段信息


    已经封装好的函数,即可调用
     1        #region 最新数据表信息显示事件
     2         /// <summary>
     3         /// 最新数据表信息显示事件
     4         /// </summary>
     5         /// <param name="sender"></param>
     6         /// <param name="e"></param>
     7         private void showNewSqliteInfo_Click(object sender, EventArgs e)
     8         {
     9             if (newDB)
    10             {
    11                 connectionString = string.Format(@"Data Source={0};Version=3;", ndb_Path);
    12                 using (SQLiteConnection conn = new SQLiteConnection(connectionString))
    13                 {
    14                     conn.Open();
    15                     DataTable schemaTable = conn.GetSchema("TABLES");
    16                     // 移除数据表中特定的列
    17                     schemaTable.Columns.Remove("TABLE_CATALOG");
    18                     // 设定特定列的序号
    19                     schemaTable.Columns["TABLE_NAME"].SetOrdinal(1);
    20                     this.new_dataGridView1.DataSource = schemaTable;
    21                     newClickState = false;
    22                 }
    23             }
    24             else
    25             {
    26                 MessageBox.Show("您未选择数据库!!!");
    27             }              
    28         }
    29         #endregion     
    View Code
    在winform窗体中点击表格单元格获取表名,然后获取该表中字段名称信息
     1 #region 获取每个新表中字段的信息双击事件
     2         /// <summary>
     3         /// 获取每个新表中字段的信息双击事件
     4         /// </summary>
     5         /// <param name="sender"></param>
     6         /// <param name="e"></param>
     7         private void new_dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
     8         {
     9             if (e.ColumnIndex == 1)
    10             {
    11                 try
    12                 {
    13                     using (SQLiteConnection conn = new SQLiteConnection(connectionString))
    14                     {
    15                         conn.Open();
    16                         DataTable table = conn.GetSchema("TABLES");
    17                         if (table != null && table.Rows.Count > 0)
    18                         {
    19                             string tableName = this.new_dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    20                             newTableName = tableName;
    21                             DataTable schemaTable = GetReaderSchema(tableName, conn);
    22                             newClickState = true;
    23                             this.new_dataGridView1.DataSource = schemaTable;                       
    24                         }
    25                     }
    26                 }
    27                 catch (Exception msg)
    28                 {
    29                     throw msg;
    30                 }               
    31             }         
    32         }
    33         #endregion
    34 #region 获取相应数据库中表的信息
    35         /// <summary>
    36         /// 获取相应数据库中表的信息
    37         /// </summary>
    38         /// <param name="tableName"></param>
    39         /// <param name="connection"></param>
    40         /// <returns></returns>
    41         private DataTable GetReaderSchema(string tableName, SQLiteConnection connection)
    42         {
    43             DataTable schemaTable = null;
    44             IDbCommand cmd = new SQLiteCommand();
    45             cmd.CommandText = string.Format("select * from [{0}]", tableName);
    46             cmd.Connection = connection;
    47             using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly))
    48             {        
    49                 schemaTable = reader.GetSchemaTable();
    50             }
    51             return schemaTable;
    52         }
    53         #endregion
    View Code
  • 相关阅读:
    【BZOJ4318】OSU! 期望DP
    【BZOJ2956】模积和 分块
    【BZOJ4443】[Scoi2015]小凸玩矩阵 二分+二分图最大匹配
    【BZOJ2253】[2010 Beijing wc]纸箱堆叠 cdq分治
    【BZOJ3555】[Ctsc2014]企鹅QQ hash
    【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈
    【BZOJ2287】【POJ Challenge】消失之物 背包动规
    【BZOJ4517】[Sdoi2016]排列计数 组合数+错排
    【BZOJ4551】[Tjoi2016&Heoi2016]树 并查集
    【BZOJ2783】[JLOI2012]树 DFS+栈+队列
  • 原文地址:https://www.cnblogs.com/IamJiangXiaoKun/p/5822044.html
Copyright © 2020-2023  润新知