using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlServerCe; using System.Data; namespace SqlCeTest { public class SqlCeHelper : IDisposable { private SqlCeConnection connection; private SqlCeCommand sqlCeCommand; private string connectionString; public SqlCeCommand command { get { return sqlCeCommand; } } public SqlCeHelper() { } public SqlCeHelper(string AconnectionString) { connectionString = AconnectionString; } #region Open/Close public void Open() { try { connection = new SqlCeConnection(connectionString); sqlCeCommand = connection.CreateCommand(); sqlCeCommand.Connection = connection; sqlCeCommand.CommandType = CommandType.Text; connection.Open(); } catch (DataException e) { Console.WriteLine(e.Message); } } public void Close() { connection.Close(); connection.Dispose(); } public void Dispose() { connection.Close(); connection.Dispose(); sqlCeCommand.Dispose(); } #endregion #region Operatons public SqlCeDataReader ExecuteReader(string ASql) { sqlCeCommand.CommandText = ASql; SqlCeDataReader reader = null; try { reader = sqlCeCommand.ExecuteReader(); } catch (DataException e) { Console.WriteLine(e.Message); } return reader; } public DataSet ExecuteDataSet(string ASql) { sqlCeCommand.CommandText = ASql; SqlCeDataAdapter adapter = new SqlCeDataAdapter(sqlCeCommand); DataSet ds = new DataSet(); ; try { adapter.Fill(ds); } catch (DataException e) { Console.WriteLine(e.Message); } return ds; } public DataTable ExecuteDataTable(string ASql) { sqlCeCommand.CommandText = ASql; SqlCeDataAdapter adapter = new SqlCeDataAdapter(sqlCeCommand); DataTable dt = new DataTable(); ; try { adapter.Fill(dt); } catch (DataException e) { Console.WriteLine(e.Message); } return dt; } public int ExecuteNonQuery(string ASql) { sqlCeCommand.CommandText = ASql; int result = -1; try { result = sqlCeCommand.ExecuteNonQuery(); } catch (DataException e) { Console.WriteLine(e.Message); } return result; } public object ExecuteScalar(string ASql) { sqlCeCommand.CommandText = ASql; object o = null; try { o = sqlCeCommand.ExecuteScalar(); } catch (DataException e) { Console.WriteLine(e.Message); } return o; } #endregion #region Transaction public void BeginTransaction() { sqlCeCommand.Transaction = connection.BeginTransaction(); } public void CommitTransaction() { sqlCeCommand.Transaction.Commit(); } public void RollbackTransaction() { sqlCeCommand.Transaction.Rollback(); } #endregion public IDataParameter CreateParamter(string AParamName, string AType) { SqlDbType dbType; if (!TryPraseDbType(AType, out dbType)) dbType = SqlDbType.NVarChar; return new System.Data.SqlServerCe.SqlCeParameter(AParamName, dbType); } private bool TryPraseDbType(string type, out SqlDbType dbType) { //return Enum.TryParse<SqlDbType>(type, true,out dbType); bool flag = true; switch (type) { case "bit": dbType = SqlDbType.Bit; break; case "smallint": dbType = SqlDbType.SmallInt; break; case "integer": case "int": dbType = SqlDbType.Int; break; case "tinyint": dbType = SqlDbType.TinyInt; break; case "float": dbType = SqlDbType.Float; break; case "numeric": case "decimal": case "real": dbType = SqlDbType.Real; break; case "date": case "datetime": dbType = SqlDbType.DateTime; break; case "char": case "nchar": dbType = SqlDbType.NChar; break; case "varchar": case "varchar2": case "nvarchar": dbType = SqlDbType.NVarChar; break; case "text": case "ntext": dbType = SqlDbType.NText; break; case "binary": dbType = SqlDbType.Binary; break; case "varbinary": dbType = SqlDbType.VarBinary; break; case "image": dbType = SqlDbType.Image; break; default: dbType = SqlDbType.NVarChar; flag = false; break; } return flag; } } }