2

특정 보고서를 인쇄 할 때 추적 할 수 있도록 datetimes 그룹을 저장하는 응용 프로그램을 작성하고 있습니다. 이 보고서는 datetimes가있는 두 번째 테이블의 정보로 구성됩니다. 첫 번째 테이블의 마지막 datetime 뒤에있는 레코드로 datagridview를 채우도록 보고서를 가져 오려고합니다.C# datetime 값과 SQL Server Compact 4 datetime 비교

첫 번째 테이블을 '배달 용지'라고하며,이 테이블은 과거 인쇄 날짜를 저장합니다. 두 번째 테이블은 'joblog'라고하며 이전 작업 항목의 레코드를 저장합니다.

프로그램을 실행하면 잘 작동하고 마지막 날짜 이후 모든 레코드가있는 gridview가 채워지지만 정제되지는 않습니다. 날짜가 아닌 날짜 이후에 채워집니다. 두 번째로 gridview를 채우는 쿼리가 필요합니다 ....

DateTime lastDeliveryDate; 

private void getLastDelivery() // Sets global variable lastDeliveryDate to the last timestamp entered in the deliverylog table 
{ 
    openLogConnection(); 

    try 
    { 
     command = new SqlCeCommand("SELECT TOP 1 * FROM deliverylog ORDER BY Id DESC", logConn); 

     drLogSet = command.ExecuteReader(); 
     while (drLogSet.Read()) 
     { 
      lastDeliveryDate = Convert.ToDateTime(drLogSet["Timestamp"]); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    finally 
    { 
     logConn.Close(); 
    } 
} 

private void populateGridView() 
{ 
    openLogConnection(); 

    try 
    { 
     command = new SqlCeCommand("SELECT * FROM joblog WHERE TimeStamp > @date", logConn); 
     command.Parameters.AddWithValue("@date", lastDeliveryDate); 

     dtLogSet = new DataTable(); 
     bsLogSet = new BindingSource(); 
     daLogSet = new SqlCeDataAdapter(command); 
     cbLogSet = new SqlCeCommandBuilder(daLogSet); 

     daLogSet.Fill(dtLogSet); 
     bsLogSet.DataSource = dtLogSet; 
     dataGridView1.DataSource = bsLogSet; 

     dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
    finally 
    { 
     logConn.Close(); 
    } 
} 

누구나이 작업 권한을 얻는 방법을 알고 계십니까? 두 테이블의 타임 스탬프를 datetime 데이터 형식으로 저장하고 다음 형식으로 저장합니다. "MM/dd/yyyy hh : mm : ss tt"

+0

'joblog' 테이블 정의를 게시 할 수 있습니까? – Xint0

+0

SQLite 또는 SQL Serve Compact Edition을 실제로 사용하고 있습니까? – Xint0

+0

SQL Server compact 4.0 –

답변

-1

날짜를 yyyyMMdd hh : mm : ss로 형식을 지정해야합니다.

lastDeliveryDate.toString ("yyyyMMdd hh : mm : ss").

이 postare에서 자세한 내용. How to compare sqlite TIMESTAMP values

+0

그건 잘못된 datetime 형식입니다 .... –

+0

그는 SQLite를 실행하지 않고 문자열 형식은 비교 및 ​​표시 및 구문 분석을위한 것이 아닙니다. –

0

AddWithValue 메서드를 사용하면 내부적으로 값을 변환하고 아마도 원하는 시간 정밀도를 잃어 버리는 것으로 생각됩니다.

var dateParameter = command.Parameters.Add("@date", SqlDbType.DateTime); 
dateParameter.Value = this.lastDeliveryDate; 

는 디버거를 사용하여 변수를 검사하여 매개 변수 값을 설정하기 전에 올바른 값이 있는지 확인합니다 : 대신, Parameters 수집의 Add(String, SqlDbType) 메서드 오버로드를 사용합니다.

그리고 당신은 적절한 정밀도를 보장하는 대신 > 연산자의 DATEDIFF SQL 함수를 사용하여 시도 할 수 :

lastDeliveryDate = Convert.ToDateTime(drLogSet["Timestamp"]); 

TimeStamp 필드를 가정 :

SELECT * FROM joblog WHERE DATEDIFF(second, @date, TimeStamp) > 0 
+0

실제로 add() 메서드는 더 이상 사용되지 않습니다 ... 나는 그것이 작동하도록했습니다. 처음에 datetime 값을 저장하는 방법이었고, 표준 시간을 사용하여 군사 시간으로 읽으려고했습니다 ...lol 수치는 어리석은 실수가 될 것입니다 .... –

+0

'Add (String, Object)'메소드 오버로드는 더 이상 사용되지 않고'AddWithValue (String, Object)'로 대체되었습니다. Add (String, SqlDbType) 메소드 오버로드는 더 이상 사용되지 않습니다. 나쁜 데이터 형식 불일치가 일반적인 실수라고 생각하지 마십시오. 그래서 디버깅하는 동안 매개 변수 값을 검사하는 것이 좋습니다. – Xint0

0

문제는 아마 이 라인 datetime 유형이면 다음을 사용해야합니다.

lastDeliveryDate = (DateTime) drLogSet["TimeStamp"]; 

TimeStamp 필드의 데이터 유형을 확인하십시오.