我在我的程序中有一段代碼,它應該在特定事件中提取最近一批的時間。然而我所得到的卻是一個空白的結果。我知道表中有相關數據,SQL Profiler顯示它正在執行。C#SQL查詢不產生數據
public void UpdateBeginStir()
{
string beginStir = null;
string connectionString = "(Omitted)";
string commandLine = "SELECT MAX(Event_Time) AS Time " +
"FROM dbo.Custom_EventLog " +
"WHERE Container_ID = @LOTNUMBR " +
"AND Event_ID = 1 " +
"GROUP BY BadgeNo, Container_ID, Event_ID";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(commandLine, connection))
{
command.Parameters.Add("@LOTNUMBR", SqlDbType.NChar, 50).Value = TextBoxLot.Text;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
beginStir = reader["Time"] as string;
break;
}
}
connection.Close();
}
MessageBox.Show(beginStir, "test", MessageBoxButton.OK);
LabelBeginStir.Content = beginStir;
}
如果執行從SSMS查詢,它返回的結果? –
所以當你執行 「SELECT MAX(EVENT_TIME)隨着時間的 FROM dbo.Custom_EventLog WHERE CONTAINER_ID = '無論LOTNUMBR是' 和事項標識= 1 GROUP BY BadgeNo,CONTAINER_ID,事項標識」,它給了預期的結果? TextBoxLot.Text的價值是什麼? –
當我在SSMS中執行查詢時,會得到最近一次的預期結果。當我在代碼中運行它時,由於消息框爲空,我得到一個空白結果。 –