Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# sql异常,缺少参数化查询_C#_Sql - Fatal编程技术网

C# sql异常,缺少参数化查询

C# sql异常,缺少参数化查询,c#,sql,C#,Sql,您好,我正试图通过我正在构建的C#WinForm将一些值写入我的SQL。我遇到了一个无法解决的错误 我称之为: SQL.insertIntoChildTable(Convert.ToInt32(child_IDTextBox.Text), child_FirstNameTextBox.Text, child_LastNameTextBox.Text,

您好,我正试图通过我正在构建的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”,未提供该时间


有什么想法吗?

我猜它是
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;
}

作为一件附带的事情,将这些都设置为
字符串听起来不太可能;应该是
DateTime
DateTime?
,否?

认为您希望将command.CommandType=CommandType.Text指定为显式(不一定是问题的原因,但良好的做法).yup
null
在SQL中的工作方式不同。谢谢。我正在将所有内容转换为文本,因为我正在尝试启动并运行此程序。将值转换为正确的类型会出现,我只需要更多经验。再次感谢。