ElcroDBGenerator
Download ElcroDBGenerator - 416 KB
ElcroDBGenerator is a FREEWARE tool for generating C# code classes which represent an instance of a table from a SQL Server database table or
a MS Access database table. You must set the database type, connection string to the database and that is it. This tool proves to be usefull
for .NET C# developers especially in conjunction with the following method which will fill an instance of a class generated by this tool.
To use this method you have to add following
using System.Reflection;
Method return type:
Returns object with filled properties with values from the datarow, Column names must match properties names, where you called this method, you must cast returned object to the correct type you want
Parameters:
dr - DataRow from which to draw data
ocit - Object which represents datatype and return type
Method:
public class clsGO
{
#region GetObjectByDataRow
///
/// Return object with filled properties with values from the datarow, Column names must match properties names
///
/// DataRow from which to draw data
/// Object which represents datatype and return type
///
public static bool GetObjectByDataRow(System.Data.DataRow dr, ref T ocit)
{
try
{
foreach (DataColumn dc in dr.Table.Columns)
{
PropertyInfo pi = ocit.GetType().GetProperty(dc.ColumnName);
if (pi != null)
{
if (dr[dc.ColumnName] != DBNull.Value)
pi.SetValue(ocit, Convert.ChangeType(dr[dc.ColumnName], pi.PropertyType, null), null);
else
{
if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(double) || pi.PropertyType == typeof(float) || pi.PropertyType == typeof(decimal))
pi.SetValue(ocit, Convert.ChangeType(0, pi.PropertyType, null), null);
else if (pi.PropertyType == typeof(DateTime))
pi.SetValue(ocit, Convert.ChangeType(DateTime.MinValue, pi.PropertyType, null), null);
else
pi.SetValue(ocit, Convert.ChangeType(null, pi.PropertyType, null), null);
}
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}
Example of usage:
DataTable dtProducts = DBLayer.GetProductDataTable();
foreach(DataRow drProduct in dtProducts.Rows)
{
clsProduct cp = new clsProduct();
cp = (clsProduct) GetObjectByDataRow(drProduct, cp);
txtName.Text = cp.ProductName;
}
In the example above you can see typical usage. clsProduct is a class generated by ElcroDBGenerator tool, you have to fetch data into a datatable like above dtProducts
, go through it's DataRow collection. And, foreach datarow you can create a new instance of clsProduct, call GetObjectByDataRow to fill it, and use it's filled properties later.
You also have to provide the correct connection string which is used to connect to the datasource.
You can find examples of connection strings at this web site ->
http://www.connectionstrings.com
Download ElcroDBGenerator - 416 KB
Screenshot: