动态向数据库表添加新列 [英] Dynamically adding new columns to a database table

查看:85
本文介绍了动态向数据库表添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能已经猜到了,我想(通过将来的季度)通过管理站点向数据库表中动态添加新列.我正在考虑提供一个文本框(列名),一个可能的子类型的下拉列表并添加一个按钮,该按钮将基本上添加该列.

As you could have guessed, I want to add new columns dynamically (for future quarters) to a database table via admin site. I''m thinking of providing a text box (column name), a dropdown for the possible subtypes and add a button which will basically add the column.


  1. 我想用C#在ASP.net中做到这一点.
  2. 能否给我一些有关如何实现这一点的提示?





Thanks in advance!

推荐答案

对于任何SQL Server,您都应该能够使用<a href="http://msdn.microsoft.com/en-us/library/ms190273.aspx">ALTER TABLE</a>[<a href="http://msdn.microsoft.com/en-us/library/ms190273.aspx" target="_blank" title="New Window">^</a>]之类的DDL语句向表中添加列.数据库用户将需要适当的权限. 注意事项:添加不允许为空的列可能很棘手!通过为要添加到表中的列设置默认值,使它可以工作一次.

如果您仍有疑问,请给我留言.

干杯,祝您编程愉快!

-MRB
With any SQL server you should be able to use DDL statements like <a href="http://msdn.microsoft.com/en-us/library/ms190273.aspx">ALTER TABLE</a>[<a href="http://msdn.microsoft.com/en-us/library/ms190273.aspx" target="_blank" title="New Window">^</a>] to add columns to your table. The DB user will need appropriate rights though. Caveat: Adding columns which don''t allow for nulls can be tricky! I''ve gotten this to work once by having a default value for the column that was to be added to the table.

If you still have doubts, drop me a note.

Cheers, and happy coding!

-MRB


这篇关于动态向数据库表添加新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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