2014-05-03 10 views
36

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(); 
     } 


    } 
+1

pan zapomniał umieścić '@' jako pierwszego znaku nazwy każdego parametru. –

+0

@AndrewMorton: Dlaczego komunikat o błędzie narzeka na '@ units' zamiast" @ Name ", które pojawia się jako pierwsze? –

+0

@ O.R.Mapper Przypuszczam, że nie musi ich szukać w kolejności, w jakiej są dostarczane. –

Odpowiedz

57

Spróbuj kod:

SqlParameter unitsParam = command.Parameters.AddWithValue("@units", units); 
if (units == null) 
{ 
    unitsParam.Value = DBNull.Value; 
} 

Musisz sprawdzić wszystkie inne parametry dla wartości pustej. Jeśli wartość jest zerowa, musisz podać wartość DBNull.Value.

+1

Nie sądzę, że może to być wartość pusta, ponieważ OP ma taki parametr 'string units =" N \\ A "' – meda

+3

Może, ponieważ możesz wywołać metodę jak this 'insertType (null, null, null, null, null, null);' – Denis

+0

Problem rozwiązany! Będę edytować moją wiadomość za pomocą rozwiązania. Dziękuję wam wszystkim! – Yogevnn

18

Oto sposób używając operatora null koalescencyjny:

cmd.Parameters.AddWithValue("@units", units ?? (object)DBNull.Value); 
cmd.Parameters.AddWithValue("@range", range ?? (object)DBNull.Value); 
cmd.Parameters.AddWithValue("@scale", scale ?? (object)DBNull.Value); 
cmd.Parameters.AddWithValue("@description", description ?? (object)DBNull.Value); 

lub uzyskać więcej ścisłej kontroli typu:

cmd.Parameters.Add("@units", SqlDbType.Int).Value = units ?? (object)DBNull.Value; 
cmd.Parameters.Add("@range", SqlDbType.Int).Value = range ?? (object)DBNull.Value; 
cmd.Parameters.Add("@scale", SqlDbType.Int).Value = scale ?? (object)DBNull.Value; 
cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = description ?? (object)DBNull.Value; 

Operator być przykuty:

int?[] a = { null, null, 1 }; 
Console.WriteLine(a[0] ?? a[1] ?? a[2]); 
0

To jest metoda do ponownego wykorzystania z wieloma parametrami:

public void NullorEmptyParameter(QC.SqlCommand command, string at, string value) 
    { 
     if (String.IsNullOrEmpty(value)) 
     { 
      command.Parameters.AddWithValue(at, DBNull.Value); 
     } 
     else 
      command.Parameters.AddWithValue(at, value); 
    } 

a następnie można używać go tak wielu poleceń i params:

NullorEmptyParameter(command, "@Idea_ID", Idea_ID); 
    NullorEmptyParameter(command, "@Opportunities_or_Idea", Opportunities_or_Idea); 
Powiązane problemy