国产探花免费观看_亚洲丰满少妇自慰呻吟_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;        }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天峨县| 沁源县| 乐陵市| 广元市| 长宁区| 峨山| 东源县| 阜南县| 玉山县| 青川县| 芦溪县| 义乌市| 乡宁县| 鸡西市| 巴林左旗| 夏河县| 疏附县| 来凤县| 阿合奇县| 洪湖市| 衡阳县| 辽阳市| 乡城县| 襄汾县| 宿州市| 赤壁市| 航空| 绩溪县| 行唐县| 桓仁| 乐都县| 新津县| 定南县| 浮梁县| 承德县| 监利县| 濮阳市| 旬邑县| 浑源县| 上栗县| 甘孜|