SQL异常,缺少参数化查询。

6

你好,我正在尝试通过我正在构建的C# WinForm将一些值写入我的SQL。但我遇到了一个无法解决的错误。

我正在调用这个函数:

SQL.insertIntoChildTable(Convert.ToInt32(child_IDTextBox.Text), 
                         child_FirstNameTextBox.Text, 
                         child_LastNameTextBox.Text, 
                         Convert.ToInt32(parent1IDTextBox.Text), 
                         Convert.ToInt32(parent2IDTextBox.Text), 
                         birthdayDateTimePicker.Value.ToShortDateString(), 
                         age.ToString(), 
                         null, null, null, null, null, 
                         child_disciplineTextBox.Text, child_NotesTextBox.Text);

从这个开始:

public static void insertIntoChildTable(int childID, string firstName, string lastName, int parent1ID, int parent2ID, string birthdate, string age, string lastCheckedInTime, string lastCheckedInBy, string lastCheckOutTime, string lastCheckedOutBy, string ageGroup, string disciplineNotes, string otherNotes)
{
   try
   {
       using (SqlConnection connection = new SqlConnection(Global.connectionString))
       using (SqlCommand command = connection.CreateCommand())
       {
          command.CommandText = "INSERT INTO ProjectList (ChildID, FirstName, LastName, Parent1ID, Parent2ID, Birthdate, Age, LastCheckedInTime, LastCheckedInBy, LastCheckOutTime, LastCheckedOutBy, AgeGroup, DisciplineNotes, OtherNotes) VALUES (@childID, @firstName, @lastName, @parent1ID, @parent2ID, @birthdate, @age, @lastCheckedInTime, @lastCheckedInBy, @lastCheckOutTime, @lastCheckedOutBy, @ageGroup, @disciplineNotes, @otherNotes)";

          command.Parameters.AddWithValue("@childID", childID);
          command.Parameters.AddWithValue("@firstName", firstName);
          command.Parameters.AddWithValue("@lastName", lastName);
          command.Parameters.AddWithValue("@parent1ID", parent1ID);
          command.Parameters.AddWithValue("@parent2ID", parent2ID);
          command.Parameters.AddWithValue("@birthdate", birthdate);
          command.Parameters.AddWithValue("@age", age);
          command.Parameters.AddWithValue("@lastCheckedInTime", lastCheckedInTime);
          command.Parameters.AddWithValue("@lastCheckedInBy", lastCheckedInBy);
          command.Parameters.AddWithValue("@lastCheckOutTime", lastCheckOutTime);
          command.Parameters.AddWithValue("@lastCheckedOutBy", lastCheckedOutBy);
          command.Parameters.AddWithValue("@ageGroup", ageGroup);
          command.Parameters.AddWithValue("@disciplineNotes", disciplineNotes);
          command.Parameters.AddWithValue("@otherNotes", otherNotes);

          connection.Open();
          command.ExecuteNonQuery();
       }
    }
    catch (SqlException ex)
    {
       Console.WriteLine(ex.Message);
    }
}

获取到以下错误信息:

在 System.Data.dll 中发生了 'System.Data.SqlClient.SqlException' 类型的第一次机会异常
参数化查询'(@childID int,@firstName nvarchar(4),@lastName nvarchar(5),@pare' 需要参数 '@lastCheckedInTime', 但未提供该参数。

有什么想法?


1
你可能想要明确指定命令的 CommandType = CommandType.Text(这不一定是问题的原因,但是这是一个好的做法)。 - kaj
1个回答

12

我猜测它是null。为 null 的参数不会被添加。它需要是 DBNull.Value。您可以通过为每个参数添加 ?? DBNull.Value 来实现这一点。很蠢,我知道:

command.Parameters.AddWithValue("@lastCheckInTime",
      lastCheckInTime ?? DBNull.Value);

对于每个参数; 或者之后循环遍历它们,将任何null修正为DBNull.Value

foreach(var param in command.Parameters) {
    if(param.Value == null) param.Value = DBNull.Value;
}
作为一个副产品 - 将它们全部作为 string 的可能性非常小;难道不应该是 DateTimeDateTime? 吗?

嗯,null 在 SQL 中的作用不同。谢谢。我正在将所有内容转换为文本,因为我只是想让这个东西运行起来。将值强制转换为正确的类型会在之后进行,我只需要更多的经验。再次感谢。 - ikathegreat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接