纸上得来终觉浅,绝知此事要躬行。

 

SQLite B/S使用(一)

SQLite属于一个轻量级的数据库,但是功能绝不逊于SQl Server 、Access等

1.去网上下载一个System.Data.SQLite.DLL在要用项目中添加引用,再打开命名空间using System.Data.SQLite;

下载地址:https://files.cnblogs.com/sjrhero/System.Data.SQLite.rar

2.下面就是下载一个SQLite工具我使用的SQLite工具是SQLiteManager,创建一个数据库扩展名为*.sqlite,再创建表跟SQL Server的Sql语句基本上是一样的(目前没有发现在不一样,还是第一次用)

3.使用创建的SQLite数据库(其实跟使用SQL Server数据库没有什么区别,第一次使用)以下是第一次使用的代码:

 

protected void Button1_Click(object sender, EventArgs e)
{
SQLiteConnection sqlcon
= new SQLiteConnection("Data Source=|DataDirectory|mydatabase.sqlite;Pooling=true;FailIfMissing=false");
sqlcon.Open();
SQLiteCommand cmd
= new SQLiteCommand("select * from testTB where userName = '用户名'", sqlcon);
SQLiteDataReader sdr
= cmd.ExecuteReader();
while (sdr.Read())
{
Response.Write(sdr[
0].ToString()+"<br />");
}
sdr.Close();
//以下是使用SQLiteHelper
DataTable dt = SQLiteHelper.ExecuteDataset(sqlcon, "select * from testTB where userName = '用户名'").Tables[0];
if (dt.Rows.Count>0)
{
foreach (DataRow item in dt.Rows)
{
Response.Write(item[
"userName"].ToString());
}
}
dt.Dispose();
sqlcon.Close();
}

上面的Data Source=|DataDirectory|mydatabase.sqlite这是使用的相对路径将它放在Web应用项目的App_Data目录,|DataDirectory|  就代表这个目录的位置,后面的就是文件名(两坚也是必须带上)。目前只在B/S模式下测试路径问题,绝对路径就不用带|DataDirectory|了。C/S模式下还没有验证 示例如下: 

connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|

AttachDbFilename=|DataDirectory|    这个直接定位App_Data文件夹。

下面是在web.config中配置的使用

 

<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 />
</configSections>
<dataConfiguration defaultDatabase="


"
>
<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>

 以下是别人初学的时候的示例:

 

private void button1_Click(object sender, EventArgs e)
{
//数据库路径
string datasource = "data.db";
//创建数据库
SQLiteConnection.CreateFile(datasource);
//创建连接,默认密码为admin
SQLiteConnection conn = new SQLiteConnection("Data Source=data.db;password=admin");
//建立连接
conn.Open();
//创建命令
SQLiteCommand cmd = new SQLiteCommand();
//设置连接
cmd.Connection = conn;
//设置命令字:创建表
cmd.CommandText = "CREATE TABLE GpsPoints(ID integer,B numeric,L numeric,H numeric)";
//执行sql语句,不需要返回值。
cmd.ExecuteNonQuery();
//读取id列最大值
int next_id = 0;
cmd.CommandText
= "select max(id) from GpsPoints";
//读取结果
SQLiteDataReader reader = cmd.ExecuteReader();
//如果没取到,空数据,则id设置为0
if (reader.IsDBNull(0)) next_id = 0;
//反之最大值+1
else next_id = int.Parse(reader[0].ToString()) + 1;
//关闭读取
reader.Close();
//创建命令,插入数据,这里例子没使用SQLiteParameter
cmd.CommandText = "insert into GpsPoints values(" + next_id.ToString() + ",123.456,123.456,123.456)";
cmd.ExecuteNonQuery();
cmd.CommandText
= "select max(id) from GpsPoints";
reader
= cmd.ExecuteReader();
MessageBox.Show(reader[
0].ToString());
conn.Close();
}

 

posted on 2010-11-26 16:56  JRoger  阅读(2624)  评论(0编辑  收藏  举报

导航