记录一下WPF开发(二)--- access数据库DAL

直奔主题....

有时候桌面小程序用Access数据库是非常方便的,SQlHelper.cs如下

class SqlHelper
    {
        //获取数据库存放路径
        private static string path = Environment.CurrentDirectory + "/DB/Pump.mdb";
        private static String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Jet OLEDB:Database Password=sql;";

        /// <summary>
        /// 执行增(insert)、删(delete)、改(update)的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(String sql, params OleDbParameter[] parameters)
        {
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);

                    return cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// 一般用来执行有且只有一行一列查询结果的sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(String sql, params OleDbParameter[] parameters)
        {
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);

                    return cmd.ExecuteScalar();
                }
            }
        }

        /// <summary>
        /// 用来执行多行多列的查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataSet(String sql, params OleDbParameter[] parameters)
        {
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);

                    return dataset.Tables[0];
                }
            }
        }

        /// <summary>
        /// 将null类型转换成DBNull类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Object ToDBNull(Object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            else
            {
                return value;
            }
        }

        /// <summary>
        /// 将DBNull类型转换成null类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Object ToNull(Object value)
        {
            if (value == DBNull.Value)
            {
                return null;
            }
            else
            {
                return value;
            }
        }

        public static OleDbParameter SetParameter(String key, object value)
        {
            //return new OleDbParameter(key, ToDBNull(value));
            if (value is DateTime?)
            {
                OleDbParameter time = new OleDbParameter(key, OleDbType.Date);
                time.Value = ToDBNull(value);

                return time;
            }
            else
            {
                return new OleDbParameter(key, ToDBNull(value));
            }
        }
    }

DAL接口

 #region DAL 父类提供上层操作
    class BaseDAL
    {

        public void Delete(String tablename,  Int32? id)
        {
            String sql = @"delete from [" + tablename + "] where Id = @id" ;

         
               SqlHelper.ExecuteNonQuery(sql,SqlHelper.SetParameter("@id", id));
         
           
        }
        public virtual int Save(Object entity) { return 0; }

        public virtual int Update(Object entity) { return 0; }


        public virtual void SetValue(DataRow row, Object o) { }
        
    }
    #endregion

具体实现DAL继承该基类即可实现简单事物操作。



你可能感兴趣的:(Access,WPF,dal)