通用 class 用于将对象列表转换为 IEnumerable<SqlDataRecord>
Generic class for convert list of object to IEnumerable<SqlDataRecord>
我有这个对象:
public class ConsentData
{
public string ConsentName { get; set; }
public bool ConsentValue { get; set; }
public DateTime ConsentDate { get; set; }
}
我应该通过 table 值参数将同意数据列表传递给 SQL 服务器中的存储过程。
寻找一种方法来转换 SqlDataRecord 列表中的 ConsentData 列表,我在网上找到了这个通用的 class:
public class SqlTableValueSupport<T> : List<T>, IEnumerable<SqlDataRecord> where T : class, new()
{
IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
{
//Create Columns (SqlMetaData)
List<SqlMetaData> records = new List<SqlMetaData>();
var properties = typeof(T).GetProperties();
foreach (var prop in properties)
{
SqlDbType sdbtyp = GetSqlType(prop.PropertyType);
records.Add(new SqlMetaData(prop.Name, sdbtyp));
}
//Create records/rows (SqlDataRecord)
SqlDataRecord ret = new SqlDataRecord(records.ToArray());
foreach (T data in this)
{
for (int i = 0; i < properties.Length; i++)
{
ret.SetValue(i, properties[i].GetValue(data, null));
}
yield return ret;
}
}
// Map C# Types to SqlDbType
private SqlDbType GetSqlType(Type type)
{
SqlDbType val = SqlDbType.VarChar;
if (type == typeof(Int64) || type == typeof(Nullable<Int64>))
{
val = SqlDbType.BigInt;
}
else if (type == typeof(Byte[]))
{
val = SqlDbType.Binary;
}
else if (type == typeof(Boolean) || type == typeof(Nullable<Boolean>))
{
val = SqlDbType.Bit;
}
else if (type == typeof(DateTime) || type == typeof(Nullable<DateTime>))
{
val = SqlDbType.DateTime;
}
else if (type == typeof(Decimal))
{
val = SqlDbType.Decimal;
}
// Please refer to the following document to add other types
// http://msdn.microsoft.com/en-us/library/ms131092.aspx
return val;
}
}
我想知道如何使用 class,如何传递 ConsentData 列表并检索 SqlDataRecord 列表?
最后,我通过静态方法处理class解决了问题。
这是 class:
public class SqlTableValueSupport<T> where T : class
{
public static DataTable ConvertData(List<T> ValuesList)
{
DataTable dtData = new DataTable();
var objectReference = ValuesList.GetType().GetGenericArguments()[0];
var properties = objectReference.GetProperties();
foreach (var prop in properties)
{
dtData.Columns.Add(prop.Name, prop.PropertyType);
}
foreach (var item in ValuesList)
{
var dataArray = new List<object>();
foreach (var prop in properties)
{
dataArray.Add(prop.GetValue(item));
}
dtData.Rows.Add(dataArray.ToArray());
}
return dtData;
}
}
方法调用方式如下
var ListOfMyObjectDT = SqlTableValueSupport<MyObject>.ConvertData(ListOfMyObject);
我有这个对象:
public class ConsentData
{
public string ConsentName { get; set; }
public bool ConsentValue { get; set; }
public DateTime ConsentDate { get; set; }
}
我应该通过 table 值参数将同意数据列表传递给 SQL 服务器中的存储过程。
寻找一种方法来转换 SqlDataRecord 列表中的 ConsentData 列表,我在网上找到了这个通用的 class:
public class SqlTableValueSupport<T> : List<T>, IEnumerable<SqlDataRecord> where T : class, new()
{
IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator()
{
//Create Columns (SqlMetaData)
List<SqlMetaData> records = new List<SqlMetaData>();
var properties = typeof(T).GetProperties();
foreach (var prop in properties)
{
SqlDbType sdbtyp = GetSqlType(prop.PropertyType);
records.Add(new SqlMetaData(prop.Name, sdbtyp));
}
//Create records/rows (SqlDataRecord)
SqlDataRecord ret = new SqlDataRecord(records.ToArray());
foreach (T data in this)
{
for (int i = 0; i < properties.Length; i++)
{
ret.SetValue(i, properties[i].GetValue(data, null));
}
yield return ret;
}
}
// Map C# Types to SqlDbType
private SqlDbType GetSqlType(Type type)
{
SqlDbType val = SqlDbType.VarChar;
if (type == typeof(Int64) || type == typeof(Nullable<Int64>))
{
val = SqlDbType.BigInt;
}
else if (type == typeof(Byte[]))
{
val = SqlDbType.Binary;
}
else if (type == typeof(Boolean) || type == typeof(Nullable<Boolean>))
{
val = SqlDbType.Bit;
}
else if (type == typeof(DateTime) || type == typeof(Nullable<DateTime>))
{
val = SqlDbType.DateTime;
}
else if (type == typeof(Decimal))
{
val = SqlDbType.Decimal;
}
// Please refer to the following document to add other types
// http://msdn.microsoft.com/en-us/library/ms131092.aspx
return val;
}
}
我想知道如何使用 class,如何传递 ConsentData 列表并检索 SqlDataRecord 列表?
最后,我通过静态方法处理class解决了问题。 这是 class:
public class SqlTableValueSupport<T> where T : class
{
public static DataTable ConvertData(List<T> ValuesList)
{
DataTable dtData = new DataTable();
var objectReference = ValuesList.GetType().GetGenericArguments()[0];
var properties = objectReference.GetProperties();
foreach (var prop in properties)
{
dtData.Columns.Add(prop.Name, prop.PropertyType);
}
foreach (var item in ValuesList)
{
var dataArray = new List<object>();
foreach (var prop in properties)
{
dataArray.Add(prop.GetValue(item));
}
dtData.Rows.Add(dataArray.ToArray());
}
return dtData;
}
}
方法调用方式如下
var ListOfMyObjectDT = SqlTableValueSupport<MyObject>.ConvertData(ListOfMyObject);