本文整理汇总了C#中System.Data.SqlClient.SqlParameter.ResetSqlDbType方法的典型用法代码示例。如果您正苦于以下问题:C# SqlParameter.ResetSqlDbType方法的具体用法?C# SqlParameter.ResetSqlDbType怎么用?C# SqlParameter.ResetSqlDbType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlParameter
的用法示例。
在下文中一共展示了SqlParameter.ResetSqlDbType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddWithValue
public static SqlParameterCollection AddWithValue(
this SqlParameterCollection parameters,
string parameterName,
SqlDbType dbType,
object value)
{
var parameter = new SqlParameter(parameterName, dbType);
parameter.Value = value;
parameters.Add(parameter);
parameter.ResetSqlDbType();
return parameters;
}
示例2: ResetSqlDbType
public void ResetSqlDbType ()
{
//Parameter with an assigned value but no SqlDbType specified
SqlParameter p1 = new SqlParameter ("foo", 42);
Assert.AreEqual (42, p1.Value, "#1");
Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#3");
p1.ResetSqlDbType ();
Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#5 The parameter with value 42 must have SqlDbType as Int");
p1.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
Assert.AreEqual (SqlDbType.DateTime, p1.SqlDbType, "#7");
p1.ResetSqlDbType (); //Resetting SqlDbType
Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting SqlDbType must infer the type from the value");
Assert.AreEqual (SqlDbType.Int, p1.SqlDbType, "#9 Resetting SqlDbType must infer the type from the value");
//Parameter with an assigned SqlDbType but no specified value
SqlParameter p2 = new SqlParameter ("foo", SqlDbType.Int);
Assert.AreEqual (null, p2.Value, "#10");
Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
Assert.AreEqual (SqlDbType.Int, p2.SqlDbType, "#12");
//Although a SqlDbType is specified, calling ResetSqlDbType resets
//the SqlDbType and DbType properties to default values
p2.ResetSqlDbType ();
Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting SqlDbType must infer the type from the value");
Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#14 Resetting SqlDbType must infer the type from the value");
p2.SqlDbType = SqlDbType.DateTime; //assigning a SqlDbType
Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
Assert.AreEqual (SqlDbType.DateTime, p2.SqlDbType, "#16");
p2.ResetSqlDbType (); //Resetting SqlDbType
Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting SqlDbType must infer the type from the value");
Assert.AreEqual (SqlDbType.NVarChar, p2.SqlDbType, "#18 Resetting SqlDbType must infer the type from the value");
}
示例3: 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 });
}