国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

bulkcopy實現批量插入與更新

2019-11-14 13:42:44
字體:
來源:轉載
供稿:網友
public static void UpdateData<T>(List<T> list, string TabelName)        {            DataTable dt = new DataTable("MyTable");            clsBulkOperation blk = new clsBulkOperation();            dt = ConvertToDataTable(list);            ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tcxxconnstring"].ConnectionString))            {                using (SqlCommand command = new SqlCommand("", conn))                {                    try                    {                        conn.Open();                        command.CommandText = "CREATE TABLE #TmpTable(...)";                        command.ExecuteNonQuery();                        using (SqlBulkCopy  = new SqlBulkCopy(conn))                        {                            bulkcopy.BulkCopyTimeout = 660;                            bulkcopy.DestinationTableName = TabelName;                            bulkcopy.WriteToServer(dt);                            bulkcopy.Close();                        }                        // Updating destination table, and dropping temp table                        command.CommandTimeout = 300;                        command.CommandText = "UPDATE T SET ... FROM " + TabelName + " T INNER JOIN #TmpTable Temp ON ...; DROP TABLE #TmpTable;";                        command.ExecuteNonQuery();                    }                    catch (Exception ex)                    {                        // Handle exception PRoperly                    }                    finally                    {                        conn.Close();                    }                }            }        }  public static void InsertData<T>(List<T> list,string TableName)        {                DataTable dt = new DataTable("MyTable");                clsBulkOperation blk = new clsBulkOperation();                dt = ConvertToDataTable(list);                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);                using (SqlBulkCopy bulkcopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["SchoolSoulDataEntitiesForReport"].ConnectionString))                {                    bulkcopy.BulkCopyTimeout = 660;                    bulkcopy.DestinationTableName = TableName;                    bulkcopy.WriteToServer(dt);                }        }    public static DataTable ConvertToDataTable<T>(IList<T> data)        {            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));            DataTable table = new DataTable();            foreach (PropertyDescriptor prop in properties)                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);            foreach (T item in data)            {                DataRow row = table.NewRow();                foreach (PropertyDescriptor prop in properties)                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;                table.Rows.Add(row);            }            return table;        }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 峨眉山市| 利辛县| 湛江市| 万年县| 扎兰屯市| 监利县| 祥云县| 黄冈市| 永春县| 天津市| 平舆县| 特克斯县| 海门市| 江都市| 固始县| 德江县| 八宿县| 苗栗市| 宁强县| 遵义县| 介休市| 天门市| 永寿县| 马公市| 武强县| 会泽县| 新巴尔虎左旗| 芜湖县| 五华县| 丰台区| 克什克腾旗| 罗平县| 永靖县| 东安县| 莫力| 织金县| 满城县| 湘乡市| 博罗县| 巴林左旗| 布尔津县|