몇 가지 날짜 제약 조건을 사용하여 쿼리를 실행하고 있습니다. 아래의 코드를 하드 코드 된 날짜와 매개 변수없이 실행하면 예상 한 결과 (단순한 정수)가 나타납니다. 그러나 매개 변수를 사용하려고하면 빈 결과 집합이 생깁니다. 오류가없고 결과가 없습니다.C#에서 OdbcConnection과 작동하는 매개 변수를 가져올 수 없습니다.
log("Connecting to SQL Server...");
string connectionString = "DSN=HSBUSTEST32;";
string queryString = "SELECT COUNT(*) FROM Table WHERE myDateTime >= '?' AND myDateTime < '?'";
//string queryString = "SELECT COUNT(*) FROM Table WHERE myDateTime >= '@startDate' AND myDateTime < '@endDate'";
string startDate = "2016-08-23";
string endDate = "2016-08-24";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
OdbcCommand command = new OdbcCommand(queryString, connection);
command.Parameters.AddWithValue("startDate",startDate);
command.Parameters.AddWithValue("endDate", endDate);
//command.Parameters.Add("startDate", OdbcType.VarChar).Value = "2016-08-23";
//command.Parameters.Add("endDate", OdbcType.VarChar).Value = "2016-08-24";
try
{
connection.Open();
OdbcDataReader reader = command.ExecuteReader();
while (reader.Read())
{
log(reader[0].ToString());
}
reader.Close();
}
catch (Exception ex)
{
log(ex.Message);
}
}
당신은 내가 ?
자리 표시자를 사용하여뿐만 아니라 모두 명명 된 매개 변수를 시도했습니다 볼 수 있듯이. 나는 또한 매개 변수를 추가하는 두 가지 다른 방법을 시도했다. Add()
과 AddWithValue()
, 진실로 나는 그 차이를 이해하지 못한다.
빈 결과의 원인은 무엇입니까?
당신이 당신의 쿼리에서 작은 따옴표를 제거하려고 했습니까? 또한 내가 매개 변수에 addr 데이터 형식에 조언 것입니다. –
... 작은 따옴표를 제거하면 해결됩니다. 설명 해주십시오. 그것은 매개 변수에 함축되어 있습니까?/좌절 –
그것은 데이터 형식이 문자열이기 때문에 그 일을합니까? –