如何使用开始日期和结束日期在数据库中自动生成列 [英] How to generate columns automatically in database using start date and end date

查看:282
本文介绍了如何使用开始日期和结束日期在数据库中自动生成列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向数据库插入值,例如

没有月份5

开始日期

结束日期



我自动计算结束日期,在开始日期加上没有月份。

我的问题是,当我向数据库提交数据时,如果没有5个月那么我想要5数据库中的列,例如5月6月8月8月9月..就像那些所有

i am inserting values to database like
No of months 5
Start Date
End Date

am calculating end date automatically by adding no of months to start date .
My question is, when i submit data to database , if No of months 5 then i want 5 columns in database like May june july August September .. like those all

protected void txtStartDate_TextChanged(object sender, EventArgs e)
{
    string inputString = txtStartDate.Text;
    DateTime dt = DateTime.ParseExact(inputString, "MM/dd/yyyy", CultureInfo.InvariantCulture);
    dt = dt.AddMonths(Convert.ToInt32(txtNoOfMonths.Text));
    txtEndDate.Text = dt.ToString("MM/dd/yyyy");
}

推荐答案

这可能是一个非常糟糕的主意 - 你可以这样做,只需看看创建表 [ ^ ]和 ALTER TABLE [ ^ ] SQL命令 - 但是......你为什么要这样做?

将数据插入带有DATETIME列的通用表中,然后使用SELECT语句对单独的数据进行排序通常要好得多。这样,您不必继续创建表,并且可能会为正在使用它的其他人弄乱数据库 - 而且由于SQL Server是一个多用户系统,因此很有可能您确实会搞砸了其他所有用户如果继续你的想法。
This is probably a very bad idea - you can do it, just see the CREATE TABLE[^] and ALTER TABLE[^] SQL commands - but...why would you want to?
It's generally a lot better to insert the data into a "generic" table with a DATETIME column, and then sort out separate data later with SELECT statements. That way, you don't have to keep creating tables and potentially messing the DB up for everyone else who is using it - and since SQL Server is a multiuser system there is a very, very good chance that you will indeed mess things up for every other user if you continue with your idea.


这篇关于如何使用开始日期和结束日期在数据库中自动生成列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆