直接看代码吧
Code
//===============================================================================
// This file Create by kkun 2008-12-23
// For more information please go to
// http://www.tstring.com.cn/
//===============================================================================
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
namespace Tstring.DataBase
{
#region DataBaseOperatorII
///
/// Sqlite数据库操作类
///
public class DataBaseOperatorII
{
private string connectionString;
public DataBaseOperatorII()
{
connectionString = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
}
public DataBaseOperatorII( string _connectionstring )
{
connectionString = _connectionstring;
}
#region ExecuteNonQuery
public int ExecuteNonQuery( string cmdText )
{
return SqliteHelper.ExecuteNonQuery( connectionString, CommandType.Text, cmdText, null );
}
public int ExecuteNonQuery( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteNonQuery( connectionString, cmdType, cmdText, null );
}
public int ExecuteNonQuery( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteNonQuery( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteReader
public SQLiteDataReader ExecuteReader( string cmdText )
{
return SqliteHelper.ExecuteReader( connectionString, CommandType.Text, cmdText, null );
}
public SQLiteDataReader ExecuteReader( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteReader( connectionString, cmdType, cmdText, null );
}
public SQLiteDataReader ExecuteReader( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteReader( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteDataSet
public DataSet ExecuteDataSet( string cmdText )
{
return SqliteHelper.ExecuteDataSet( connectionString, CommandType.Text, cmdText, null );
}
public DataSet ExecuteDataSet( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteDataSet( connectionString, cmdType, cmdText, null );
}
public DataSet ExecuteDataSet( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteDataSet( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteScalar
public object ExecuteScalar( string cmdText )
{
return SqliteHelper.ExecuteScalar( connectionString, CommandType.Text, cmdText, null );
}
public object ExecuteScalar( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteScalar( connectionString, cmdType, cmdText, null );
}
public object ExecuteScalar( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteScalar( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
}
#endregion
#region SqliteHelper
///
/// The SqliteHelper class is intended to encapsulate high performance,
/// scalable best practices for common uses of SqlClient.
///
public abstract class SqliteHelper
{
///
/// Database connection strings
///
//public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
//public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
//public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
//public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized( new Hashtable() );
///
/// Execute a SQLiteCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// an int representing the number of rows affected by the command
public static int ExecuteNonQuery( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
using( SQLiteConnection conn = new SQLiteConnection( connectionString ) )
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
///
/// Execute a SQLiteCommand (that returns no resultset) against an existing database connection
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing database connection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// an int representing the number of rows affected by the command
public static int ExecuteNonQuery( SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
/// Execute a SQLiteCommand (that returns no resultset) using an existing SQL Transaction
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing sql transaction
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// an int representing the number of rows affected by the command
public static int ExecuteNonQuery( SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, trans.Connection, trans, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
/// Execute a SQLiteCommand that returns a resultset against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// SQLiteDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// A SQLiteDataReader containing the results
public static SQLiteDataReader ExecuteReader( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection( connectionString );
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
SQLiteDataReader rdr = cmd.ExecuteReader( CommandBehavior.CloseConnection );
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
///
/// 生成DataSet
///
///
///
///
///
///
public static DataSet ExecuteDataSet( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection( connectionString );
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
DataSet ds = new DataSet();
SQLiteDataAdapter adapter = new SQLiteDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill( ds );
cmd.Parameters.Clear();
return ds;
}
catch
{
conn.Close();
throw;
}
}
///
/// Execute a SQLiteCommand that returns the first column of the first record against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// An object that should be converted to the expected type using Convert.To{Type}
public static object ExecuteScalar( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
using( SQLiteConnection connection = new SQLiteConnection( connectionString ) )
{
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
///
/// Execute a SQLiteCommand that returns the first column of the first record against an existing database connection
/// using the provided parameters.
///
///
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing database connection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
/// An object that should be converted to the expected type using Convert.To{Type}
public static object ExecuteScalar( SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
///
/// add parameter array to the cache
///
/// Key to the parameter cache
/// an array of SqlParamters to be cached
public static void CacheParameters( string cacheKey, params SQLiteParameter[] commandParameters )
{
parmCache[cacheKey] = commandParameters;
}
///
/// Retrieve cached parameters
///
/// key used to lookup parameters
/// Cached SqlParamters array
public static SQLiteParameter[] GetCachedParameters( string cacheKey )
{
SQLiteParameter[] cachedParms = (SQLiteParameter[])parmCache[cacheKey];
if( cachedParms == null )
return null;
SQLiteParameter[] clonedParms = new SQLiteParameter[cachedParms.Length];
for( int i = 0, j = cachedParms.Length; i < j; i++ )
clonedParms[i] = (SQLiteParameter)( (ICloneable)cachedParms[i] ).Clone();
return clonedParms;
}
///
/// Prepare a command for execution
///
/// SQLiteCommand object
/// SQLiteConnection object
/// SQLiteTransaction object
/// Cmd type e.g. stored procedure or text
/// Command text, e.g. Select * from Products
/// SQLiteParameters to use in the command
private static void PrepareCommand( SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms )
{
if( conn.State != ConnectionState.Open )
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if( trans != null )
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if( cmdParms != null )
{
foreach( SQLiteParameter parm in cmdParms )
cmd.Parameters.Add( parm );
}
}
}
#endregion
}
//===============================================================================
// This file Create by kkun 2008-12-23
// For more information please go to
// http://www.tstring.com.cn/
//===============================================================================
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
namespace Tstring.DataBase
{
#region DataBaseOperatorII
///
/// Sqlite数据库操作类
///
public class DataBaseOperatorII
{
private string connectionString;
public DataBaseOperatorII()
{
connectionString = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
}
public DataBaseOperatorII( string _connectionstring )
{
connectionString = _connectionstring;
}
#region ExecuteNonQuery
public int ExecuteNonQuery( string cmdText )
{
return SqliteHelper.ExecuteNonQuery( connectionString, CommandType.Text, cmdText, null );
}
public int ExecuteNonQuery( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteNonQuery( connectionString, cmdType, cmdText, null );
}
public int ExecuteNonQuery( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteNonQuery( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteReader
public SQLiteDataReader ExecuteReader( string cmdText )
{
return SqliteHelper.ExecuteReader( connectionString, CommandType.Text, cmdText, null );
}
public SQLiteDataReader ExecuteReader( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteReader( connectionString, cmdType, cmdText, null );
}
public SQLiteDataReader ExecuteReader( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteReader( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteDataSet
public DataSet ExecuteDataSet( string cmdText )
{
return SqliteHelper.ExecuteDataSet( connectionString, CommandType.Text, cmdText, null );
}
public DataSet ExecuteDataSet( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteDataSet( connectionString, cmdType, cmdText, null );
}
public DataSet ExecuteDataSet( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteDataSet( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
#region ExecuteScalar
public object ExecuteScalar( string cmdText )
{
return SqliteHelper.ExecuteScalar( connectionString, CommandType.Text, cmdText, null );
}
public object ExecuteScalar( CommandType cmdType, string cmdText )
{
return SqliteHelper.ExecuteScalar( connectionString, cmdType, cmdText, null );
}
public object ExecuteScalar( CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
return SqliteHelper.ExecuteScalar( connectionString, cmdType, cmdText, commandParameters );
}
#endregion
}
#endregion
#region SqliteHelper
///
/// The SqliteHelper class is intended to encapsulate high performance,
/// scalable best practices for common uses of SqlClient.
///
public abstract class SqliteHelper
{
///
/// Database connection strings
///
//public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
//public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
//public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
//public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized( new Hashtable() );
///
/// Execute a SQLiteCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static int ExecuteNonQuery( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
using( SQLiteConnection conn = new SQLiteConnection( connectionString ) )
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
///
/// Execute a SQLiteCommand (that returns no resultset) against an existing database connection
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing database connection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static int ExecuteNonQuery( SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
/// Execute a SQLiteCommand (that returns no resultset) using an existing SQL Transaction
/// using the provided parameters.
///
///
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing sql transaction
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static int ExecuteNonQuery( SQLiteTransaction trans, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, trans.Connection, trans, cmdType, cmdText, commandParameters );
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///
/// Execute a SQLiteCommand that returns a resultset against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// SQLiteDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static SQLiteDataReader ExecuteReader( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection( connectionString );
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
SQLiteDataReader rdr = cmd.ExecuteReader( CommandBehavior.CloseConnection );
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
///
/// 生成DataSet
///
///
///
///
///
///
public static DataSet ExecuteDataSet( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
SQLiteConnection conn = new SQLiteConnection( connectionString );
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try
{
PrepareCommand( cmd, conn, null, cmdType, cmdText, commandParameters );
DataSet ds = new DataSet();
SQLiteDataAdapter adapter = new SQLiteDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill( ds );
cmd.Parameters.Clear();
return ds;
}
catch
{
conn.Close();
throw;
}
}
///
/// Execute a SQLiteCommand that returns the first column of the first record against the database specified in the connection string
/// using the provided parameters.
///
///
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// a valid connection string for a SQLiteConnection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static object ExecuteScalar( string connectionString, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
using( SQLiteConnection connection = new SQLiteConnection( connectionString ) )
{
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
///
/// Execute a SQLiteCommand that returns the first column of the first record against an existing database connection
/// using the provided parameters.
///
///
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SQLiteParameter("@prodid", 24));
///
/// an existing database connection
/// the CommandType (stored procedure, text, etc.)
/// the stored procedure name or T-SQL command
/// an array of SqlParamters used to execute the command
///
public static object ExecuteScalar( SQLiteConnection connection, CommandType cmdType, string cmdText, params SQLiteParameter[] commandParameters )
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand( cmd, connection, null, cmdType, cmdText, commandParameters );
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
///
/// add parameter array to the cache
///
/// Key to the parameter cache
/// an array of SqlParamters to be cached
public static void CacheParameters( string cacheKey, params SQLiteParameter[] commandParameters )
{
parmCache[cacheKey] = commandParameters;
}
///
/// Retrieve cached parameters
///
/// key used to lookup parameters
///
public static SQLiteParameter[] GetCachedParameters( string cacheKey )
{
SQLiteParameter[] cachedParms = (SQLiteParameter[])parmCache[cacheKey];
if( cachedParms == null )
return null;
SQLiteParameter[] clonedParms = new SQLiteParameter[cachedParms.Length];
for( int i = 0, j = cachedParms.Length; i < j; i++ )
clonedParms[i] = (SQLiteParameter)( (ICloneable)cachedParms[i] ).Clone();
return clonedParms;
}
///
/// Prepare a command for execution
///
/// SQLiteCommand object
/// SQLiteConnection object
/// SQLiteTransaction object
/// Cmd type e.g. stored procedure or text
/// Command text, e.g. Select * from Products
/// SQLiteParameters to use in the command
private static void PrepareCommand( SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, CommandType cmdType, string cmdText, SQLiteParameter[] cmdParms )
{
if( conn.State != ConnectionState.Open )
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if( trans != null )
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if( cmdParms != null )
{
foreach( SQLiteParameter parm in cmdParms )
cmd.Parameters.Add( parm );
}
}
}
#endregion
}
sqlite的DLL for .NET
http://files.cnblogs.com/kkun/System.Data.SQLite.rar
记录学习中的点点滴滴,一次书写,终生享用,人人为我,我为人人