sql ce 数据库访问通用组件.
/**
此组件,是 基于,wince 的 sql ce 数据库的通用的访问组件,
数据库访问的基本功能,如下
/*
*Copyright (c)2007, *All Rights reserved.
*文件名称:SqlCEDatabaseDll.dll
*文件标识:
*摘要:
*
*当前版本:1.0
*作者:蔡海洋
*完成日期:2007.5.31
*功能:SqlCE数据库的基本操作
*开发工具:vs2005
* 运行平台 : Pocket PC 2003 ARMI CPU
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;
namespace ZHXK.WinCE50
{
public class SqlCEDatabaseDll : IDisposable
{
// connection to data source
private SqlCeConnection sqlcecon;
private string connectionString = "";
/// <summary>
///
/// </summary>
public string ConStr
{
set { this.connectionString = value; }
get { return connectionString; }
}
public SqlCEDatabaseDll(string cons)
{
this.ConStr = cons;
}
/// <summary>
/// [创建人] 蔡海洋
/// [功能说明] 执行sql语句 返回一个int 它表明操作影响数据库的行数
/// 它所适合的数据库的操作有 insert into table () values ()
/// delete * from table where ……
/// [使用说明] int ds=new SqlCEDatabaseDll("str").RunSQLCommandReturnInt("select * from table")
/// </summary>
/// <param name="ss"></param>
/// <returns>
/// DataSet
/// </returns>
public int RunSQLCommandReturnInt(string ss)
{
int i = -1;
Open();
SqlCeCommand sqla = new SqlCeCommand(ss, sqlcecon);
try
{
i = sqla.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
this.Close();
}
return i;
}
/// <summary>
/// [创建人] 蔡海洋
/// [功能说明] 执行sql语句 返回一个SqlDataReader 它表明操作影响数据库的行数
/// 它所适合的数据库的操作有 insert into table () values ()
/// delete * from table where ……
/// [使用说明] SqlDataReader ds=new ZHXKDatabase("str").RunSQLCommandReturnDataReader("select * from table")
/// </summary>
/// <param name="ss"></param>
/// <returns>
/// DataSet
/// </returns>
public void RunSQLCommandReturnDataReader(string ss, out SqlCeDataReader dataReader)
{
Open();
SqlCeCommand sqla = new SqlCeCommand(ss, sqlcecon);
try
{
dataReader = sqla.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
throw new Exception("sql语句执行错误,检查sql语句是否合法!");
}
finally
{
}
}
/// <summary>
/// [创建人] 蔡海洋
/// [功能说明] 执行sql语句 返回一个DataSet的对象
/// [使用说明] DataSet ds=new ZHXKDatabase("str").RunSQLCommand("select * from table")
/// </summary>
/// <param name="ss"></param>
/// <returns>
/// DataSet
/// </returns>
public DataSet RunSQLCommand(string ss,string tableName)
{
Open();
SqlCeDataAdapter sqla = new SqlCeDataAdapter(ss, sqlcecon);
DataSet ds = new DataSet();
sqla.Fill(ds,tableName);
return ds;
}
/// <summary>
/// 打开连接
/// </summary>
private void Open()
{
// open connection
if (sqlcecon == null)
{
sqlcecon = new SqlCeConnection(this.ConStr);
sqlcecon.Open();
}
else if (sqlcecon.State == ConnectionState.Closed)
{
sqlcecon.Open();
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
if (sqlcecon != null)
sqlcecon.Close();
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
// make sure connection is closed
if (sqlcecon != null)
{
sqlcecon.Dispose();
sqlcecon = null;
}
}
}
}