如何在SQL中更改值

时间:2017-02-08 13:02:38

标签: c# sql-server datetime stored-procedures

我在SQL-Server2008中使用Stored-Procedure。 C#中实现的代码如下:

SqlConnection con = new SqlConnection("...");

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand("sp_point", con);
da.SelectCommand = cmd;
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
da.Fill(dt);
dataGridView1.DataSource = dt;

我的数据库中的日期时间格式基于日期,小时,分钟,秒和毫秒。 列也显示毫秒。 如果我想显示毫秒,我该如何修改代码?

修改

我的存储程序:

    USE [Diagnose]
GO
/****** Object:  StoredProcedure [dbo].[sp_point]    Script Date: 02/09/2017 14:04:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_point]

as
SELECT     dbo.Station.StationName, dbo.SPointState.SPointStateName, dbo.Point.PointName, dbo.PointState.PointStateName, dbo.PositionPoint.PositionPointName, 
                      dbo.RouteType.RouteTypeName, dbo.PartOfRoute.PartOfRoute AS PartOfRoute, dbo.PointPacket.PointPacketID AS PointPacketID, dbo.PointPacket.Operator AS Operator, 
                      dbo.PointPacket.BlockedPoint AS BlockedPoint, dbo.PointPacket.WarningLable AS WarningLable, dbo.PointPacket.EmergencyLock AS EmergencyLock, 
                      dbo.PointPacket.SendPointStateFromIO AS SendPointStateFromIO, dbo.PointPacket.SendPointPositionFromIO AS SendPointPositionFromIO, dbo.PointPacket.HealthSendFromIO AS HealthSendFromIO, 
                      dbo.Date.Date AS Date_Time
FROM         dbo.PointPacket INNER JOIN
                      dbo.SPointState ON dbo.PointPacket.SPointState = dbo.SPointState.SPointStateID INNER JOIN
                      dbo.Station ON dbo.PointPacket.StationCode = dbo.Station.StationID INNER JOIN
                      dbo.Date ON dbo.PointPacket.Date = dbo.Date.DateID INNER JOIN
                      dbo.Point ON dbo.PointPacket.PointCode = dbo.Point.PointID INNER JOIN
                      dbo.PointState ON dbo.PointPacket.PointState = dbo.PointState.PointStateID INNER JOIN
                      dbo.PositionPoint ON dbo.PointPacket.PositionPoint = dbo.PositionPoint.PositionPointID INNER JOIN
                      dbo.RouteType ON dbo.PointPacket.RouteType = dbo.RouteType.ID INNER JOIN
                      dbo.PartOfRoute ON dbo.PointPacket.PartOfRoute = dbo.PartOfRoute.ID

select * from PointPacket

2 个答案:

答案 0 :(得分:3)

您应该更改数据在视图层中的显示方式,而不是数据(SQL过程)或服务层(da.Fill(dt);)。

您可以自定义日期时间信息在网格视图列中的显示方式here

// keep all date/time info until seconds, included
gridView.Columns[dateColIndex].DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss";

答案 1 :(得分:0)

尝试将datetime列转换为varchar格式。