14 个解决方案
#1
CodeSmith4
#2
#3
/*自动生成前台C#类代码*/
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
Create proc sp_sysGenerateCode
@table varchar(50)
as
Begin
declare @sqlstr varchar(2000)
create table #tmp
(
column_name varchar(20),
type_name varchar(20),
length varchar(10)
)
create table #text(n_text varchar(1000))
select @sqlstr ='
insert into #tmp (column_name,type_name,length) select name,type_name,a.prec
from syscolumns a ,master.dbo.spt_datatype_info b
where id = object_id(N'''+@table+''')
and OBJECTPROPERTY(id, N''IsUserTable'') =1
and a.xtype=b.ss_dtype
and a.length = isnull(b.fixlen, a.length)
AND isnull(b.AUTO_INCREMENT,0) = isnull(ColumnProperty (a.id, a.name, ''IsIdentity''),0)
AND (b.ODBCVer is null or b.ODBCVer = 2)
'
execute(@sqlstr)
declare @name varchar(20)
declare @type varchar(20)
declare cur cursor scroll for select column_name,type_name from #tmp
open cur
fetch next from cur into @name,@type
while @@fetch_status = 0
begin
insert into #text values('private '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'string ' + @name + ';'
when @type='int' or @type='int identity' or @type='tinyint' then 'int ' + @name + ';'
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + @name + ';'
when @type='bit' then 'bool ' + @name + ';'
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + @name + ';'
when @type='float' then 'double ' + @name + ';'
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + @name + ';'
when @type='real' then 'single ' + @name + ';'
end
)
fetch next from cur into @name,@type
end
fetch absolute 1 from cur into @name,@type
while @@fetch_status = 0
begin
insert into #text values('public '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'string ' + upper(@name)
when @type='int' or @type='int identity' or @type='tinyint' then 'int ' + upper(@name)
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + upper(@name)
when @type='bit' then 'bool ' + upper(@name)
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + upper(@name)
when @type='float' then 'double ' + upper(@name)
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + upper(@name)
when @type='real' then 'single ' + upper(@name)
end
)
insert into #text values('{')
insert into #text values('get{return this.' + @name + ';}')
insert into #text values('set{this.' + @name + ' = value;}')
insert into #text values('}')
fetch next from cur into @name,@type
end
close cur
deallocate cur
select * from #text
drop table #tmp
drop table #text
End
GO
给你个SQL代码
#4
不好意思
要去除开头的
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
这一句
要去除开头的
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
这一句
#5
好的,我试一下
#6
要放在master数据库下?
#7
对象名 'master.dbo.spt_datatype_info' 无效。怎么回事?
#8
用工具吧,前人都写过好多模版的
#9
根本没有这个表啊
#10
什么工具,说具体点行不?
#11
-_-!!!
#12
VS2005 自帶的 資料集xsd 就可以自動生成 表對應的Class
#13
你说的是数据集?
#14
我就刚好写了一个这样的工具~需要M我吧!1953420@qq.com
#1
CodeSmith4
#2
#3
/*自动生成前台C#类代码*/
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
Create proc sp_sysGenerateCode
@table varchar(50)
as
Begin
declare @sqlstr varchar(2000)
create table #tmp
(
column_name varchar(20),
type_name varchar(20),
length varchar(10)
)
create table #text(n_text varchar(1000))
select @sqlstr ='
insert into #tmp (column_name,type_name,length) select name,type_name,a.prec
from syscolumns a ,master.dbo.spt_datatype_info b
where id = object_id(N'''+@table+''')
and OBJECTPROPERTY(id, N''IsUserTable'') =1
and a.xtype=b.ss_dtype
and a.length = isnull(b.fixlen, a.length)
AND isnull(b.AUTO_INCREMENT,0) = isnull(ColumnProperty (a.id, a.name, ''IsIdentity''),0)
AND (b.ODBCVer is null or b.ODBCVer = 2)
'
execute(@sqlstr)
declare @name varchar(20)
declare @type varchar(20)
declare cur cursor scroll for select column_name,type_name from #tmp
open cur
fetch next from cur into @name,@type
while @@fetch_status = 0
begin
insert into #text values('private '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'string ' + @name + ';'
when @type='int' or @type='int identity' or @type='tinyint' then 'int ' + @name + ';'
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + @name + ';'
when @type='bit' then 'bool ' + @name + ';'
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + @name + ';'
when @type='float' then 'double ' + @name + ';'
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + @name + ';'
when @type='real' then 'single ' + @name + ';'
end
)
fetch next from cur into @name,@type
end
fetch absolute 1 from cur into @name,@type
while @@fetch_status = 0
begin
insert into #text values('public '+ case when @type='varchar' or @type='nvarchar' or @type='char' or @type='text' or @type='ntext' then 'string ' + upper(@name)
when @type='int' or @type='int identity' or @type='tinyint' then 'int ' + upper(@name)
when @type='datetime' or @type='smalldatetime' then 'DateTime ' + upper(@name)
when @type='bit' then 'bool ' + upper(@name)
when @type='decimal' or @type='numeric' or @type='money' or @type='smallmoney' then 'decimal ' + upper(@name)
when @type='float' then 'double ' + upper(@name)
when @type='binary' or @type='image' or @type='varbinary' or @type='timestamp' then 'byte[] ' + upper(@name)
when @type='real' then 'single ' + upper(@name)
end
)
insert into #text values('{')
insert into #text values('get{return this.' + @name + ';}')
insert into #text values('set{this.' + @name + ' = value;}')
insert into #text values('}')
fetch next from cur into @name,@type
end
close cur
deallocate cur
select * from #text
drop table #tmp
drop table #text
End
GO
给你个SQL代码
#4
不好意思
要去除开头的
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
这一句
要去除开头的
Exec sp_sysDropObject 'sp_sysGenerateCode'
go
这一句
#5
好的,我试一下
#6
要放在master数据库下?
#7
对象名 'master.dbo.spt_datatype_info' 无效。怎么回事?
#8
用工具吧,前人都写过好多模版的
#9
根本没有这个表啊
#10
什么工具,说具体点行不?
#11
-_-!!!
#12
VS2005 自帶的 資料集xsd 就可以自動生成 表對應的Class
#13
你说的是数据集?
#14
我就刚好写了一个这样的工具~需要M我吧!1953420@qq.com