[WM] WM下访问SQLite(一种替代SQL CE的解决方案)

本文介绍了如何在Windows Mobile上使用.NET CF访问SQLite数据库,并提供了一个简单的SQLite操作帮助类。此外,还对比了SQLite与SQL CE的性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  这两天用了用SQLite,感觉比SQL CE好用多了,最主要的是性能好多了.这一点对手持设备来讲很重要.

  闲话少说,下来将怎么在WM用.NET CF访问SQLite.

  1. ADO.NET Provider For SQLite.

  ADO.NET 提供程序是香港(貌似)一个公司提供的.项目地址见:http://sourceforge.net/projects/sqlite-dotnet2

  2. 对SQLite.NET的封装,提供一个简单的操作帮助类.

ContractedBlock.gif ExpandedBlockStart.gif SQLiteHelper
ExpandedBlockStart.gifContractedBlock.gif/**//**
InBlock.gif * SQLite操作的帮助类.
InBlock.gif * 
InBlock.gif * Author:    egmkang.wang
InBlock.gif * Date:        2009-06-21
ExpandedBlockEnd.gif 
*/

None.gif
namespace System.Data.SQLite
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Data.SQLite;
InBlock.gif    
using System.IO;
InBlock.gif
InBlock.gif    
public class SqliteHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static string pwd = "PWD";
InBlock.gif        
private static string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\sqliteTest.db";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private static string connString = string.Format("Data Source =\"dot.gif{0}\"", path, pwd);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回数据库链接字符串
ExpandedSubBlockEnd.gif        
/// </summary> 

InBlock.gif        public static string ConnString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn connString; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行SQL语句,返回受影响的行数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">需要被执行的SQL语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>受影响的行数</returns> 

InBlock.gif        public static int ExecuteNonQuery(string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteNonQuery(ConnString, cmdText);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行带有事务的SQL语句
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="trans">事务</param>
InBlock.gif        
/// <param name="cmdText">SQL语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>受影响的行数</returns> 

InBlock.gif        public static int ExecuteNonQuery(SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int val = 0;
InBlock.gif
InBlock.gif            
using (SQLiteCommand cmd = new SQLiteCommand())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, (SQLiteConnection)trans.Connection, trans, cmdText, parameters);
InBlock.gif                val 
= cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行SQL语句,返回受影响的行数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串</param>
InBlock.gif        
/// <param name="cmdText">SQL语句</param>
InBlock.gif        
/// <param name="parameters">SQL的参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>受影响的行数</returns>

InBlock.gif        public static int ExecuteNonQuery(string connString, string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SQLiteConnection conn = new SQLiteConnection(connString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteNonQuery(conn, cmdText, parameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行SQL语句,返回受影响的行数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connection">数据库链接</param>
InBlock.gif        
/// <param name="cmdText">SQL语句</param>
InBlock.gif        
/// <param name="parameters">参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>受影响的行数</returns>

InBlock.gif        public static int ExecuteNonQuery(SQLiteConnection connection, string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int val = 0;
InBlock.gif
InBlock.gif            
using (SQLiteCommand cmd = new SQLiteCommand())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, connection, 
null, cmdText, parameters);
InBlock.gif                val 
= cmd.ExecuteNonQuery();
InBlock.gif                cmd.Parameters.Clear();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">SQL 语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>第一行的第一列的值</returns>

InBlock.gif        public static object ExecuteScalar(string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteScalar(ConnString, cmdText);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串</param>
InBlock.gif        
/// <param name="cmdText">SQL 语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>第一行的第一列的值</returns>

InBlock.gif        public static object ExecuteScalar(string connString, string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (SQLiteConnection conn = new SQLiteConnection(connString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteScalar(conn, cmdText);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connection">数据库链接</param>
InBlock.gif        
/// <param name="cmdText">SQL 语句</param>
ExpandedSubBlockEnd.gif        
/// <returns>第一行的第一列的值</returns>

InBlock.gif        public static object ExecuteScalar(SQLiteConnection connection, string cmdText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object val;
InBlock.gif
InBlock.gif            
using (SQLiteCommand cmd = new SQLiteCommand())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, connection, 
null, cmdText);
InBlock.gif                val 
= cmd.ExecuteScalar();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return val;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行SQL语句,返回结果集的DataReader
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmdText">SQL语句</param>
InBlock.gif        
/// <param name="parameters">参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>结果集的DataReader</returns>

InBlock.gif        public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ExecuteReader(ConnString, cmdText, parameters);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 执行SQL语句,返回结果集的DataReader
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connString">连接字符串</param>
InBlock.gif        
/// <param name="cmdText">SQL语句</param>
InBlock.gif        
/// <param name="parameters">参数</param>
ExpandedSubBlockEnd.gif        
/// <returns>结果集的DataReader</returns>

InBlock.gif        public static SQLiteDataReader ExecuteReader(string connString, string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SQLiteConnection conn 
= new SQLiteConnection(connString);
InBlock.gif            SQLiteCommand cmd 
= new SQLiteCommand();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdText, parameters);
InBlock.gif                SQLiteDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif                
return rdr;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                conn.Close();
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="cmd">Command对象</param>
InBlock.gif        
/// <param name="conn">Connection对象</param>
InBlock.gif        
/// <param name="trans">Transcation对象</param>
InBlock.gif        
/// <param name="cmdText">SQL Text</param>
ExpandedSubBlockEnd.gif        
/// <param name="parameters">参数实例</param>

InBlock.gif        private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (conn.State != ConnectionState.Open)
InBlock.gif                conn.Open();
InBlock.gif
InBlock.gif            cmd.Connection 
= conn;
InBlock.gif            cmd.CommandText 
= cmdText;
InBlock.gif
InBlock.gif            
if (trans != null)
InBlock.gif                cmd.Transaction 
= trans;
InBlock.gif            
if (null != parameters && parameters.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Parameters.AddRange(parameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

 

  3. 增删改查:

ContractedBlock.gif ExpandedBlockStart.gif Insert,Delete,Update
const string s_AddressTreeIntoSQLite = "Insert into [AddressTree] ([Id],[ItemType],[ParentId],[Name]) values (@Id,@ItemType,@ParentId,@Name);";

SqliteHelper.ExecuteNonQuery(tran, s_AddressTreeIntoSQLite,
//new SQLiteParameter[] here
);
ContractedBlock.gif ExpandedBlockStart.gif Select
const string s_AddresTreeFromSqlCE = "Select [Id],[ItemType],[ParentId],[Name] From [AddressTree];";

using (SqlCeDataReader rdr = SqlCeHelper.ExecuteReader(s_AddresTreeFromSqlCE ))
{
    
while (rdr.Read())
    {
        
//Read Data Here
    }
}

 

  4. 其他

  SQLite性能绝对强悍.四表连接查询,查询200次,SQL CE需要44秒(with index),SQLite只需要3-6秒(with index).
插入,删除更新性能参见http://www.cnblogs.com/egmkang/archive/2009/06/06/1497678.html

  PS:最近发现执行sql的时候,最好使用单一的长连接,而不是ConnectionString.原因很简单,嵌入式数据库没有连接池技术,

在进行数据库查询中的链接的打开关闭费用相对来说比较高昂.这一点在写程序的时候注意以下.

  还有,有关二进制资源,需要及时释放,例如SqlCommand,这些在写Web 程序的时候体验不是很大,毕竟那种环境拥有大量

的内存,GC的效率又是比较高.

 

Windows CE,WinCE,WM,SQLite,.NET

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值