----------test.aspx-------
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_Today;
using BL_Today;
namespace testasp
{
public partial class tesst : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<BEToday> list = new List<BEToday>();
for (int i = 0; i <= 100; i++)
{
BEToday be = new BEToday();
be.Id = i;
be.Name = "Ali " + i.ToString();
be.Address = "Multan " + i.ToString();
list.Add(be);
}
DataTable dt = ToDataTable(list);
new BLToday().SaveBulkCharity(dt);
GridView1.DataSource =list;
GridView1.DataBind();
}
}
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}
}
----------------------------DAl-------------------
private static string connect = MyConnectionString.path;
public void SaveBulkCharity(DataTable dataTable)
{
SqlConnection connection = new SqlConnection(connect);
if (dataTable != null)
{
try
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.ColumnMappings.Add("Id", "Id");
bulkCopy.ColumnMappings.Add("Name", "Name");
bulkCopy.ColumnMappings.Add("Address", "Address");
bulkCopy.DestinationTableName = "dbo.tbl_to";
bulkCopy.BulkCopyTimeout = 2000;
bulkCopy.WriteToServer(dataTable);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
---------------------BE------
public class BEToday
{
public int Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
-----------------Bl-----------
public void SaveBulkCharity(DataTable dataTable)
{
new DALToday().SaveBulkCharity(dataTable);
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE_Today;
using BL_Today;
namespace testasp
{
public partial class tesst : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<BEToday> list = new List<BEToday>();
for (int i = 0; i <= 100; i++)
{
BEToday be = new BEToday();
be.Id = i;
be.Name = "Ali " + i.ToString();
be.Address = "Multan " + i.ToString();
list.Add(be);
}
DataTable dt = ToDataTable(list);
new BLToday().SaveBulkCharity(dt);
GridView1.DataSource =list;
GridView1.DataBind();
}
}
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
}
}
----------------------------DAl-------------------
private static string connect = MyConnectionString.path;
public void SaveBulkCharity(DataTable dataTable)
{
SqlConnection connection = new SqlConnection(connect);
if (dataTable != null)
{
try
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.ColumnMappings.Add("Id", "Id");
bulkCopy.ColumnMappings.Add("Name", "Name");
bulkCopy.ColumnMappings.Add("Address", "Address");
bulkCopy.DestinationTableName = "dbo.tbl_to";
bulkCopy.BulkCopyTimeout = 2000;
bulkCopy.WriteToServer(dataTable);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
---------------------BE------
public class BEToday
{
public int Id {get; set;}
public string Name {get; set;}
public string Address {get; set;}
}
-----------------Bl-----------
public void SaveBulkCharity(DataTable dataTable)
{
new DALToday().SaveBulkCharity(dataTable);
}
No comments :
Post a Comment