Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用存储过程从数据库中提取数据时如何从DateTime中删除时间_C#_Asp.net_Sql Server_Stored Procedures_Ado.net - Fatal编程技术网

C# 使用存储过程从数据库中提取数据时如何从DateTime中删除时间

C# 使用存储过程从数据库中提取数据时如何从DateTime中删除时间,c#,asp.net,sql-server,stored-procedures,ado.net,C#,Asp.net,Sql Server,Stored Procedures,Ado.net,我有一个数据库表托管在SQL Server上 该问题包含一个“日期”类型的列。使用“我的存储过程”从数据库中提取数据时,将显示日期,并在日期后附加12:00:00 AM的时间 例如,日期“30/10/2015”将作为单独的日期存储在数据库中,但当它添加到gridview时,如上所述,时间12:00:00AM将附加到它。我只想在gridview中显示日期,而不是时间 以下是将数据库中的数据绑定到gridview的代码: private void BindGridEvent() { stri

我有一个数据库表托管在SQL Server上

该问题包含一个“日期”类型的列。使用“我的存储过程”从数据库中提取数据时,将显示日期,并在日期后附加12:00:00 AM的时间

例如,日期“30/10/2015”将作为单独的日期存储在数据库中,但当它添加到gridview时,如上所述,时间12:00:00AM将附加到它。我只想在gridview中显示日期,而不是时间

以下是将数据库中的数据绑定到gridview的代码:

private void BindGridEvent()
{
    string constr = System.Configuration.ConfigurationManage.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("EventInfo_CRUD"))
        {
            cmd.Parameters.AddWithValue("@Action", "SELECT");

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                sda.SelectCommand = cmd;

                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView2.DataSource = dt;
                    GridView2.DataBind();
                }
            }
        }
    }
}
下面是用于从数据库中提取数据的存储过程:

[dbo].[EventInfo_CRUD]
      @Action VARCHAR(10)
      ,@EventID INT = NULL
      ,@Location nvarchar(MAX) = NULL
      ,@EventName nvarchar(MAX) = NULL
      ,@EventDescription nvarchar(MAX) = NULL
      ,@EventDate date = NULL
      ,@EventTime time = NULL
      ,@ImageUrl nvarchar(MAX) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    --SELECT
    IF @Action = 'SELECT'
    BEGIN
        SELECT 
            EventID,Location, EventName, EventDescription, 
            EventDate, EventTime, ImageUrl
        FROM EventInformation
    END

帮助将是非常感激的家伙!谢谢

您需要指示gridview——或者更准确地说:它的
BoundField
列类型之一——以特定格式显示此.NET
DateTime
——只显示日期部分,不显示时间

这意味着您需要手动将列添加到gridview-您不能在gridview上使用
AutoGenerateColumns

然后,对于这些有问题的列,您需要设置
DataFormatString
属性,如下所示:

<asp:GridView ......>
    <Columns>  
        .......
        <asp:BoundField DataField="EventDate" DataFormatString="{0:d}" />

        .......
    </Columns>
</asp:GridView ......>

有关您拥有的各种选项的更多详细信息

当我需要在gridview中设置DateTime列的格式时,以下内容适用于我

<asp:BoundField DataField="TRANDATE" HeaderText="Document Date" dataformatstring="{0:MM/dd/yyyy}" htmlencode="false" />

请尝试以下模板字段:

<%# Bind("Document Date", "{0:dd/MM/yyyy}") %>

如果您使用的是
asp:TemplateField
,请尝试以下操作

<asp:TemplateField>
    <ItemTemplate>
        <asp:Label ID="EventDate" runat="server" Text='<%# Bind("EventDate ", "{0:d}") %>'>
        </asp:Label>
    </ItemTemplate>
</asp:TemplateField>


这可能是一个格式问题。没有
Date
在C#中键入,只有
DateTime
因此,如果您不想在输出中显示更改格式所需的时间。很抱歉,我的错,我没有显示我的ASP代码。如果我的列是一个BoundField,但它是一个TemplateField,那么这肯定会起作用。我的templateField中有一个ItemTemplate,因此将其更改为BoundField会引发错误“Validation(HTML5):元素“ItemTemplate”不受支持。”是的,很抱歉我忘了提及。该列是TemplateField而不是BoundField:(还有其他建议吗?:)非常感谢!这就解决了!谢谢你,先生,终于修好了!