如何利用C#代码获取SQLite数据库的元数据

Sqlite数据库,在很多场合已经用得比较多,由于我的代码生成工具的需要,需要把Sqlite的表、字段、视图等信息获取出来,以便实现各种数据库快速生成项目工程的操作。这里就需要利用C#获取Sqlite数据库的元数据了,和其他数据库一样。

为了获取Sqlite的数据库对象数据,我做了一个测试的例子来获取他的相关信息,其实它的元数据还是和Access的操作方式很接近。首先我们先通过Sqlite的数据库管理工具或者Visual Studio来打开创建一些表,如下所示。

首先我们先来看看通过C#代码获取到数据库对象的操作界面,如下所示。

获取表的元数据界面效果如下所示,视图和这个也查不多,很有意思的一点,就是它把创建的脚本的显示出来了,呵呵。

获取的表字段信息效果如下所示。

有了这些数据,我就很方便在我的代码生成工具Database2Sharp里面实现代码生成操作了。

现在我们来看看以上实现的后台代码是如何的,来了解Sqlite的数据库获取元数据的操作。

        string connectionString = "";

        public Form1()
        {
            InitializeComponent();

            connectionString = string.Format(@"Data Source={0}\OrderWater.db;Version=3;", Application.StartupPath);
        }

        private void btnGetSchema_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connectionString))
            {
                conn.Open();
                DataTable schemaTable = conn.GetSchema("TABLES");
                this.dataGridView1.DataSource = schemaTable;
            }
        }

获取表字段的操作代码如下所示。

        private void btnGetColumns_Click(object sender, EventArgs e)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connectionString))
            {
                conn.Open();
                DataTable table = conn.GetSchema("TABLES");
                if (table != null && table.Rows.Count > 0)
                {
                    string tableName = table.Rows[0]["TABLE_NAME"].ToString();

                    DataTable schemaTable = GetReaderSchema(tableName, conn);
                    this.dataGridView1.DataSource = schemaTable;
                }
            }
        }

        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;
        }

为了实现和我代码生成工具中的数据库字段信息绑定,需要通过获取设置Sqlite的属性为对应的ColumnInfo对象,如下所示。

            using (SQLiteConnection conn = new SQLiteConnection(ConnectString))
            {
                conn.Open();
                DataTable schemaTable = GetReaderSchema(tableName, conn);

                foreach (DataRow dr in schemaTable.Rows)
                {
                    ColumnInfo info = new ColumnInfo();
                    info.Name = new NameElement(dr["ColumnName"].ToString());
                    info.Ordinal = Convert.ToInt32(dr["ColumnOrdinal"].ToString());
                    info.AllowDBNull = (bool)dr["AllowDBNull"];
                    info.MaxLength = Convert.ToInt32(dr["ColumnSize"].ToString());
                    info.DataTypeId = Convert.ToInt32(dr["ProviderType"].ToString());
                    info.DataType = dr["DataTypeName"].ToString().Trim();
                    info.AutoIncrement = (bool)dr["IsAutoIncrement"];
                    info.IsPrimaryKey = (bool)dr["IsKey"];
                    info.Unique = (bool)dr["IsUnique"];
                    info.IsReadOnly = (bool)dr["IsReadOnly"];
                    string netType = dr["DataType"].ToString();

                    list.Add(info.Name.Name.ToString(), info);
                }

                conn.Close();
            }

代码生成工具中,这些数据库的元数据实体类信息是可以提供访问的,方便我们定制代码生成工具的模板,代码生成工具关于这些数据库对象的帮助如下所示。

 

这样,在代码生成工具中,就可以利用Sqlite的数据库对象数据,来生成和Oracle数据库、SqlServer数据库、Access数据库、MySql等数据库一样的项目工程代码了,获取甚至可以在自定义的模板代码模块中,添加自己的处理逻辑。

快速生成的代码如下所示。

本文通过介绍获取Sqlite的数据库元数据库,并在代码生成工具中的应用为例,给大家提供一个使用元数据进行程序开发的一个思路,希望大家可以同这种方式实现更多自定义模板或者逻辑的引用。

天下虽大同,一直在努力,孜孜不倦,终有所获。

posted on 2012-03-09 19:31  伍华聪  阅读(13646)  评论(2编辑  收藏  举报

导航