怎样动态修改数据表的字段?

ljm10 2008-06-13 12:01:52
如果要动态修改数据表的字段,例如新建一个字段,删除一个字段,类的方法应该怎么写啊?最好有源码,给点思路我也可以。谢谢!
...全文
61 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljm10 2008-06-13
  • 打赏
  • 举报
回复
我也曾想过用字符串拼接的方法,谢谢你了。
fsy123456accp 2008-06-13
  • 打赏
  • 举报
回复
顶1楼的代码 收藏学习一下
wzy_love_sly 2008-06-13
  • 打赏
  • 举报
回复
create table tb(id int)

create proc alter_tb
@tbname varchar(50),
@colname varchar(50),
@coltype varchar(50),
@type int
as
begin
if @type=1
begin
if not exists(select 1 from syscolumns where id=object_id(@tbname) and name=@colname)
exec('alter table '+@tbname+' add '+@colname+' '+@coltype)
end
else
begin
if exists(select 1 from syscolumns where id=object_id(@tbname) and name=@colname)
exec('alter table '+@tbname+' drop column '+@colname)
end
end


exec alter_tb 'tb','name','varchar(50)',1--添加
select * from tb
exec alter_tb 'tb','name','varchar(50)',2--删除
select * from tb

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧