C# 在select语句中传递参数时npgsql不返回行
我正在使用中提供的以下代码。当我的查询字符串为C# 在select语句中传递参数时npgsql不返回行,c#,postgresql,npgsql,C#,Postgresql,Npgsql,我正在使用中提供的以下代码。当我的查询字符串为Select*from public.accounts时,我可以检索数据,但当条件在where子句中时,此函数不会返回数据 我在上看到了其他答案,比如在AddWithValue函数中传递命令参数,但这不会为我返回任何信息 查询Select*from public.accounts,其中ext_auth0_user_id='github | 42357689'在我直接在pgAdmin中运行时确实返回数据,因此我假设我的某些格式错误 va
Select*from public.accounts
时,我可以检索数据,但当条件在where子句中时,此函数不会返回数据
我在上看到了其他答案,比如在AddWithValue
函数中传递命令参数,但这不会为我返回任何信息
查询Select*from public.accounts,其中ext_auth0_user_id='github | 42357689'
在我直接在pgAdmin中运行时确实返回数据,因此我假设我的某些格式错误
var userId = "github|42357689";
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("Select * from public.accounts where ext_auth0_user_id = @userId", conn);
// Retrieve all rows
using (cmd)
{
cmd.Parameters.AddWithValue("@userId", userId);
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
dataTable.Load(reader);
}
}
}
}
npgsqlparamuserid=cmd.Parameters.Add(“userId”,NpgsqlTypes.NpgsqlDbType.Varchar,20) paramUserId.Direction=ParameterDirection.Input; paramUserId.value=userId npgsqlparamanotherid=cmd.Parameters.Add(“anotherId”,NpgsqlTypes.NpgsqlDbType.Integer) paramAnotherId.Direction=参数Direction.Input;
paramAnotherId.value=anotherId 您正在将
ext\u auth0\u user\u id
与字符串'@userId'
进行比较,而不是将@userId
参数的值进行比较。删除引号。它可能是单引号(“@userId”)。单引号表示文本和数据库可能有整数。删除单引号。@很遗憾,在删除参数周围的单引号后,我仍然没有得到任何数据。更新了OPT,这导致CS1503参数2:无法从'string'转换为'NpgsqlTypes.NpgsqlDbType'
。将字符串解析为NpgsqlDbType的任何方法?PgSqlParameter paramUserId=cmd.Parameters.Add(“userId”,PgSqlType.VarChar,20);paramUserId.value=userId;对不起,我很久没用了postgresql@Shapka更新你的答案,这样更正就不会隐藏在评论中。此外,OP使用的是Npgsql,而不是dotConnect。此解决方案适用于文本列。然而,它不适用于字符变化列,这是我所追求的。。。