Dostaję ten wyjątek:Zapytanie parametryzowane ..... oczekuje parametru '@units', który nie został dostarczony
Zapytanie parametryzowane „(@name nvarchar (8), @ typ nvarchar (8) @ jednostek nvarchar (4000), @ rang”oczekuje parametr '@units', które nie zostały dostarczone
My kod wprowadzającej.
public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
command.Connection = connection;
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@type", type);
command.Parameters.AddWithValue("@units", units);
command.Parameters.AddWithValue("@range", range);
command.Parameters.AddWithValue("@scale", scale);
command.Parameters.AddWithValue("@description", description);
command.Parameters.AddWithValue("@guid", guid);
return (int)command.ExecuteScalar();
}
}
wyjątek niespodzianka, ponieważ używam AddWithValue
i upewnij się, że dodałem domyślne parametry dla funkcji.
rozwiązany:
Problem polegał na tym, że niektóre parametry gdzie pustych Strings (które zastępują domyślne)
Jest to kod pracy:
public int insertType(string name, string type, string units = "N\\A", string range = "N\\A", string scale = "N\\A", string description = "N\\A", Guid guid = new Guid())
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO Type(name, type, units, range, scale, description, guid) OUTPUT INSERTED.ID VALUES (@Name, @type, @units, @range, @scale, @description, @guid) ";
command.Connection = connection;
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@type", type);
if (String.IsNullOrEmpty(units))
{
command.Parameters.AddWithValue("@units", DBNull.Value);
}
else
command.Parameters.AddWithValue("@units", units);
if (String.IsNullOrEmpty(range))
{
command.Parameters.AddWithValue("@range", DBNull.Value);
}
else
command.Parameters.AddWithValue("@range", range);
if (String.IsNullOrEmpty(scale))
{
command.Parameters.AddWithValue("@scale", DBNull.Value);
}
else
command.Parameters.AddWithValue("@scale", scale);
if (String.IsNullOrEmpty(description))
{
command.Parameters.AddWithValue("@description", DBNull.Value);
}
else
command.Parameters.AddWithValue("@description", description);
command.Parameters.AddWithValue("@guid", guid);
return (int)command.ExecuteScalar();
}
}
pan zapomniał umieścić '@' jako pierwszego znaku nazwy każdego parametru. –
@AndrewMorton: Dlaczego komunikat o błędzie narzeka na '@ units' zamiast" @ Name ", które pojawia się jako pierwsze? –
@ O.R.Mapper Przypuszczam, że nie musi ich szukać w kolejności, w jakiej są dostarczane. –