//Overrides the binding process so we can use the correct database types public override void BindParameters(DbCommand command, List <Parameter> results) { if (results != null) { //Morphs the command object intot he sql version SqlCommand sqlCommand = command as SqlCommand; foreach (Parameter param in results) { //Overrides the binding if its an sp variable string bindingStart = (sqlCommand.CommandType == CommandType.StoredProcedure) ? "@" : InternalDatabaseBinding; string bindingDefinition = $"{bindingStart}{param.BindingName}"; sqlCommand.Parameters.AddWithValue(bindingDefinition, (param.Value ?? DBNull.Value)); if (param.Value != null) { if (param.DatabaseType != null) { SQLServerDatabaseType dbType = param.DatabaseType as SQLServerDatabaseType; sqlCommand.Parameters[bindingDefinition].SqlDbType = dbType.SQLDBType.Value; } else//Use our automatic conversion system { SqlDbType?dbType = BindingConvertor.ToSqlDbType(param.Type); if (dbType.HasValue) { sqlCommand.Parameters[bindingDefinition].SqlDbType = dbType.Value; } } } } } }
public void SuccessWithTypesCheck() { var pms = new { you = "test123", AGE = 11 }; var typs = new { you = SqlDbType.VarChar, AGE = SqlDbType.Int }; SQLServerParameterProcessor mProcessor = new SQLServerParameterProcessor("@"); var parameters = mProcessor.GetParameters(query, pms, typs); Assert.IsTrue(parameters != null && parameters.Count == 2); SQLServerDatabaseType youType = parameters.Where(p => p.BindingName == "you").First().DatabaseType as SQLServerDatabaseType; SQLServerDatabaseType AGEType = parameters.Where(p => p.BindingName == "AGE").First().DatabaseType as SQLServerDatabaseType; Assert.IsTrue(youType.SQLDBType == SqlDbType.VarChar && AGEType.SQLDBType == SqlDbType.Int); }
//Overrides the updating of the database type to store our MySQL version protected override void AddParameterDatabaseType(Parameter parameter, string propertyName, Type propertyType, object propertyValue) { SQLServerDatabaseType databaseType = new SQLServerDatabaseType(propertyName, propertyType, propertyValue); parameter.DatabaseType = databaseType; }