public static GetParameters ( System.Guid dataID, |
||
dataID | System.Guid | |
providerMetadata | ||
columns | ||
Результат | NpgsqlParameter>.Dictionary |
static public void PopulateStoredProcParameters(NpgsqlCommand cmd, tgDataRequest request, tgEntitySavePacket packet) { Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlParameter p; foreach (tgColumnMetadata col in request.Columns) { p = types[col.Name]; p = CloneParameter(p); if (packet.CurrentValues.ContainsKey(col.Name)) { p.Value = packet.CurrentValues[col.Name]; } if (p.NpgsqlDbType == NpgsqlDbType.Timestamp) { p.Direction = ParameterDirection.InputOutput; } if (col.IsComputed && col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } cmd.Parameters.Add(p); } }
static public NpgsqlCommand BuildStoredProcUpdateCommand(tgDataRequest request, tgEntitySavePacket packet) { Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = Delimiters.StoredProcNameOpen + request.ProviderMetadata.spUpdate + Delimiters.StoredProcNameClose; PopulateStoredProcParameters(cmd, request, packet); foreach (tgColumnMetadata col in request.Columns) { if (col.IsComputed) { NpgsqlParameter p = types[col.Name]; p = cmd.Parameters[p.ParameterName]; p.Direction = ParameterDirection.InputOutput; } } return(cmd); }
static public NpgsqlCommand BuildStoredProcInsertCommand(tgDataRequest request, tgEntitySavePacket packet) { Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = Delimiters.StoredProcNameOpen + request.ProviderMetadata.spInsert + Delimiters.StoredProcNameClose; PopulateStoredProcParameters(cmd, request, packet); foreach (tgColumnMetadata col in request.Columns) { if (col.HasDefault && col.Default.ToLower().Contains("newid")) { NpgsqlParameter p = types[col.Name]; p = cmd.Parameters[p.ParameterName]; p.Direction = ParameterDirection.InputOutput; } else if (col.IsComputed || col.IsAutoIncrement) { NpgsqlParameter p = types[col.Name]; p = cmd.Parameters[p.ParameterName]; p.Direction = ParameterDirection.Output; } } return(cmd); }
static public NpgsqlCommand BuildDynamicDeleteCommand(tgDataRequest request, tgEntitySavePacket packet) { Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } string sql = "DELETE FROM " + CreateFullName(request) + " "; string comma = String.Empty; comma = String.Empty; sql += " WHERE "; foreach (tgColumnMetadata col in request.Columns) { if (col.IsInPrimaryKey || col.IsTiraggoConcurrency) { NpgsqlParameter p = types[col.Name]; p = cmd.Parameters.Add(CloneParameter(p)); p.Value = packet.OriginalValues[col.Name]; sql += comma; sql += Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; comma = " AND "; } } cmd.CommandText = sql; cmd.CommandType = CommandType.Text; return(cmd); }
static public NpgsqlCommand BuildStoredProcDeleteCommand(tgDataRequest request, tgEntitySavePacket packet) { Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = Delimiters.StoredProcNameOpen + request.ProviderMetadata.spDelete + Delimiters.StoredProcNameClose; NpgsqlParameter p; foreach (tgColumnMetadata col in request.Columns) { if (col.IsInPrimaryKey) { p = types[col.Name]; p = CloneParameter(p); p.Value = packet.OriginalValues[col.Name]; cmd.Parameters.Add(p); } else if (col.IsConcurrency || col.IsTiraggoConcurrency) { p = types[col.Name]; p = CloneParameter(p); p.Value = packet.OriginalValues[col.Name]; cmd.Parameters.Add(p); } } return(cmd); }
static public NpgsqlCommand BuildDynamicInsertCommand(tgDataRequest request, tgEntitySavePacket packet) { string sql = String.Empty; string defaults = String.Empty; string into = String.Empty; string values = String.Empty; string comma = String.Empty; string defaultComma = String.Empty; string where = String.Empty; string autoInc = String.Empty; NpgsqlParameter p = null; Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } tgColumnMetadataCollection cols = request.Columns; foreach (tgColumnMetadata col in cols) { bool isModified = packet.ModifiedColumns == null ? false : packet.ModifiedColumns.Contains(col.Name); if (isModified && (!col.IsAutoIncrement && !col.IsConcurrency && !col.IsTiraggoConcurrency)) { p = cmd.Parameters.Add(CloneParameter(types[col.Name])); object value = packet.CurrentValues[col.Name]; p.Value = value != null ? value : DBNull.Value; into += comma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; values += comma + p.ParameterName; comma = ", "; } else if (col.IsAutoIncrement && request.ProviderMetadata.ContainsKey("AutoKeyText")) { string sequence = request.ProviderMetadata["AutoKeyText"].Replace("nextval", "currval"); if (sequence != null && sequence.Length > 0) { // Our identity column ... p = cmd.Parameters.Add(CloneParameter(types[col.Name])); p.Direction = ParameterDirection.Output; autoInc += " SELECT * FROM " + sequence + " as \"" + col.Name + "\""; } p = CloneParameter(types[col.Name]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); } else if (col.IsConcurrency) { // These columns have defaults and they weren't supplied with values, so let's // return them p = cmd.Parameters.Add(CloneParameter(types[col.Name])); p.Direction = ParameterDirection.InputOutput; defaults += defaultComma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; defaultComma = ", "; if (col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } } else if (col.IsTiraggoConcurrency) { p = cmd.Parameters.Add(CloneParameter(types[col.Name])); p.Direction = ParameterDirection.Output; into += comma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; values += comma + "1"; comma = ", "; p.Value = 1; // Seems to work, We'll take it ... } else if (col.IsComputed) { // Do nothing but leave this here } else if (cols.IsSpecialColumn(col)) { // Do nothing but leave this here } else if (col.HasDefault) { // These columns have defaults and they weren't supplied with values, so let's // return them p = cmd.Parameters.Add(CloneParameter(types[col.Name])); p.Direction = ParameterDirection.InputOutput; defaults += defaultComma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; defaultComma = ","; if (col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } } if (col.IsInPrimaryKey) { p = types[col.Name]; if (where.Length > 0) { where += " AND "; } where += Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; if (!cmd.Parameters.Contains(p.ParameterName)) { p = CloneParameter(p); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); } } } #region Special Column Logic if (cols.DateAdded != null && cols.DateAdded.IsServerSide && cols.FindByColumnName(cols.DateAdded.ColumnName) != null) { p = CloneParameter(types[cols.DateAdded.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); into += comma + Delimiters.ColumnOpen + cols.DateAdded.ColumnName + Delimiters.ColumnClose; values += comma + request.ProviderMetadata["DateAdded.ServerSideText"]; comma = ", "; defaults += defaultComma + cols.DateAdded.ColumnName; defaultComma = ","; } if (cols.DateModified != null && cols.DateModified.IsServerSide && cols.FindByColumnName(cols.DateModified.ColumnName) != null) { p = CloneParameter(types[cols.DateModified.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); into += comma + Delimiters.ColumnOpen + cols.DateModified.ColumnName + Delimiters.ColumnClose; values += comma + request.ProviderMetadata["DateModified.ServerSideText"]; comma = ", "; defaults += defaultComma + cols.DateModified.ColumnName; defaultComma = ","; } if (cols.AddedBy != null && cols.AddedBy.IsServerSide && cols.FindByColumnName(cols.AddedBy.ColumnName) != null) { p = CloneParameter(types[cols.AddedBy.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); into += comma + Delimiters.ColumnOpen + cols.AddedBy.ColumnName + Delimiters.ColumnClose; values += comma + request.ProviderMetadata["AddedBy.ServerSideText"]; comma = ", "; defaults += defaultComma + cols.AddedBy.ColumnName; defaultComma = ","; tgColumnMetadata col = request.Columns[cols.ModifiedBy.ColumnName]; if (col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } } if (cols.ModifiedBy != null && cols.ModifiedBy.IsServerSide && cols.FindByColumnName(cols.ModifiedBy.ColumnName) != null) { p = CloneParameter(types[cols.ModifiedBy.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); into += comma + Delimiters.ColumnOpen + cols.ModifiedBy.ColumnName + Delimiters.ColumnClose; values += comma + request.ProviderMetadata["ModifiedBy.ServerSideText"]; comma = ", "; defaults += defaultComma + cols.ModifiedBy.ColumnName; defaultComma = ","; tgColumnMetadata col = request.Columns[cols.ModifiedBy.ColumnName]; if (col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } } #endregion string fullName = CreateFullName(request); sql += " INSERT INTO " + fullName; if (into.Length != 0) { sql += " (" + into + ") VALUES (" + values + ");"; } else { sql += " DEFAULT VALUES;"; } sql += autoInc; if (defaults.Length > 0) { sql += " SELECT " + defaults + " FROM " + fullName + " WHERE (" + where + ")"; } cmd.CommandText = sql + String.Empty; cmd.CommandType = CommandType.Text; return(cmd); }
static public NpgsqlCommand BuildDynamicUpdateCommand(tgDataRequest request, tgEntitySavePacket packet) { string where = String.Empty; string conncur = String.Empty; string scomma = String.Empty; string defaults = String.Empty; string defaultsComma = String.Empty; string and = String.Empty; string sql = "UPDATE " + CreateFullName(request) + " SET "; PropertyCollection props = new PropertyCollection(); NpgsqlParameter p = null; Dictionary <string, NpgsqlParameter> types = Cache.GetParameters(request); NpgsqlCommand cmd = new NpgsqlCommand(); if (request.CommandTimeout != null) { cmd.CommandTimeout = request.CommandTimeout.Value; } tgColumnMetadataCollection cols = request.Columns; foreach (tgColumnMetadata col in cols) { bool isModified = packet.ModifiedColumns == null ? false : packet.ModifiedColumns.Contains(col.Name); if (isModified && (!col.IsAutoIncrement && !col.IsConcurrency && !col.IsTiraggoConcurrency)) { p = cmd.Parameters.Add(CloneParameter(types[col.Name])); object value = packet.CurrentValues[col.Name]; p.Value = value != null ? value : DBNull.Value; sql += scomma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; scomma = ", "; } else if (col.IsAutoIncrement) { // Nothing to do but leave this here } else if (col.IsConcurrency) { p = CloneParameter(types[col.Name]); p.SourceVersion = DataRowVersion.Original; p.Direction = ParameterDirection.InputOutput; cmd.Parameters.Add(p); conncur += Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; } else if (col.IsTiraggoConcurrency) { p = CloneParameter(types[col.Name]); p.Value = packet.OriginalValues[col.Name]; p.Direction = ParameterDirection.InputOutput; cmd.Parameters.Add(p); sql += scomma; sql += Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName + " + 1"; conncur += Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; defaults += defaultsComma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; defaultsComma = ","; } else if (col.IsComputed) { // Do nothing but leave this here } else if (cols.IsSpecialColumn(col)) { // Do nothing but leave this here } else if (col.HasDefault) { // defaults += defaultsComma + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose; // defaultsComma = ","; } if (col.IsInPrimaryKey) { p = CloneParameter(types[col.Name]); p.Value = packet.OriginalValues[col.Name]; cmd.Parameters.Add(p); where += and + Delimiters.ColumnOpen + col.Name + Delimiters.ColumnClose + " = " + p.ParameterName; and = " AND "; } } #region Special Column Logic if (cols.DateModified != null && cols.DateModified.IsServerSide && cols.FindByColumnName(cols.DateModified.ColumnName) != null) { p = CloneParameter(types[cols.DateModified.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); sql += scomma + Delimiters.ColumnOpen + cols.DateModified.ColumnName + Delimiters.ColumnClose + " = " + request.ProviderMetadata["DateModified.ServerSideText"]; scomma = ", "; defaults += defaultsComma + cols.DateModified.ColumnName; defaultsComma = ","; } if (cols.ModifiedBy != null && cols.ModifiedBy.IsServerSide && cols.FindByColumnName(cols.ModifiedBy.ColumnName) != null) { p = CloneParameter(types[cols.ModifiedBy.ColumnName]); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); sql += scomma + Delimiters.ColumnOpen + cols.ModifiedBy.ColumnName + Delimiters.ColumnClose + " = " + request.ProviderMetadata["ModifiedBy.ServerSideText"]; scomma = ", "; defaults += defaultsComma + cols.ModifiedBy.ColumnName; defaultsComma = ","; tgColumnMetadata col = request.Columns[cols.ModifiedBy.ColumnName]; if (col.CharacterMaxLength > 0) { p.Size = (int)col.CharacterMaxLength; } } #endregion sql += " WHERE " + where + ""; if (conncur.Length > 0) { sql += " AND " + conncur; } if (defaults.Length > 0) { sql += "; SELECT " + defaults + " FROM " + CreateFullName(request) + " WHERE (" + where + ")"; } cmd.CommandText = sql; cmd.CommandType = CommandType.Text; return(cmd); }
protected static string GetComparisonStatement(StandardProviderParameters std, tgDynamicQuerySerializable query, List <tgComparison> items, string prefix) { string sql = String.Empty; string comma = String.Empty; IDynamicQuerySerializableInternal iQuery = query as IDynamicQuerySerializableInternal; //======================================= // WHERE //======================================= if (items != null) { sql += prefix; string compareTo = String.Empty; foreach (tgComparison comparisonItem in items) { tgComparison.tgComparisonData comparisonData = (tgComparison.tgComparisonData)comparisonItem; tgDynamicQuerySerializable subQuery = null; bool requiresParam = true; bool needsStringParameter = false; if (comparisonData.IsParenthesis) { if (comparisonData.Parenthesis == tgParenthesis.Open) { sql += "("; } else { sql += ")"; } continue; } if (comparisonData.IsConjunction) { switch (comparisonData.Conjunction) { case tgConjunction.And: sql += " AND "; break; case tgConjunction.Or: sql += " OR "; break; case tgConjunction.AndNot: sql += " AND NOT "; break; case tgConjunction.OrNot: sql += " OR NOT "; break; } continue; } Dictionary <string, NpgsqlParameter> types = null; if (comparisonData.Column.Query != null) { IDynamicQuerySerializableInternal iLocalQuery = comparisonData.Column.Query as IDynamicQuerySerializableInternal; types = Cache.GetParameters(iLocalQuery.DataID, (tgProviderSpecificMetadata)iLocalQuery.ProviderMetadata, (tgColumnMetadataCollection)iLocalQuery.Columns); } if (comparisonData.IsLiteral) { if (comparisonData.Column.Name[0] == '<') { sql += comparisonData.Column.Name.Substring(1, comparisonData.Column.Name.Length - 2); } else { sql += comparisonData.Column.Name; } continue; } if (comparisonData.ComparisonColumn.Name == null) { subQuery = comparisonData.Value as tgDynamicQuerySerializable; if (subQuery == null) { if (comparisonData.Column.Name != null) { IDynamicQuerySerializableInternal iColQuery = comparisonData.Column.Query as IDynamicQuerySerializableInternal; tgColumnMetadataCollection columns = (tgColumnMetadataCollection)iColQuery.Columns; compareTo = Delimiters.Param + columns[comparisonData.Column.Name].PropertyName + (++std.pindex).ToString(); } else { compareTo = Delimiters.Param + "Expr" + (++std.pindex).ToString(); } } else { // It's a sub query compareTo = GetSubquerySearchCondition(subQuery) + " (" + BuildQuery(std, subQuery) + ") "; requiresParam = false; } } else { compareTo = GetColumnName(comparisonData.ComparisonColumn); requiresParam = false; } switch (comparisonData.Operand) { case tgComparisonOperand.Exists: sql += " EXISTS" + compareTo; break; case tgComparisonOperand.NotExists: sql += " NOT EXISTS" + compareTo; break; //----------------------------------------------------------- // Comparison operators, left side vs right side //----------------------------------------------------------- case tgComparisonOperand.Equal: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " = " + compareTo; } else { sql += compareTo + " = " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.NotEqual: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " <> " + compareTo; } else { sql += compareTo + " <> " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.GreaterThan: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " > " + compareTo; } else { sql += compareTo + " > " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.LessThan: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " < " + compareTo; } else { sql += compareTo + " < " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.LessThanOrEqual: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " <= " + compareTo; } else { sql += compareTo + " <= " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.GreaterThanOrEqual: if (comparisonData.ItemFirst) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " >= " + compareTo; } else { sql += compareTo + " >= " + ApplyWhereSubOperations(std, query, comparisonData); } break; case tgComparisonOperand.Like: string esc = comparisonData.LikeEscape.ToString(); if (String.IsNullOrEmpty(esc) || esc == "\0") { sql += ApplyWhereSubOperations(std, query, comparisonData) + " LIKE " + compareTo; needsStringParameter = true; } else { sql += ApplyWhereSubOperations(std, query, comparisonData) + " LIKE " + compareTo; sql += " ESCAPE '" + esc + "'"; needsStringParameter = true; } break; case tgComparisonOperand.NotLike: esc = comparisonData.LikeEscape.ToString(); if (String.IsNullOrEmpty(esc) || esc == "\0") { sql += ApplyWhereSubOperations(std, query, comparisonData) + " NOT LIKE " + compareTo; needsStringParameter = true; } else { sql += ApplyWhereSubOperations(std, query, comparisonData) + " NOT LIKE " + compareTo; sql += " ESCAPE '" + esc + "'"; needsStringParameter = true; } break; case tgComparisonOperand.Contains: sql += " CONTAINS(" + GetColumnName(comparisonData.Column) + ", " + compareTo + ")"; needsStringParameter = true; break; case tgComparisonOperand.IsNull: sql += ApplyWhereSubOperations(std, query, comparisonData) + " IS NULL"; requiresParam = false; break; case tgComparisonOperand.IsNotNull: sql += ApplyWhereSubOperations(std, query, comparisonData) + " IS NOT NULL"; requiresParam = false; break; case tgComparisonOperand.In: case tgComparisonOperand.NotIn: { if (subQuery != null) { // They used a subquery for In or Not sql += ApplyWhereSubOperations(std, query, comparisonData); sql += (comparisonData.Operand == tgComparisonOperand.In) ? " IN" : " NOT IN"; sql += compareTo; } else { comma = String.Empty; if (comparisonData.Operand == tgComparisonOperand.In) { sql += ApplyWhereSubOperations(std, query, comparisonData) + " IN ("; } else { sql += ApplyWhereSubOperations(std, query, comparisonData) + " NOT IN ("; } foreach (object oin in comparisonData.Values) { string str = oin as string; if (str != null) { // STRING sql += comma + Delimiters.StringOpen + str + Delimiters.StringClose; comma = ","; } else if (null != oin as System.Collections.IEnumerable) { // LIST OR COLLECTION OF SOME SORT System.Collections.IEnumerable enumer = oin as System.Collections.IEnumerable; if (enumer != null) { System.Collections.IEnumerator iter = enumer.GetEnumerator(); while (iter.MoveNext()) { object o = iter.Current; string soin = o as string; if (soin != null) { sql += comma + Delimiters.StringOpen + soin + Delimiters.StringClose; } else { sql += comma + Convert.ToString(o); } comma = ","; } } } else { // NON STRING OR LIST sql += comma + Convert.ToString(oin); comma = ","; } } sql += ")"; requiresParam = false; } } break; case tgComparisonOperand.Between: NpgsqlCommand sqlCommand = std.cmd as NpgsqlCommand; sql += ApplyWhereSubOperations(std, query, comparisonData) + " BETWEEN "; sql += compareTo; if (comparisonData.ComparisonColumn.Name == null) { sqlCommand.Parameters.AddWithValue(compareTo, comparisonData.BetweenBegin); } if (comparisonData.ComparisonColumn2.Name == null) { IDynamicQuerySerializableInternal iColQuery = comparisonData.Column.Query as IDynamicQuerySerializableInternal; tgColumnMetadataCollection columns = (tgColumnMetadataCollection)iColQuery.Columns; compareTo = Delimiters.Param + columns[comparisonData.Column.Name].PropertyName + (++std.pindex).ToString(); sql += " AND " + compareTo; sqlCommand.Parameters.AddWithValue(compareTo, comparisonData.BetweenEnd); } else { sql += " AND " + Delimiters.ColumnOpen + comparisonData.ComparisonColumn2 + Delimiters.ColumnClose; } requiresParam = false; break; } if (requiresParam) { NpgsqlParameter p; if (comparisonData.Column.Name != null) { p = types[comparisonData.Column.Name]; p = Cache.CloneParameter(p); p.ParameterName = compareTo; p.Value = comparisonData.Value; if (needsStringParameter) { p.DbType = DbType.String; } } else { p = new NpgsqlParameter(compareTo, comparisonData.Value); } std.cmd.Parameters.Add(p); } } } return(sql); }