欢迎投稿

今日深度:

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

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


1.   查询

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

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

select name fromsqlite_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);
        }
    }
}

www.htsjk.Com true http://www.htsjk.com/SQLite/36482.html NewsArticle C#获取SQLite数据库表名和字段名, 1.    查询 表 查询sqlite中所有表,可用如下sql语句。原理是,sqlite中有一个内建表sqlite_master,这个表中存储这所有自建表的表名称等信息。 通过以下...
相关文章
    暂无相关文章
评论暂时关闭