SQL 参数和整数值
SQL Parameter And int Value
我使用 ado.net 中的查询来读取要在数据网格中显示的信息,但问题是我的 SQL 参数(与 Teacher_Id 相关)具有一个问题,不允许显示信息。
你的解决方案是什么?
我记得,没有报错,只是datagrid中没有显示信息!
我在 SQL 中执行了这段代码,它工作正常,但我不知道 ado.net 有什么问题?
我之前在 LIKE 中使用了相同的代码,它在 ado.net
上运行正常
我的代码是:
public DataTable Read_For_Properties(string University_Name, int Teacher_Id, string Year, string Term, string Lesson_Code, string Houre, string ClassRoom_Name)
{
SqlConnection SQL_Con = new SqlConnection(@"Data Source=SQLSERVER\SQLSERVER;Initial Catalog=DB1;Integrated Security=True");
SqlCommand SQL_Com = new SqlCommand(@"select
Turns.Id,
case Humen.Discriminator when 'Student' then Humen.Name end as 'Name',
case Humen.Discriminator when 'Student' then Humen.Family end as 'Family',
Turns.Score as 'Score'
from Turns
inner join Lessons on Lessons.Id = Turns.Lesson_Id
inner join Universities on Universities.Id = Turns.University_Id
inner join Class_Room on Class_Room.Id = Turns.Class_Room_Id
inner join Humen on Humen.Id = Turns.Student_Id
where Class_Room.Name = @P1 AND Turns.Houre = @P2 AND Lessons.Lesson_Code = @P3 AND Turns.Term = @P4 AND Turns.Year = @P5 AND
Turns.Teacher_Id = @P6 AND Universities.Name = @P7");
SQL_Com.Connection = SQL_Con;
SQL_Com.Parameters.Add(new SqlParameter("@P1", "%" + ClassRoom_Name + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P2", "%" + Houre + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P3", "%" + Lesson_Code + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P4", "%" + Term + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P5", "%" + Year + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P6", Teacher_Id ));
SQL_Com.Parameters.Add(new SqlParameter("@P7", "%" + University_Name + "%"));
DataSet DS = new DataSet();
SqlDataAdapter SQL_DA = new SqlDataAdapter();
SQL_DA.SelectCommand = SQL_Com;
SQL_DA.Fill(DS);
return DS.Tables[0];
}
解决方案
我无意中发现解决方案很简单,只需删除“%”即可!
符号“%”显然用于 LIKE 命令
我使用 ado.net 中的查询来读取要在数据网格中显示的信息,但问题是我的 SQL 参数(与 Teacher_Id 相关)具有一个问题,不允许显示信息。 你的解决方案是什么?
我记得,没有报错,只是datagrid中没有显示信息!
我在 SQL 中执行了这段代码,它工作正常,但我不知道 ado.net 有什么问题?
我之前在 LIKE 中使用了相同的代码,它在 ado.net
上运行正常我的代码是:
public DataTable Read_For_Properties(string University_Name, int Teacher_Id, string Year, string Term, string Lesson_Code, string Houre, string ClassRoom_Name)
{
SqlConnection SQL_Con = new SqlConnection(@"Data Source=SQLSERVER\SQLSERVER;Initial Catalog=DB1;Integrated Security=True");
SqlCommand SQL_Com = new SqlCommand(@"select
Turns.Id,
case Humen.Discriminator when 'Student' then Humen.Name end as 'Name',
case Humen.Discriminator when 'Student' then Humen.Family end as 'Family',
Turns.Score as 'Score'
from Turns
inner join Lessons on Lessons.Id = Turns.Lesson_Id
inner join Universities on Universities.Id = Turns.University_Id
inner join Class_Room on Class_Room.Id = Turns.Class_Room_Id
inner join Humen on Humen.Id = Turns.Student_Id
where Class_Room.Name = @P1 AND Turns.Houre = @P2 AND Lessons.Lesson_Code = @P3 AND Turns.Term = @P4 AND Turns.Year = @P5 AND
Turns.Teacher_Id = @P6 AND Universities.Name = @P7");
SQL_Com.Connection = SQL_Con;
SQL_Com.Parameters.Add(new SqlParameter("@P1", "%" + ClassRoom_Name + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P2", "%" + Houre + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P3", "%" + Lesson_Code + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P4", "%" + Term + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P5", "%" + Year + "%"));
SQL_Com.Parameters.Add(new SqlParameter("@P6", Teacher_Id ));
SQL_Com.Parameters.Add(new SqlParameter("@P7", "%" + University_Name + "%"));
DataSet DS = new DataSet();
SqlDataAdapter SQL_DA = new SqlDataAdapter();
SQL_DA.SelectCommand = SQL_Com;
SQL_DA.Fill(DS);
return DS.Tables[0];
}
解决方案
我无意中发现解决方案很简单,只需删除“%”即可! 符号“%”显然用于 LIKE 命令