gpt4 book ai didi

c# - 为什么我的 SqlParameter 没有被识别?

转载 作者:行者123 更新时间:2023-12-05 08:11:03 25 4
gpt4 key购买 nike

我收到这个错误:

System.Data.SqlClient.SqlException: 'The parameterized query '(@inputKarakterSoort varchar(8000))SELECT TOP 2 * FROM Karakter ' expects the parameter '@inputKarakterSoort', which was not supplied.

但我想我已经给出了参数化查询。

我的 DAL:

public class SortedKarakterContext : ISortedKarakterContext
{
public string conn { get; set; }

public SortedKarakterContext()
{
this.conn = "MYCONNECTIONSTRING";
}

private SqlConnection GetConnection()
{
return new SqlConnection(conn);
}

public IEnumerable<KarakterDTO> GetSortedKarakters(string inputKarakterSoort)//User input was not supplied?? Hoe veranderen
{
using (SqlConnection connection = GetConnection())
{
connection.Open();
var command = new SqlCommand("SELECT TOP 2 * FROM Karakter WHERE KarakterSoort = @inputKarakterSoort ORDER BY NEWID();", connection);
command.Parameters.Add("@inputKarakterSoort", SqlDbType.VarChar).Value = inputKarakterSoort;
var reader = command.ExecuteReader();
var sortedKarakters = new List<KarakterDTO>();

while (reader.Read())
{
var karakter = new KarakterDTO
{
KarakterId = (int)reader["KarakterId"],
KarakterSoort = reader["KarakterSoort"]?.ToString(),
KarakterNaam = reader["KarakterNaam"]?.ToString()
};

sortedKarakters.Add(karakter);
}

return sortedKarakters;
}
}
}
}

我以为我是在这一行做的:

   command.Parameters.Add("@inputKarakterSoort", SqlDbType.VarChar).Value = inputKarakterSoort;

我确信 inputKaraktersoort 有一个值,因为当我在那里放置一个断点时,它说它有正确的值。

错误在这一行:

var reader = command.ExecuteReader();

谁能帮帮我?

根据请求的命令值和inputKarakterSoort: Values

当我输入这个时:

 inputKarakterSoort = "Defensive"

以上:connection.Open()

它确实有效,但我想在查询中使用我的参数。

最佳答案

我尝试了 sgmoore 的评论并实现了它。现在可以使用了。

现在这是我的 DAL:

public class SortedKarakterContext : ISortedKarakterContext
{
public string conn { get; set; }

public SortedKarakterContext()
{
this.conn = "MYCONNECTIONSTRING";
}

private SqlConnection GetConnection()
{
return new SqlConnection(conn);
}

public IEnumerable<KarakterDTO> GetSortedKarakters(string inputKarakterSoort)
{
using (SqlConnection connection = GetConnection())
{
connection.Open();
var command = new SqlCommand("SELECT TOP 2 * FROM Karakter WHERE KarakterSoort = @inputKarakterSoort ORDER BY NEWID();", connection);
if (inputKarakterSoort == null)
command.Parameters.Add("@inputKarakterSoort", SqlDbType.VarChar).Value = DBNull.Value;
else
command.Parameters.Add("@inputKarakterSoort", SqlDbType.VarChar).Value = inputKarakterSoort;
var reader = command.ExecuteReader();
var sortedKarakters = new List<KarakterDTO>();

while (reader.Read())
{
var karakter = new KarakterDTO
{
KarakterId = (int)reader["KarakterId"],
KarakterSoort = reader["KarakterSoort"]?.ToString(),
KarakterNaam = reader["KarakterNaam"]?.ToString()
};

sortedKarakters.Add(karakter);
}

return sortedKarakters;
}
}
}
}

关于c# - 为什么我的 SqlParameter 没有被识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64772693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com