1

我将 2 个同步时间作为 2 个新列添加到我的数据库中,并插入如下值:

USE [DB]


ALTER TABLE [dbo].[TableName]
    ADD ColumnName2 time, ColumnName3 time

这是为了添加列。

为了插入我所做的行值:

USE DB

INSERT INTO TableName (ColumnName2, ColumnName3)
VALUES ('20:30:00', '23:30:00')

这是那些列的行中固定时间的数据。

我还浏览了应用程序的所有层,例如(控制器、模型、视图、查询、服务、接口等。现在,当我尝试更新添加的任何新时间时,它们默认为已经存在的第一次在表上作为一个 COLUMN 与行中的时间。

我无法从应用程序中发布时间字段的图像,因为这是不允许的。但是,图像在一个小面板中,由 3 个字段(textboxfor)组成,每个字段都有一个时间选择器。

任何帮助将不胜感激。

谢谢

现在我想我会发布一些示例代码,看看这是否有帮助

// 这些同步时间的控制器方法

[HttpPost]
        [Page(PageName.UpdateSystemConfigTime)]
        public ActionResult UpdateTime(SystemMaintenanceViewModel model)
        {
            var dateTime = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime2 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime2, "h:mm tt", CultureInfo.InvariantCulture);
            var dateTime3 = DateTime.ParseExact(model.SystemConfiguration.SynchronizationTime3, "h:mm tt", CultureInfo.InvariantCulture);                        
            //model.ProcessTime
            if (model.SystemConfiguration.SynchronizationTime != null &&
                model.SystemConfiguration.SynchronizationTime2 != null &&
                model.SystemConfiguration.SynchronizationTime3 != null);
            {
                var sysConfig = new DTO.SystemSync.SystemConfiguration
                {
                    SyncTime = dateTime.TimeOfDay,
                    SyncTime2 = dateTime2.TimeOfDay,
                    SyncTime3 = dateTime3.TimeOfDay
                };


                configService.UpdateSyncTime(sysConfig);
                configService.UpdateSyncTime2(sysConfig);
                configService.UpdateSyncTime3(sysConfig);

            }


            return RedirectToAction("Index");
        }



////My Private method

private SystemConfiguration GetSystemConfig()
        {
            var model = new SystemConfiguration();
            var config = configService.GetSyncTime();
                         configService.GetSyncTime2();
                         configService.GetSyncTime3();

            if (config == null) return model;
            var ts = config.SyncTime;
            if (ts != null)
            {
                model.SynchronizationTime = ts.ToString();
            }

            var ts2 = config.SyncTime2;
            if (ts2 != null)
            {
                model.SynchronizationTime2 = ts2.ToString();
            }

            var ts3 = config.SyncTime3;
            if (ts3 != null)
            {
                model.SynchronizationTime3 = ts3.ToString();
            }
            return model;
============================================================================
/// My configuration command


namespace --.--.Commands
{
    public class ConfigurationCommand : CommandBase, IConfigurationCommand
    {
        static ConfigurationCommand()
        {
            ConfigureAutoMapper();
        }

        private static void ConfigureAutoMapper()
        {
             Mapper.CreateMap<SystemConfiguration, entity.SystemConfiguration>()
                .ForMember(dest => dest.SyncTime, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime2, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>())
                .ForMember(dest => dest.SyncTime3, opt => opt.ResolveUsing<TimeSpanToSqlTimeResolver>());
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();

            //if this is the first time, then we need to insert
            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime = mapped.SyncTime
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime = mapped.SyncTime;
            }
            SaveChanges();
        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime2 = mapped.SyncTime2
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime2 = mapped.SyncTime2;
            }
            SaveChanges();
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            Guard.NotNull(timeOfDay);
            var mapped = Mapper.Map<entity.SystemConfiguration>(timeOfDay);

            var config = Context.SystemConfigurations.SingleOrDefault();


            if (config == null)
            {
                var newConfig = new entity.SystemConfiguration
                {
                    SyncTime3 = mapped.SyncTime3
                };
                Context.SystemConfigurations.Add(newConfig);
            }
            else
            {
                config.SyncTime3 = mapped.SyncTime3;
            }
            SaveChanges();
        }
    }
}



=========================================================================================================
// My configuration service


namespace --.--.--.SystemSync
{
    public class ConfigurationService : IConfigurationService
    {
        private IConfigurationQuery query;
        private IConfigurationCommand command;

        public ConfigurationService(IConfigurationQuery query,IConfigurationCommand command)
        {
            this.query = query;
            this.command = command;
        }

        public void UpdateSyncTime(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime(timeOfDay);

        }

        public void UpdateSyncTime2(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime2(timeOfDay);
        }

        public void UpdateSyncTime3(SystemConfiguration timeOfDay)
        {
            command.UpdateSyncTime3(timeOfDay);
        }


        public SystemConfiguration GetSyncTime()
        {
            return query.GetSyncTime();
        }

        public SystemConfiguration GetSyncTime2()
        {
            return query.GetSyncTime2();
        }

        public SystemConfiguration GetSyncTime3()
        {
            return query.GetSyncTime3();
        }


        public List<PageResource> GetPages()
        {
            return query.GetPages().ToList();
        }

    }
}
4

1 回答 1

0

你发表了关于固定时间的评论,你在寻找这样的东西吗?

   CREATE TABLE [dbo].[Zamen](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [time1] [time](3) NOT NULL,
    [time2] [time](3) NOT NULL,
    [Content] [varchar](100) NULL,
 CONSTRAINT [PK_Zamen] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
))

GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time1]  DEFAULT (getdate()) FOR [time1]
GO

ALTER TABLE [dbo].[Zamen] ADD  CONSTRAINT [DF_Zamen_time2]  DEFAULT (getdate()) FOR [time2]
GO

那些 alter table 语句允许自动插入时间。所以当你这样做时:

INSERT INTO Zamen (Content) VALUES ('demo')

当前时间被放入值中。

*在看到您添加的代码后,一些输入:在您的UpdateTime Action Method 中,一个突出的问题是您调用了 UpdateTimeSync 三次,但每次都传递了所有三个变量。我建议重构您的更新方法——而不是三种更新方法,而是对所有时间变量使用一种更新方法。

于 2017-07-19T18:48:41.793 回答