自动插入与插入日期时间(Automatically inserting datetime with

2019-10-20 20:08发布

我有以下设置一个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>

Answer 1:

你可以设置你的TIME_STAMP列有在插入新行会自动设置为当前日期和时间的默认约束

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

现在,每次插入某一行没有值从前端提供,SQL Server将在当前日期和时间为填补TIME_STAMP自动将:-)



文章来源: Automatically inserting datetime with insert