在sql studio上运行的查询,但不适用于C#

时间:2015-07-30 10:35:21

标签: c# sql

我有这个查询,当我在Sql studio上执行它时我得到了结果:

select TeamName, count(*) 
  from NetHoz_Decision
  inner join NetHoz_Case on NetHoz_Decision.CaseDisplayIdentifier = NetHoz_Case.CaseDisplayIdentifier 
  inner join maintik on MainTik.Counter = NetHoz_Case.TikCounter
  inner join Teams on MainTik.TeamCounter = Teams.Counter
  where NetHoz_Decision.DecisionStatusChangeDate between '2015-07-23' and '2015-07-28'
  group by TeamName

但是当我在C#SqlCommand上执行它时,我得不到任何结果。 dataSet为空,我没有收到任何错误。 我认为问题可能是在日期。 我转换它吗?

String date1 = String.Format("{0:yyyy-MM-dd}", dateTimePicker1.Value);
String date2 = String.Format("{0:yyyy-MM-dd}", dateTimePicker2.Value.AddDays(1));

1 个答案:

答案 0 :(得分:1)

使用SqlCommandtype-safe parameters

const string query = "
select TeamName, count(*) 
 from NetHoz_Decision
 inner join NetHoz_Case on NetHoz_Decision.CaseDisplayIdentifier = NetHoz_Case.CaseDisplayIdentifier 
 inner join maintik on MainTik.Counter = NetHoz_Case.TikCounter
 inner join Teams on MainTik.TeamCounter = Teams.Counter
 where NetHoz_Decision.DecisionStatusChangeDate between @from and @to
 group by TeamName
";

SqlCommand command = new SqlCommand(query);
command.Parameters.AddWithValue("@from", dateTimePicker1.Value);
command.Parameters.AddWithValue("@to", dateTimePicker2.Value.AddDays(1));