日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 數據庫 > 數據庫應用 > 使用oledb對數據庫進行增刪改查及批量插入操作

使用oledb對數據庫進行增刪改查及批量插入操作

來源:程序員人生   發布時間:2015-06-30 08:32:59 閱讀次數:6480次

使用oledb操作數據庫工具類,可以使用泛型統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
    {

      //傳遞數據庫文件路徑,這里使用的是access2007數據庫
        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);//創建1個connection對象


        //使用泛型,獲得所有的實體類對象,返回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;
            }

        }

        //同上,根據條件,查詢,返回list實體類列表

        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條數據
        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();
            }

        }


        //更新實體類

        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();
            }

        }

      //根據條件刪除數據
        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();
            }

        }

 

        //批量插入數據
        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();
            }

        }

 

       //這個方法是用來履行無返回結果的插入語句,如 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();
            }

        }

    }
  

}




生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 91嫩草视频在线 | 色婷婷精品国产一区二区三区 | 亚洲精选一区 | 久久久久久久网 | 天堂在线视频免费 | 日韩激情在线 | 最新一级毛片 | 91精品国产99久久久 | 国产精品久久久久久久久 | 九九热精品视频 | 国产精品一区二区三区四区视频 | 日产精品久久久一区二区 | 在线观看亚洲视频 | 久久国产高清 | 91啦国产| 天堂视频在线观看 | 久久综合免费视频 | 久久97精品| 国产免费小视频 | 欧美性天天影院 | 欧美日韩国产一区 | 99在线免费观看视频 | 国内精品一区二区 | 久久成人免费网 | 欧美亚洲国产视频 | 欧美大片免费观看网址 | 爱爱免费视频 | 粉嫩精品一区二区三区在线观看 | 成人毛片网站 | 日本一区不卡视频 | 黄色小视频免费观看 | 日韩在线精品 | 在线一区二区三区 | 久久精品1 | 国产电影在线 | 亚洲一区二区在线免费观看 | 美女福利视频一区 | 亚洲免费在线 | 黄色小视频在线观看 | 国产 麻豆 日韩 欧美 久久 | 欧美日韩在线观看一区 |