本文整理汇总了C#中System.Data.SqlClient.SqlParameter.ResetDbType方法的典型用法代码示例。如果您正苦于以下问题:C# SqlParameter.ResetDbType方法的具体用法?C# SqlParameter.ResetDbType怎么用?C# SqlParameter.ResetDbType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlParameter
的用法示例。
在下文中一共展示了SqlParameter.ResetDbType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResetDbType
public void ResetDbType ()
{
SqlParameter p;
//Parameter with an assigned value but no DbType specified
p = new SqlParameter ("foo", 42);
p.ResetDbType ();
Assert.AreEqual (DbType.Int32, p.DbType, "#A:DbType");
Assert.AreEqual (SqlDbType.Int, p.SqlDbType, "#A:SqlDbType");
Assert.AreEqual (42, p.Value, "#A:Value");
p.DbType = DbType.DateTime; //assigning a DbType
Assert.AreEqual (DbType.DateTime, p.DbType, "#B:DbType1");
Assert.AreEqual (SqlDbType.DateTime, p.SqlDbType, "#B:SqlDbType1");
p.ResetDbType ();
Assert.AreEqual (DbType.Int32, p.DbType, "#B:DbType2");
Assert.AreEqual (SqlDbType.Int, p.SqlDbType, "#B:SqlDbtype2");
//Parameter with an assigned SqlDbType but no specified value
p = new SqlParameter ("foo", SqlDbType.Int);
p.ResetDbType ();
Assert.AreEqual (DbType.String, p.DbType, "#C:DbType");
Assert.AreEqual (SqlDbType.NVarChar, p.SqlDbType, "#C:SqlDbType");
p.DbType = DbType.DateTime; //assigning a SqlDbType
Assert.AreEqual (DbType.DateTime, p.DbType, "#D:DbType1");
Assert.AreEqual (SqlDbType.DateTime, p.SqlDbType, "#D:SqlDbType1");
p.ResetDbType ();
Assert.AreEqual (DbType.String, p.DbType, "#D:DbType2");
Assert.AreEqual (SqlDbType.NVarChar, p.SqlDbType, "#D:SqlDbType2");
p = new SqlParameter ();
p.Value = DateTime.MaxValue;
Assert.AreEqual (DbType.DateTime, p.DbType, "#E:DbType1");
Assert.AreEqual (SqlDbType.DateTime, p.SqlDbType, "#E:SqlDbType1");
p.Value = null;
p.ResetDbType ();
Assert.AreEqual (DbType.String, p.DbType, "#E:DbType2");
Assert.AreEqual (SqlDbType.NVarChar, p.SqlDbType, "#E:SqlDbType2");
p = new SqlParameter ("foo", SqlDbType.VarChar);
p.Value = DateTime.MaxValue;
p.ResetDbType ();
Assert.AreEqual (DbType.DateTime, p.DbType, "#F:DbType");
Assert.AreEqual (SqlDbType.DateTime, p.SqlDbType, "#F:SqlDbType");
Assert.AreEqual (DateTime.MaxValue, p.Value, "#F:Value");
p = new SqlParameter ("foo", SqlDbType.VarChar);
p.Value = DBNull.Value;
p.ResetDbType ();
Assert.AreEqual (DbType.String, p.DbType, "#G:DbType");
Assert.AreEqual (SqlDbType.NVarChar, p.SqlDbType, "#G:SqlDbType");
Assert.AreEqual (DBNull.Value, p.Value, "#G:Value");
p = new SqlParameter ("foo", SqlDbType.VarChar);
p.Value = null;
p.ResetDbType ();
Assert.AreEqual (DbType.String, p.DbType, "#G:DbType");
Assert.AreEqual (SqlDbType.NVarChar, p.SqlDbType, "#G:SqlDbType");
Assert.IsNull (p.Value, "#G:Value");
}
示例2: AddCompilationInfo
private static void AddCompilationInfo(Type itemType)
{
var members = itemType.GetMembers();
var props =
members.Where(m => m.MemberType == MemberTypes.Property || m.MemberType == MemberTypes.Field)
.ToArray();
var coercionParameter = new SqlParameter();
var metaData = new SqlMetaData[props.Length];
for (int index = 0; index < props.Length; index++)
{
var prop = props[index];
if (index > 0)
{
coercionParameter.ResetDbType();
coercionParameter.ResetSqlDbType();
}
switch (props[index].MemberType)
{
case MemberTypes.Property:
coercionParameter.DbType = CommandManager.GetDbType(((PropertyInfo)prop).PropertyType) ??
DbType.Object;
break;
case MemberTypes.Field:
coercionParameter.DbType = CommandManager.GetDbType(((FieldInfo)prop).FieldType) ??
DbType.Object;
break;
}
metaData[index] = new SqlMetaData(prop.Name, coercionParameter.SqlDbType);
}
var bodyExpressions = new List<Expression>();
var metaDataParameter = Expression.Parameter(typeof(SqlMetaData[]), "metaData");
var itemParameter = Expression.Parameter(typeof(object), "item");
var castedItemParameter = Expression.Variable(itemType, "castedItem");
var recordVariable = Expression.Variable(typeof(SqlDataRecord), "record");
bodyExpressions.Add(Expression.Assign(recordVariable,
Expression.New(SqlDataRecordConstructorInfo, metaDataParameter)));
bodyExpressions.Add(Expression.Assign(castedItemParameter,
Expression.Convert(itemParameter, itemType)));
for (int index = 0; index < props.Length; index++)
{
var mi = props[index];
switch (mi.MemberType)
{
case MemberTypes.Property:
bodyExpressions.Add(Expression.Call(recordVariable, SetValueMethodInfo,
new Expression[]
{
Expression.Constant(index, typeof (int)),
Expression.Convert(Expression.Property(castedItemParameter, itemType, mi.Name),
typeof (object))
}));
break;
case MemberTypes.Field:
bodyExpressions.Add(Expression.Call(recordVariable, SetValueMethodInfo,
new Expression[]
{
Expression.Constant(index, typeof (int)),
Expression.Convert(Expression.Field(castedItemParameter, itemType, mi.Name),
typeof (object))
}));
break;
}
}
bodyExpressions.Add(recordVariable);
var body = Expression.Block(new[] { recordVariable, castedItemParameter }, bodyExpressions);
var lambda = Expression.Lambda<Func<SqlMetaData[], object, SqlDataRecord>>(body, metaDataParameter,
itemParameter);
CompiledFuncs.Add(itemType.AssemblyQualifiedName, new DelegateInfo { Func = lambda.Compile(), MetaData = metaData });
}