发一个SQLCE Helper类,(未验证)

 using System;

using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.Data;

namespace sqlce
{
    class SqlCeHelper : IDisposable
    {
        private SqlCeConnection connection;
        private SqlCeCommand command;
        private const string connectionString = "Data Source=/MyDatabase#1.sdf";

        #region Open/Close
        public void Open()
        {
            try
            {
                connection = new SqlCeConnection(connectionString);
                command = connection.CreateCommand();
                command.Connection = connection;
                command.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();
            command.Dispose();
        }
        #endregion

        #region Operatons
        public SqlCeDataReader ExecuteReader(string sql)
        {
            command.CommandText = sql;
            SqlCeDataReader reader = null;
            try
            {
                reader = command.ExecuteReader();
            }
            catch (DataException e)
            {
                Console.WriteLine(e.Message);
            }
            return reader;
        }

        public DataSet ExecuteDataSet(string sql)
        {
            command.CommandText = sql;
            SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
            DataSet ds = new DataSet(); ;

            try
            {
                adapter.Fill(ds);
            }
            catch (DataException e)
            {
                Console.WriteLine(e.Message);
            }
            return ds;
        }

        public int ExecuteNonQuery(string sql)
        {
            command.CommandText = sql;
            int result = -1;

            try
            {
                result = command.ExecuteNonQuery();
            }
            catch (DataException e)
            {
                Console.WriteLine(e.Message);

            }
            return result;
        }

        public object ExecuteScalar(string sql)
        {
            command.CommandText = sql;
            object o = null;
            try
            {
                o = command.ExecuteScalar();
            }
            catch (DataException e)
            {
                Console.WriteLine(e.Message);
            }
            return o;
        }
        #endregion

        #region Transaction
        public void BeginTransaction()
        {
            command.Transaction = connection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            command.Transaction.Commit();
        }

        public void RollbackTransaction()
        {
            command.Transaction.Rollback();
        }
        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值