gpt4 book ai didi

c# - MySQL :syntax using in C#

转载 作者:行者123 更新时间:2023-11-30 23:41:39 26 4
gpt4 key购买 nike

我在 C# MYsql 中使用。如果我在 MySql Workbench 上运行,我有一个有效的查询,但在 C# 中它不返回任何值,也不会给出 ant 错误。我之前在 Mysql 上使用的只有一种不同表名 databaseName.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 :)");

}

最佳答案

经过长时间的实验 )) 我发现了我的错误。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')解决了问题。我认为在我的表中我将类(class)时间定义为字符串并且 ('?Time') 返回字符串。

关于c# - MySQL :syntax using in C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2549029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com