public partial class BulkTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> list = new List<Student>();
for (int i = 0; i < 100; i++)
{
Student be = new Student();
be.MyId = i;
be.MyName = "Sajjad " + i.ToString();
be.MyAddress = "Ryk " + i.ToString();
list.Add(be);
}
DataTable dt = ToDataTable(list);
new BLAdmin().SaveBulkCharity(dt);
}
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;
}
}
public class Student
{
public int MyId { get; set; }
public string MyName { get; set; }
public string MyAddress { get; set; }
}
-------------------DAL
public void SaveBulkCharity(DataTable dataTable)
{
SqlConnection connection = new SqlConnection(path);
if (dataTable != null)
{
try
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.ColumnMappings.Add("MyId", "Id");
bulkCopy.ColumnMappings.Add("MyName", "Name");
bulkCopy.ColumnMappings.Add("MyAddress", "Address");
bulkCopy.DestinationTableName = "dbo.tblTest";
bulkCopy.BulkCopyTimeout = 2000;
bulkCopy.WriteToServer(dataTable);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
List<Student> list = new List<Student>();
for (int i = 0; i < 100; i++)
{
Student be = new Student();
be.MyId = i;
be.MyName = "Sajjad " + i.ToString();
be.MyAddress = "Ryk " + i.ToString();
list.Add(be);
}
DataTable dt = ToDataTable(list);
new BLAdmin().SaveBulkCharity(dt);
}
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;
}
}
public class Student
{
public int MyId { get; set; }
public string MyName { get; set; }
public string MyAddress { get; set; }
}
-------------------DAL
public void SaveBulkCharity(DataTable dataTable)
{
SqlConnection connection = new SqlConnection(path);
if (dataTable != null)
{
try
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
{
bulkCopy.ColumnMappings.Add("MyId", "Id");
bulkCopy.ColumnMappings.Add("MyName", "Name");
bulkCopy.ColumnMappings.Add("MyAddress", "Address");
bulkCopy.DestinationTableName = "dbo.tblTest";
bulkCopy.BulkCopyTimeout = 2000;
bulkCopy.WriteToServer(dataTable);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
No comments:
Post a Comment