使用insert自动插入datetime

时间:2014-08-28 14:10:23

标签: asp.net .net sql-server telerik radgrid

我有一个带有下面设置的radgrid。我能够在表格中正确插入新行,但Time_Stamp列仍显示为NULL。数据库中的数据类型为datetime。我尝试在Time_Stamp查询中加入Insert,但仍然在另一方获得NULL

<telerik:RadGrid ID="Grid_ValueCard" runat="server" DataSourceID="SqlValueCard" AutoGenerateEditColumn="True" AllowAutomaticUpdates="True" Width="750px"  Skin="WebBlue" AllowAutomaticInserts="True" CellSpacing="0" GridLines="None">
    <MasterTableView AutoGenerateColumns="False" AllowAutomaticUpdates="True" DataSourceID="SqlValueCard" CommandItemDisplay="Top">
        <CommandItemSettings ShowCancelChangesButton="True" ShowSaveChangesButton="True" />
        <Columns>
            <telerik:GridBoundColumn DataField="ProviderID" DataType="System.Int32" FilterControlAltText="Filter ProviderID column" HeaderText="ProviderID" SortExpression="ProviderID" UniqueName="ProviderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="AdditionalReports" DataType="System.Byte" FilterControlAltText="Filter AdditionalReports column" HeaderText="AdditionalReports" SortExpression="AdditionalReports" UniqueName="AdditionalReports">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SurveyCompleted" DataType="System.Byte" FilterControlAltText="Filter SurveyCompleted column" HeaderText="SurveyCompleted" SortExpression="SurveyCompleted" UniqueName="SurveyCompleted">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="UniqueReportRequests" DataType="System.Byte" FilterControlAltText="Filter UniqueReportRequests column" HeaderText="UniqueReportRequests" SortExpression="UniqueReportRequests" UniqueName="UniqueReportRequests">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Time_Stamp" DataType="System.DateTime" FilterControlAltText="Filter Time_Stamp column" HeaderText="Time_Stamp" SortExpression="Time_Stamp" UniqueName="Time_Stamp">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

这是我的SqlDataSource

<asp:SqlDataSource ID="SqlValueCard" runat="server" ConnectionString="<%$ ConnectionStrings:RTMD_MSConnectionString %>" SelectCommand="SELECT TOP 1 ProviderID, AdditionalReports, SurveyCompleted, UniqueReportRequests, Time_Stamp FROM ProviderValueCard"
     InsertCommand="INSERT INTO [ProviderValueCard] ([ProviderID], [AdditionalReports], [UniqueReportRequests], [SurveyCompleted]) VALUES (@ProviderID, @AdditionalReports, @UniqueReportRequests, @SurveyCompleted)">
    <InsertParameters>
        <asp:Parameter Name="ProviderID" />
        <asp:Parameter Name="AdditionalReports" />
        <asp:Parameter Name="UniqueReportRequests" />
        <asp:Parameter Name="SurveyCompleted" />
    </InsertParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

您可以将TIME_STAMP列设置为默认约束,以便在插入新行时自动设置当前日期和时间:

ALTER TABLE dbo.ProviderValueCard
ADD CONSTRAINT DF_ProviderValueCard_Time_Stamp
   DEFAULT (SYSDATETIME()) FOR TIME_STAMP

现在,每次插入一行并且前端没有提供任何值时,SQL Server将填写当前日期&amp;自动进入TIME_STAMP的时间: - )