SQLite数据库访问编程及管理工具

工具

  • SQLite Administrator
  • SQLite Database Browser

Visual studio 2005下使用SQLite数据库

[@more@]

可视化的SQLite数据库管理工具

管理SQLite数据库除命令行外,网络上还有很多开源的可视化的SQLite数据库管理工具,登陆https://sourceforge.net/或者通过其他搜索引擎(GOOOGLE/BAIDU),输入【SQLite】可以找到一大片相关工具,比如,SQLite Database Browser、SQLite Administrator... 等等。试用了几个,比较而言,个人比较喜欢SQLite Administrator,该工具界面支持简体中文、界面比较简洁、数据库相关管理操作相对比较简便,该工具可以登陆http://sqliteadmin.orbmu2k.de/下载,以下是该工具的一个截图。

sqlite-admin.GIF

在Visual studio下使用SQLite数据库

使用SQLite.NET访问SQLite

下载地址:http://sourceforge.net/projects/sqlite-dotnet2/
源码:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki


最新版的ADO.NET 2.0 Provider for SQLite,下载解压缩后为一个安装包。点击安装后,可在安装目录下发现System.Data.SQLite.dll文件,同时在安装过程中该DLL文件也会自动注册到Visual studio 2005,可为开发环境引用(见图)。

sqlite-yinyong.GIF

在Visual studio 2005选择C#语言,建立新的项目SQLiteView,主界面参见下面的截图。该项目实现了对SQLite数据库的数据表的数据记录的浏览、新增、修改和删除操作功能。

sqlite-test.GIF

数据库访问类DataAccess的代码:

using System;
using System.Data;
using System.Data.SQLite;

namespace SQLiteView
{
class DataAccess
{
SQLiteConnection con ;
SQLiteCommand command;
public DataAccess()
{
con = new SQLiteConnection("Data Source=test.db3");//test.db3位于debug目录下
command = con.CreateCommand();
}
//读取数据
public DataTable ReadTable(string tableName)
{
command.CommandText = "SELECT * FROM " + tableName;
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataTable dt = new DataTable(tableName);
da.Fill(dt);
return dt;
}
//修改数据表
public bool UpdateTable(DataTable srcTable, string tableName)
{
bool isok = false;
try
{
command.CommandText = "SELECT * FROM " + tableName;
SQLiteDataAdapter oda = new SQLiteDataAdapter(command);
SQLiteCommandBuilder ocb = new SQLiteCommandBuilder(oda);
oda.InsertCommand = ocb.GetInsertCommand();
oda.DeleteCommand = ocb.GetDeleteCommand();
oda.UpdateCommand = ocb.GetUpdateCommand();
oda.Update(srcTable);
isok = true;
}
catch (Exception ex)
{}
return isok;
}
}
}

相关RIDU操作方法的代码如下:

//刷新数据源
private void RefreshTable()
{
this.dataGridView1.DataSource = dba.ReadTable("testone");
}
//更新数据源
private void UpdateTable(DataTable dt)
{
if (dt != null)
{
if (dba.UpdateTable(dt, "testone"))
{
RefreshTable();
MessageBox.Show("OK");
}
else
MessageBox.Show("Failed");
}
}
//浏览
private void button1_Click(object sender, EventArgs e)
{
RefreshTable();
}

//新增、修改
private void button2_Click(object sender, EventArgs e)
{
DataTable dt = this.dataGridView1.DataSource as DataTable;
UpdateTable(dt);
}

//删除
private void button3_Click(object sender, EventArgs e)
{
DataTable dt = this.dataGridView1.DataSource as DataTable;
DataRowView rowview = this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
if (rowview != null)
{
rowview.Row.Delete();
UpdateTable(dt);
}
}

通过试用 ,初步感觉SQLite数据库对SQL语言的支持也不错、有相关不俗实力的技术支持(开发团队、社区、论坛)、运行速度较快、普遍关注程度较高、够轻量级...,其中够轻量级是其最大的优点和亮点。

附源码下载:/Files/ysxlh/SQLiteView.rar

结合Enterprise Library操作SQLite

企业库是我们常用的框架之一,可以从http://entlib.codeplex.com/下载。安装之后有源代码和chm的文档。最新版本目前是V5.0。里面的很多思想更值得我们程序员去研究,例如:如何设计可扩展的组建?

  企业库中的数据访问组件更是我们常用的数据访问组件之一。组件默认支持SQL Server和Oracle的数据库访问,支持自定义的扩展。

  使用企业库操作SQLite数据库,需要用到企业库的一个扩展组件,Enterprise Library Contrib 。里面扩展了企业库的很多功能。其中对数据库的扩展包括了访问操作SQLite,让我们可以像在操作SQL SERVER那样,保持代码不用很大的修改,可以很容易的过渡到SQLite上。

  遗憾的是目前的这个entlib contrib的版本是V4.1,它只支持企业库的V4.1版本,也就是说它只能和V4.1版本的企业库的数据访问组件配合使用。否则会报错。

  在http://entlib.codeplex.com/上也可以下载到历史版本,也就是可以下载到V4.1。

  用法也可以参考:ASP.NET: Using SQLite with Enterprise Library 3.1

  首先在web.config或者是app.config中添加如下配置

ContractedBlock.gifExpandedBlockStart.gif代码
复制代码

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt<configuration><configSections><section name="dataConfiguration" type ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null />

"><providerMappings><add databaseType="EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SqLite, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
name="System.Data.SQLite" />providerMappings>dataConfiguration><connectionStrings><add name="sqlite" connectionString="Data Source=|DataDirectory|db;Pooling=true;FailIfMissing=false"
providerName="System.Data.SQLite" />connectionStrings>configuration>
复制代码

  上面的connectionstring配置节的db就是SQLite的数据库文件,将它放在Web应用的App_Data目录,|DataDirectory|就代表这个目录的位置,后面的就是文件名。

  剩下的就是我们使用企业库访问SQL Server是一样的了。

ADO.NET访问SQLite

原生态的访问,就是说直接用connection和command这些对象打开数据库,然后打开连接,进行数据的操作。

复制代码
ContractedBlock.gifExpandedBlockStart.gif代码

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt using (DbConnection conn = new SQLiteConnection( System.Configuration.ConfigurationManager.ConnectionStrings["sqlite" ].ConnectionString))
{
conn.Open();
DbCommand comm = conn.CreateCommand();
comm.CommandText = "select * from customer" ;
comm.CommandType = CommandType.Text;using (IDataReader reader = comm.ExecuteReader())
{while (reader.Read())
{
Response.Write(reader[0 ]);
}
}
}
复制代码

复制代码
ContractedBlock.gifExpandedBlockStart.gif代码

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt Database db=DatabaseFactory.CreateDatabase ("ConnectionString" );
DbCommand comm = db.GetStoredProcCommand("GetUserByID" );
IDataReader reader = null ;
db.AddInParameter(comm, "UserID", DbType.String, "12");using (reader = db.ExecuteReader(comm))
{

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/66009/viewspace-1059723/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/66009/viewspace-1059723/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值