是否可以对数据库字段进行子字符串?

时间:2016-07-26 18:11:18

标签: sql vb.net datetime substring infragistics

在vb.net中,无论我尝试什么代码,我的ultragrid DateTime2列都不会格式化以显示日期。

所以,我想知道,是否可以使用代码从datetime2字段(称为dateStart)获取数据,然后对其进行子串,将其拆分为日期和时间之间的空格,然后只是在超网格列中显示字符串的日期部分?

用于填充网格的代码(我会在日期问题解决后再对其进行参数化)

 Public Sub loadIncidents()

    sql = "1=1"
    If ugClients.Selected.Rows.Count = 1 Then
        sql &= " AND clientName = '" & ugClients.Selected.Rows(0).Cells("Account_Code").Text & "'"
    End If

    sql = "SELECT supportID, clientName, dateStart, incidentProblem, incidentSolved FROM dbo.tblIncidents WHERE " & sql & " ORDER BY [supportID] ASC;"

    Dim cm As New OleDbCommand(sql, _con)
    Dim da As New OleDbDataAdapter(cm)
    Dim dt As New DataTable
    da.Fill(dt)
    ugHistory.DataSource = dt

End Sub

InitializeLayout的代码

Private Sub ugHistory_InitializeLayout(sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles ugHistory.InitializeLayout

    ugHistory.DisplayLayout.Bands(0).Columns("supportID").Header.Caption = "ID"
    ugHistory.DisplayLayout.Bands(0).Columns("clientName").Header.Caption = "Account Code"
    ugHistory.DisplayLayout.Bands(0).Columns("dateStart").Header.Caption = "Date"
    ugHistory.DisplayLayout.Bands(0).Columns("incidentProblem").Header.Caption = "Problem"
    ugHistory.DisplayLayout.Bands(0).Columns("incidentSolved").Header.Caption = "Solved?"

    With e.Layout.Override
        .CellClickAction = CellClickAction.RowSelect
        .AllowAddNew = AllowAddNew.No
        .AllowDelete = DefaultableBoolean.False
        .AllowUpdate = DefaultableBoolean.False
        .AllowColSizing = AllowColSizing.None
        .RowSelectors = DefaultableBoolean.False
    End With

End Sub

我希望这是有道理的!任何关于如何做到这一点的建议将不胜感激

3 个答案:

答案 0 :(得分:1)

您可以使用UltraGridColumn的Format属性。没有必要操纵您的数据,网格足够聪明,可以为您完成此操作。在InitializeLayout中添加例如:

ugHistory.DisplayLayout.Bands(0).Columns("dateStart").Format = "dd/M/yyyy"

答案 1 :(得分:0)

也许是演员或格式

Declare @dateStart DateTime = GetDate()

Select cast(@dateStart as Date)          -- Returns date of 2016-07-26
Select Format(@dateStart,'MM/dd/yyyy')   -- Returns string of 07/26/2016

答案 2 :(得分:-1)

This answer是最佳解决方案。使用SQL Server复制列及其内容,删除原始文件,然后删除。

请记住,您需要将列拖回原始位置,否则保存记录将成为问题,数据列错误,数据类型不匹配等