如何使用c#将额外的列添加到datebase [英] how to add extra columns to datebase using c#

查看:68
本文介绍了如何使用c#将额外的列添加到datebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个控件月数,开始日期和结束日期,

如果我没有月份5和开始日期30/5/2015然后我自动计算结束日期几个月



i有表字段Noofmonths StartDate和结束日期



将数据插入数据库时​​(sql服务器2008)我想添加新的列,这些是5月至2015年的服务器,2015年6月......

我的代码是



protected void Button1_Click(object sender,EventArgs e)

{

SqlCommand cmd = new SqlCommand(insert into monthcal(NoOfMonths,StartDate, EndDate)值(@ NoOfMonths,@ StartDate,@ EndDate),con);

cmd.Parameters.AddWithValue(@ NoOfMonths,txtNoOfMonths.Text.ToString());

cmd.Parameters.AddWithValue(@ StartDate,txtStartDate.Text.ToString());

cmd.Parameters.AddWithValue(@ EndDate,txtEndDate.Text.ToString() );

< br $>




con.Open();

cmd.ExecuteNonQuery();



}

protected void txtStartDate_TextChanged(object sender,EventArgs e)

{

string inputString = txtStartDate。文字;

DateTime dt = DateTime.ParseExact(inputString,yyyy / MM / dd,CultureInfo.InvariantCulture);

dt = dt.AddMonths(Convert.ToInt32( txtNoOfMonths.Text));

txtEndDate.Text = dt.ToString(yyyy / MM / dd);

I have 3 controls Number of Months,Start Date and End Date,
if i No of months 5 and Start date 30/5/2015 then i am calculating end date automatically by using no of months

i have table with fields Noofmonths StartDate and End Date

while inserting the data to database(sql server 2008) i want to add new columns those are No of Months 5 to server like May-2015, june-2015......
my code is

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("Insert into monthcal (NoOfMonths,StartDate,EndDate) values (@NoOfMonths,@StartDate,@EndDate)", con);
cmd.Parameters.AddWithValue("@NoOfMonths", txtNoOfMonths.Text.ToString());
cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Text.ToString());
cmd.Parameters.AddWithValue("@EndDate", txtEndDate.Text.ToString());



con.Open();
cmd.ExecuteNonQuery();

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

推荐答案

试试这个.....



try this.....

SqlCommand com;
string str;
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "ALTER TABLE  [dbo].[new] ADD  " + TextBox1.Text.ToString() + " Varchar(50)  NULL ";
com = new SqlCommand(str,con);
com.ExecuteNonQuery();
con.Close();


如果某些值(例如结束日期)可以从现有参数(例如月份数和开始日期)派生,则没有理由存储它。只需存储月数和开始日期即可。

尝试向关系表添加新列更糟糕。重新查看您的数据库设计。

1. http:/ /www.datanamic.com/support/lt-dez005-introduction-db-modeling.html [ ^ ]

2. http://www.studytonight.com/dbms/database-normalization.php [ ^ ]
If some value such as end date can be derived from existing parameters like number of month and start date, then there is no reason to store it. Just store the number of month and start date will suffice.
It is even worse trying to add new columns to relational table. Re-look your database design.
1. http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html[^]
2. http://www.studytonight.com/dbms/database-normalization.php[^]


这篇关于如何使用c#将额外的列添加到datebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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