![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
CYQ.Data 用SQL语句操作文本数据库原理及示例
Principles |
|
|
| #TopicOwner |
CYQ.Data V5版本的文本数据库,以前有过相关的介绍:周末一起用文本数据库玩玩Code First
数据的存储,是基于json格式或xml格式的,而实现的原理,也有一篇介绍: CYQ.Data V5文本数据库技术原理解密 前几天实现基本功能后,也写了一篇突发想支持SQL语句的冲突想法的来源: CYQ.Data 文本数据库解析SQL语法 对于文本数据库的原理:简单的说,就4步: 1:存储用json或xml。
2:读取时还原为MDataTable。 3:操作时操作MDataTable实现数据行的变化。 4:保存时重新写为json或xml。 PS:当然还有一个CodeFirst模式时自动产生用于记录表结构的"表名.ts"文件。
文本数据库的Where语句分拆: 原先的功能,有Select(where),这个where是支持sql语句的,简单说就是对where进行了以下分析:
string where="id>1 and name like '%a%' order by id desc"; 分析出where,order by ,再分析出id,name及对应的值,循环进行值判断,提取出合适的行。 从而达到查询的功能。
现在要支持SQL,看似复杂点又提升了点,因为简单的SQL语句大体如下: Insert into ErrorLogs(PageUrl , ErrorMessage) values('http://...','what'' is the ...')
Select count(*) from ErrorLogs... Select * from ErrorLogs where ... Select id,name from ErrorLogs ... Update ErrorLogs set id=1 and name='name is Mr'' Right' Delete From ErrorLogs where ...
对于这些字符串的处理,真是很考基本功: 比如:Insert into ErrorLogs(PageUrl , ErrorMessage) values('http://...','what'' is (the (name))...')
把这条语句,分拆出: 1:操作:Insert 2: 表名:ErrorLogs 3:PageUrl :"http://..." 4: ErrorMessage :"'what'' is (the (name))..."
PS:我觉的可以适当的拿来做面试题了。。。。
大体整体的实现,基本上就等同于解析上面的SQL语法,然后回归到MDataTable的原始操作上去,细节就不啰嗦重复了。
下面看一下实现后的效果图:
项目截图就省略了,页面后台的代码: protected void btnExec_Click(object sender, EventArgs e)
{ if (!string.IsNullOrEmpty(txtSql.Text)) { MProc proc = new MProc(txtSql.Text, txtConn.Text); switch (ddlExeType.SelectedValue) { case "0": labTip.Text = proc.ExeScalar<string>(); break; case "1": labTip.Text = proc.ExeNonQuery().ToString(); break; case "2": MDataTable dt = proc.ExeMDataTable(); rptList.DataSource = dt; rptList.DataBind(); break; } proc.Close(); } }
上面这段代码很简单,执行指定链接的SQL语句,然后显示相应的信息。
操作数据库,基本配置文件里指定日志的链接字符串是必不可少的内容: <add name="LogConn" connectionString="txt path={0}App_Data"/>
说明:把系统错误日志用文件数据库记录,CYQ.Data V5内置用了CodeFirst方式来实现异常的数据库执行日志错误: 表名为ErrorLogs
4个字段分别为: 1:ID,PageUrl,ErrorMessage,CreateTime。
下面我们看界面:由于我的项目里本身就有日志,我们查询一下:
OK,下面我们删除所有数据库,执行成功后删除了5行数据: 再添加一行数据: 最后更新这行1行数据: 最终再查询显示一次:
至此,整个基本的SQL语法解析和实现就告终了,至于什么GroupBy,多表,视图,函数之类的,就在文本数据库的能力之外了。 功能的实现自己用两个Table去For还是while去实现自己需要的逻辑。
下面再补充一下ErrogLogs这个日志说明: 默认的CodeFirst,简单说就是你指定哪种数据库类型,就会自动创建相应的表结构,然后把执行SQL的错误信息写到该日志里,方便查询分析和解决。
以前,我习惯把日志的链接写成: <add name="LogConn" connectionString="Conn"/> 其实就是指向Conn(CYQ.Data默认的主数据库配置顶),这样就把日志表放到和主数据库表同一个数据库。。 结果有时候,如果主数据库挂掉,异常日志就无法记录了,所以建议是存到其它数据库。 但有了文件数据库之后,用文本来存储日志,看似是个不错的选择,而且操作也相当方便。
CodeFirst时,如果有数据(第一条数据产生时,自动根据实体产生了表结构,如果是文本,在指定的目录下出来这个)
当然了,除了比较标准的,是自动生成的,如果你是自己手工创建,可以新建一个表名.ts表结构,内容:
ID,int;
PageUrl,string; ErrorMessage,string; CreateTime,datetime;
CYQ.Data V5里文本数据库,实用价值:
包括网站站点的广告节点显示,友情链接,一些需要经常或偶尔编辑的Key,Value值等项,存文本数据库是最方便不过了。
有效的把非主站的附加数据,存储到文本数据库,利用文本数据完全内存表机制,高效,节能,又方便。
CYQ.Data V5在文本数据库、Access、SQLite、XHtmlAction等功能,都是不需要授权可以直接使用的。
![]() |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.