1

我在 C# MYsql 中使用。如果我在 MySql Workbench 上运行,我的查询可以工作,但是在 C# 中它不返回任何值也不会给出 ant 错误。在表名 databaseName 之前我在 Mysql 上使用只有一种不同的用法.tableName ,但在 C# 中我认为没有必要。这是不返回任何内容的查询的一部分。

编辑:已解决

"(select Lesson_Name from schedule  where Group_NO = (select Group_NO from sinif inner join student ON sinif.Group_ID=student.Group_ID where Student_Name=(?Student))"+
           " And Day_Name =(select Day_Name from day inner join date ON day.Day_ID=date.DayName where Date=(?Date))" +
           "And Lesson_Time= (select Lesson_Time from clock where Lesson_Time <= (?Time)order by Lesson_Time DESC limit 0, 1) " +
           " And Week_NO = (select Week_NO from week inner join date ON week.Week_ID=date.Week_ID where Date=(?Date)))

这里是用户单击按钮时执行的所有代码。

 private void check_B_Click(object sender, EventArgs e)
    {

        connection.Open();

       for (int i = 0; i < existingStudents.Count; i++)
        {
            MySqlCommand cmd1 = new MySqlCommand("select Student_Name,Student_Surname,Student_MacAddress from student  ", connection);
            MySqlCommand cmd2 = new MySqlCommand("insert into check_list (Student,Mac_Address,Date,Time,Lesson_Name)"+
           "values((?Student),(?MacAddress),(?Date),(?Time),"+
           "(select Lesson_Name from schedule  where Group_NO = (select Group_NO from sinif inner join student ON sinif.Group_ID=student.Group_ID where Student_Name=(?Student))"+
           " And Day_Name =(select Day_Name from day inner join date ON day.Day_ID=date.DayName where Date=(?Date))" +
           "And Lesson_Time= (select Lesson_Time from clock where Lesson_Time <= (?Time)order by Lesson_Time DESC limit 0, 1) " +
           " And Week_NO = (select Week_NO from week inner join date ON week.Week_ID=date.Week_ID where Date=(?Date))))", connection);

            MySqlParameter param1 = new MySqlParameter();
            param1.ParameterName = "?Student";
            reader = cmd1.ExecuteReader();
            if (reader.HasRows)
                while (reader.Read())
                {
                    if (reader["Student_MacAddress"].ToString() == existingStudentsMac[i].ToString())
                        param1.Value = reader["Student_Name" ]+" "+reader["Student_Surname"];
                }

            reader.Close();

            MySqlParameter param2 = new MySqlParameter();
            param2.ParameterName = "?MacAddress";
            param2.Value = existingStudentsMac[i];

            MySqlParameter param3 = new MySqlParameter();
            param3.ParameterName = "?Date";
            param3.Value = DateTime.Today.Date;

            MySqlParameter param4 = new MySqlParameter();
            param4.ParameterName = "?Time";
            param4.Value = DateTime.Now.ToString("HH:mm");


            cmd2.Parameters.Add(param1);
            cmd2.Parameters.Add(param2);
            cmd2.Parameters.Add(param3); 
            cmd2.Parameters.Add(param4);

           cmd2.ExecuteNonQuery();

        }


        connection.Close();
        MessageBox.Show("Sucsess :)");

    }
4

1 回答 1

0

经过长时间的实验))我发现了我的错误。MySqlParameter param1 具有 Student_Name 的值,我还添加了 Student_Surname 但在我的表中只有 Student_Name,因此它们不匹配。也是 param4.ParameterName = "?Time";返回系统时间,但使用不同的时间格式。我将其更改为param4.Value=DateTime.Now.ToString("hh:mm"); Alson 我认识到不是Lesson_Time <= (?Time) 使用 已Lesson_Time <= ('?Time')解决的问题。我认为在我的表中我将课程时间定义为字符串并且 ('?Time') 返回字符串。

于 2010-03-31T00:13:53.747 回答