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; }
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"); }
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 }); }