C#获取SQLite数据库表名和字段名

本文介绍了一种使用C#和SQLite来查询数据库中所有表名及各表所有字段的方法。通过执行特定SQL语句和PRAGMA命令,可以方便地获取数据库结构信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.   查询表

查询sqlite中所有表,可用如下sql语句。原理是,sqlite中有一个内建表sqlite_master,这个表中存储这所有自建表的表名称等信息。

通过以下语句可查询出某个数据库的所有表名称信息

select name from sqlite_master where type='table' order by name;

2.   查询与判断列

通过以下语句可查询出某个表的所有字段信息

PRAGMA  table_info([tablename])

比如:我想查看表StudentInfo的所有列信息,可以用下述代码。

PRAGMA table_info(StudentInfo)

图一:

图二:

实现的代码如下:

using System;
using System.Text;
using System.Windows.Forms;

// 引入命名空间
using System.Data.SQLite;

namespace GetTableNameAndFieldName
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        // 获取
        private void btnGet_Click(object sender, EventArgs e)
        {
            // SQLite连接字符串
            string connectionString = @"Data Source='" + @"G:\CdcKvsSys\Data\kvs-1.db" + "';Version=3;";
            // 获取指定数据库中的所有表名
            StringBuilder tableNames = new StringBuilder();
            using (SQLiteConnection conn = new SQLiteConnection(connectionString))
            {
                conn.Open();
                // 获取数据库中的所有表名
                string sqlTableNames = "select name from sqlite_master where type='table' order by name;";
                // 创建命令对象
                SQLiteCommand cmd = new SQLiteCommand(sqlTableNames, conn);
                using (SQLiteDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        // 表名
                        tableNames.Append(dr["Name"] + ",");
                    }
                }
            }
            MessageBox.Show("所有表名:" + tableNames, "小赖温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // 获取指定表名的所有字段名
            StringBuilder fieldNames = new StringBuilder();
            foreach (var tableName in Convert.ToString(tableNames).Split(','))
            {
                using (SQLiteConnection conn = new SQLiteConnection(connectionString))
                {
                    conn.Open();
                    if (!string.IsNullOrEmpty(tableName))
                    {
                        // 获取表中的所有字段名
                        string sqlfieldName = "Pragma Table_Info(" + tableName + ")";
                        // 创建命令对象
                        SQLiteCommand cmd = new SQLiteCommand(sqlfieldName, conn);
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                // 字段名
                                fieldNames.Append(dr["Name"] + ",");
                            }
                        }
                    }
                }
            }
            MessageBox.Show("所有字段名:" + fieldNames, "小赖温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值