将 SQL 服务器 DataRequest class 转换为 NpgSQL

Converting SQL Server DataRequest class to NpgSQL

正在尝试转换 SQL 服务器数据请求 class 以便与 PostgreSQL 一起使用。卡在这段代码中的 DBTypes 上。任何人都可以帮助我进行此转换吗?

/// <summary>
/// Creates a <see cref="SqlParameter"/> from the supplied parameters.
/// </summary>
/// <param name="name">The parameter name (<see cref="SqlParameter.ParameterName"/>)</param>
/// <param name="type">The SQL type (<see cref="SqlParameter.SqlDbType"/>)</param>
/// <param name="value">The parameter value (<see cref="SqlParameter.Value"/>)</param>
/// <param name="size">The parameter size (<see cref="SqlParameter.Size"/>)</param>
/// <param name="direction">The parameter direction (<see cref="SqlParameter.Direction"/>)</param>
/// <param name="sqlTypeName">The parameter SQL type (<see cref="SqlParameter.TypeName"/>). Applies to <see cref="SqlDbType.Structured"/> only.</param>
/// <returns>A <see cref="SqlParameter"/> object</returns>
protected SqlParameter NewSqlParameter(string name, SqlDbType type = SqlDbType.VarChar, object value = null, int? size = null, ParameterDirection direction = ParameterDirection.Input, string sqlTypeName = null)
{
    var parameter = new SqlParameter
    {
        SqlDbType = type,
        ParameterName = name,
        Value = value ?? DBNull.Value,
        Direction = direction
    };

    // Structured params i.e. DataTables require the TypeName defined in the SQL database
    if (type == SqlDbType.Structured && !string.IsNullOrEmpty(sqlTypeName))
    {
        parameter.TypeName = sqlTypeName;
    }

    // Only set the size if it is explicitly passed. Otherwise, we could get truncated at 0.
    if (size.HasValue)
    {
        parameter.Size = size.Value;
    }
    return parameter;
}

这是我对所指出问题的半心半意的尝试:

/// <summary>
/// Creates a <see cref="SqlParameter"/> from the supplied parameters.
/// </summary>
/// <param name="name">The parameter name (<see cref="NpgsqlParameter.ParameterName"/>)</param>
/// <param name="type">The SQL type (<see cref="NpgsqlParameter.NpgsqlDbType"/>)</param>
/// <param name="value">The parameter value (<see cref="NpgsqlParameter.Value"/>)</param>
/// <param name="size">The parameter size (<see cref="NpgsqlParameter.Size"/>)</param>
/// <param name="direction">The parameter direction (<see cref="NpgsqlParameter.Direction"/>)</param>
/// <param name="sqlTypeName">The parameter SQL type (<see cref="NpgsqlParameter.DataTypeName"/>). Applies to <see cref="SqlDbType.Structured"/> only.</param>
/// <returns>A <see cref="SqlParameter"/> object</returns>protected NpgsqlParameter NewSqlParameter(string name, DbType type =  DbType.String, object value = null, int? size = null, protected NpgsqlParameter NewSqlParameter(string name, NpgsqlDbType type = NpgsqlDbType.Varchar, object value = null, int? size = null, ParameterDirection direction = ParameterDirection.Input, string sqlTypeName = null)
{
    var parameter = new NpgsqlParameter
    {
        NpgsqlDbType = type,
        ParameterName = name,
        Value = value ?? DBNull.Value,
        Direction = direction
    };

    //********************************************************************************************
    // NOTE: There is no DbType of Structured in the NpgSql package. Not sure what to use here
    //********************************************************************************************

    // Structured params i.e. DataTables require the TypeName defined in the SQL database
    if (type == NpgsqlTypes.DbType.Structured && !string.IsNullOrEmpty(sqlTypeName))
    {
        parameter.DataTypeName = sqlTypeName;
    }

    // Only set the size if it is explicitly passed. Otherwise, we could get truncated at 0.
    if (size.HasValue)
    {
        parameter.Size = size.Value;
    }

    return parameter;
}

以上似乎使用了 SQL 服务器 user-defined 类型(UDT); PostgreSQL 确实有 composite types (and the Npgsql library supports them well),但不同数据库的工作方式不同。

假设您需要使用 user-defined 类型,您必须通过上面的链接熟悉 PostgreSQL 复合类型的工作原理。