如何利用C#代码获取SQLite数据库的元数据
2012-03-09 19:31
495 查看
Sqlite数据库,在很多场合已经用得比较多,由于我的代码生成工具的需要,需要把Sqlite的表、字段、视图等信息获取出来,以便实现各种数据库快速生成项目工程的操作。这里就需要利用C#获取Sqlite数据库的元数据了,和其他数据库一样。
为了获取Sqlite的数据库对象数据,我做了一个测试的例子来获取他的相关信息,其实它的元数据还是和Access的操作方式很接近。首先我们先通过Sqlite的数据库管理工具或者Visual Studio来打开创建一些表,如下所示。
首先我们先来看看通过C#代码获取到数据库对象的操作界面,如下所示。
获取表的元数据界面效果如下所示,视图和这个也查不多,很有意思的一点,就是它把创建的脚本的显示出来了,呵呵。
获取的表字段信息效果如下所示。
有了这些数据,我就很方便在我的代码生成工具Database2Sharp里面实现代码生成操作了。
现在我们来看看以上实现的后台代码是如何的,来了解Sqlite的数据库获取元数据的操作。
获取表字段的操作代码如下所示。
为了实现和我代码生成工具中的数据库字段信息绑定,需要通过获取设置Sqlite的属性为对应的ColumnInfo对象,如下所示。
代码生成工具中,这些数据库的元数据实体类信息是可以提供访问的,方便我们定制代码生成工具的模板,代码生成工具关于这些数据库对象的帮助如下所示。
这样,在代码生成工具中,就可以利用Sqlite的数据库对象数据,来生成和Oracle数据库、SqlServer数据库、Access数据库、MySql等数据库一样的项目工程代码了,获取甚至可以在自定义的模板代码模块中,添加自己的处理逻辑。
快速生成的代码如下所示。
本文通过介绍获取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的数据库元数据库,并在代码生成工具中的应用为例,给大家提供一个使用元数据进行程序开发的一个思路,希望大家可以同这种方式实现更多自定义模板或者逻辑的引用。
天下虽大同,一直在努力,孜孜不倦,终有所获。
相关文章推荐
- 如何利用C#代码获取SQLite的元数据
- 如何利用C#代码获取SQLite的元数据
- 如何利用C#代码来进行操作AD
- C# 如何获取本地数据库SQL Server的数据库服务器名称和数据库名称?然后连接,代码怎么写?
- 【转】 如何利用C#代码来进行操作AD
- 牛腩购物9 用户表设计/动软生成器/金钱字段decimal(18, 2)/ 注册的时候的前台js判断/后台代码判断/正则表达式软件/RegexBuddy/设置数据库字段的唯一性约束/如何获取控件在前台html的id值/如何将C#的后台正则换成js的正则
- 利用svcutil.exe 访问windows Server 2003的wcf服务生成客户端代码报错:无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法
- 如何获取用C#代码截取<td>中间的值</td>并且他是随机产生的
- 如何获取C#中方法的执行时间以及其代码注入详解
- 利用svcutil.exe 访问windows Server 2003的wcf服务生成客户端代码报错:无法从http://XXX/XXX.svc?wsdl获取元数据”错误的解决方法
- 干货!如何利用Photoshop CC进行自动切图和获取CSS代码
- C# 下面的代码示例演示如何结束异步操作以获取请求的流,然后开始一个请求以获取响应。....
- C#里面如何利用流获取一个记事本内容的行数
- C#利用反射在debug状态下获取调用代码所在文件及行信息
- 如何用C#代码获取某网页的内容(数据),
- 如何利用代码从网站上获取一个文件的大小,并且下载时不出现保存提示框
- 如何利用C#代码来进行操作AD
- 如何用C#动态编译、执行代码
- (转)javascript如何调用C#后代码中的过程 和ASP.NET调用JS乱码解决方案
- C#中如何动态运行代码