多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國(guó)內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁(yè) > 數(shù)據(jù)庫(kù) > 數(shù)據(jù)庫(kù)應(yīng)用 > 使用oledb對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查及批量插入操作

使用oledb對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查及批量插入操作

來(lái)源:程序員人生   發(fā)布時(shí)間:2015-06-30 08:32:59 閱讀次數(shù):6473次

使用oledb操作數(shù)據(jù)庫(kù)工具類,可以使用泛型統(tǒng)1操作


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;


namespace CommonUtil
{
    public class DataBaseUtil
    {

      //傳遞數(shù)據(jù)庫(kù)文件路徑,這里使用的是access2007數(shù)據(jù)庫(kù)
        public DataBaseUtil(string path)
        {
            Path = path;
            ConnStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False; ",
                path);
            conn = new OleDbConnection(ConnStr);
        }
        public string Path;
        public static  string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E: umdb.accdb;Persist Security Info=False; ";

        private OleDbConnection conn= new OleDbConnection(ConnStr);//創(chuàng)建1個(gè)connection對(duì)象


        //使用泛型,獲得所有的實(shí)體類對(duì)象,返回list
        public List<T> ReieveList<T>() where T : class,new()
        {
            try
            {
                var className = (typeof(T)).Name;
                var sql = string.Format("SELECT *  FROM {0}", className);
                OleDbDataAdapter da = new OleDbDataAdapter(sql, ConnStr);
                DataSet ds = new DataSet();
                da.Fill(ds);
                var dt = ds.Tables[0];

                var list = ConverterUtil.ConvertDataTableToList<T>(dt);

                return list;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return null;
            }

        }

        //同上,根據(jù)條件,查詢,返回list實(shí)體類列表

        public List<T> ReieveList<T>(string where) where T : class,new()
        {
            try
            {
                var className = (typeof(T)).Name;
                var sql = string.Format("SELECT *  FROM {0} {1}", className,where);
                OleDbDataAdapter da = new OleDbDataAdapter(sql, ConnStr);

                DataSet ds = new DataSet();
                da.Fill(ds);
             
                var dt = ds.Tables[0];

                var list = ConverterUtil.ConvertDataTableToList<T>(dt);

                return list;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return null;
            }

        }

        //插入1條數(shù)據(jù)
        public bool Insert<T>(T entity) where T : class,new()
        {
          
            try
            {
                var type = typeof (T);
                var className = type.Name;

                var fields = "";
                var values = "";

                foreach (var property in type.GetProperties())
                {
                    if (property.Name.Equals("ID")) continue;
                    fields += "," + property.Name;
                    var isNumStr = (property.PropertyType == typeof (double) ||
                                    property.PropertyType == typeof (int))
                        ? ""
                        : "'";
                    values += "," + isNumStr + property.GetValue(entity, null) + isNumStr;
                }
                fields = fields.Substring(1);
                values = values.Substring(1);
                var sql = string.Format("insert into {0}({1}) values ({2}) ", className,
                    fields, values);

                OleDbDataAdapter da = new OleDbDataAdapter();

               
                da.InsertCommand = new OleDbCommand(sql, conn);
                da.InsertCommand.CommandText = sql;

                conn.Open();
                da.InsertCommand.ExecuteNonQuery();
                conn.Close();
              
                return true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }

        }


        //更新實(shí)體類

        public bool Update<T>(T entity) where T : class,new()
        {

            try
            {
                var type = typeof(T);
                var className = type.Name;

                var values = "";

                var id = "";
                foreach (var property in type.GetProperties())
                {
                    if (property.Name.Equals("ID"))
                    {
                        id = " where ID="+  property.GetValue(entity, null).ToString();
                        continue;
                    }
                    var isNumStr = (property.PropertyType == typeof(double) ||
                                    property.PropertyType == typeof(int))
                        ? ""
                        : "'";
                    values += "," +property.Name +"="+ isNumStr + property.GetValue(entity, null) + isNumStr;
                }
                values = values.Substring(1);
                var sql = string.Format("update {0} set {1} {2}", className,
                     values,id);

                OleDbDataAdapter da = new OleDbDataAdapter();

                da.UpdateCommand = new OleDbCommand(sql, conn);
                da.UpdateCommand.CommandText = sql;

                conn.Open();
                da.UpdateCommand.ExecuteNonQuery();
                conn.Close();

                return true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }

        }

      //根據(jù)條件刪除數(shù)據(jù)
        public bool Delete<T>(string  where)
        {

            try
            {

                var type = typeof(T);
                var className = type.Name;
                var sql = string.Format("delete from {0} {1}", className,
                     where);

                OleDbDataAdapter da = new OleDbDataAdapter();

                da.DeleteCommand = new OleDbCommand(sql, conn);
                da.DeleteCommand.CommandText = sql;

                conn.Open();
                da.DeleteCommand.ExecuteNonQuery();
                conn.Close();

                return true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }

        }

 

        //批量插入數(shù)據(jù)
        public bool InsertList<T>(List<T> entitysList) where T : class,new()
        {
          
            try
            {
                var type = typeof (T);
                var className = type.Name;

                var fields = "";
                var values = "";

                foreach (var property in type.GetProperties())
                {
                    if (property.Name.Equals("ID")) continue;
                    fields += "," + property.Name;
                    var isNumStr = (property.PropertyType == typeof (double) ||
                                    property.PropertyType == typeof (int))
                        ? ""
                        : "'";
                    values += ",?" ;
                }
                fields = fields.Substring(1);
                values = values.Substring(1);
                var sql = string.Format("insert into {0}({1}) values ({2}) ", className,
                    fields, values);

                OleDbDataAdapter da = new OleDbDataAdapter();

               
                da.InsertCommand = new OleDbCommand(sql, conn);
                da.InsertCommand.CommandText = sql;

                foreach (var property in type.GetProperties())
                {
                    if (property.Name.Equals("ID")) continue;
                    var oleType = (property.PropertyType == typeof(double) ||
                                   property.PropertyType == typeof(int))
                       ? OleDbType.Integer
                       : OleDbType.VarChar;
                    da.InsertCommand.Parameters.Add(property.Name, oleType, int.MaxValue,
                        property.Name);
                    fields += "," + property.Name;
                  
                    values += ",?";
                }
                var table = ConverterUtil.ConvertListToDataTable(entitysList);
                table.TableName = className;
                da.Update(table);
              
                return true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }

        }

 

       //這個(gè)方法是用來(lái)履行無(wú)返回結(jié)果的插入語(yǔ)句,如 insert  select
        public bool ExecuteInsertSql(string sql)
        {
          
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter();

               
                da.InsertCommand = new OleDbCommand(sql, conn);
                da.InsertCommand.CommandText = sql;

                conn.Open();
                da.InsertCommand.ExecuteNonQuery();
                conn.Close();
              
                return true;
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
                return false;
            }
            finally
            {
                conn.Close();
            }

        }

    }
  

}




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 激情欧美成人久久综合小说 | 欧美日韩国产亚洲一区二区三区 | 不卡一级aaa全黄毛片 | 欧美精品在线免费 | 暴力欧美娇小 videos | 日本精品久久久久护士 | 91伊人久久 | 国产精品日韩欧美久久综合 | 国产成人精品免费久久久久 | 波多野结衣 在线资源观看 波多野结衣 一区二区 | 日韩欧美亚洲一区 | 国产6080一级毛片 | 国产在线高清视频 | 欧美日韩一区二区三区四区在线观看 | 欧美一级毛片一级 | 国产精品福利视频手机免费观看 | 欧美多人性受xxxx喷水 | 亚洲ass| 久久精品一区二区三区不卡 | 久久国产精品久久国产片 | 老司机深夜福利在线 | free性欧美人另类 | 午夜视频在线免费看 | 久久精品亚洲欧美日韩久久 | 性做久久久久久久久 | 日韩亚洲欧美日本精品va | 亚洲最新永久在线观看 | 国产亚洲福利一区二区免费看 | 亚洲伊人成人网 | 老司机午夜精品视频观看 | 91美女啪啪 | 亚洲天堂图片 | 24小时中文乱码字幕在线观看 | 亚洲小说春色综合另类网蜜桃 | 成年人性生活免费视频 | 亚洲成人高清 | 欧美videos粗暴高清性 | 黄色毛片免费网站 | 欧美日韩一区二区三区久久 | 亚洲欧美日韩中文字幕在线一区 | 2022偷拍午夜视频在线播放 |