自动生成程序,免去增,删,改,查的数据库编程 ( 定义游标,逐个遍历表的每个列。生成你想要的程序)

数据库编程,繁琐的增,删,改,查。
每个表都要实现,为了简化工作,可以用以下的sql批处理来输出你想要得结果。
不用一个一个列去写。
只需要提供表名称tablename,并把print部分修改成你需要的就可以了。

--请设置查询分析器以文本显示结果

--定义变量
declare @tablename nvarchar(50)--表的名称
declare @tableid int  --表的id
declare @colname nvarchar(50)  --列名
declare @index int    

--赋值
set @tablename = 'tablename'  -->>>tablename改为你要查询的表名         
select @tableid = id from sysobjects where id = object_id(@tablename)
set @index = 0

--定义游标,逐个遍历表的每个列
declare columns_cursor cursor
for select name as 'Column_name' from syscolumns where id = @tableid order by colid
--打开游标
open columns_cursor
fetch next from columns_cursor into @colname

while @@FETCH_STATUS = 0
begin
     -->>>这句需要修改成你要的样式即可(增,删,改,查。)
     --我的这句是向一个数据控件写数据(用友华表cell)
     print 'axCell1.SetCellString(' + cast(@index as nvarchar) + ', j, 0, ds.Tables[0].Rows[i]["'  + @colname + '"].ToString());'

     set @index = @index + 1
     fetch next from columns_cursor into @colname
end

--关闭,释放游标
close columns_cursor
deallocate columns_cursor

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值