asp.net 存储过程调用
程序员文章站
2024-03-09 08:28:11
1.调用存储过程,但无返回值 复制代码 代码如下:private function sqlproc1(byval procname as string) as boolea...
1.调用存储过程,但无返回值
private function sqlproc1(byval procname as string) as boolean
'定义数据链接部分省略, myconn为链接对象 procname为存储过程名
dim mycommand as new sqlclient.sqlcommand(procname, myconn)
with mycommand
.commandtype = commandtype.storedprocedure
.parameters.add("@codetype", sqldbtype.varchar, 20).value = "年级编码"
try
.executenonquery()
return true
catch ex as exception
return false
end try
end function
2.调用存储过程,返回普通值
private function sqlproc1(byval procname as string) as string
'定义数据链接部分省略, myconn为链接对象
dim mycommand as new sqlclient.sqlcommand(procname, myconn)
with mycommand
.commandtype = commandtype.storedprocedure
.parameters.add("@codetype", sqldbtype.varchar, 20).value = "年级编码"
.parameters.add("@newcode", sqldbtype.varchar, 20).direction = parameterdirection.output
try
.executenonquery()
return .parameters(1).value()
catch ex as exception
return "无编码生成"
end try
end function
3.调用存储过程,返回数据集
'vb.net代码
private function sqlproc2(byval procname as string, byval param1 as string) as dataset
'定义命令对象,并使用储存过程
dim mycommand as new sqlclient.sqlcommand
mycommand.commandtype = commandtype.storedprocedure
mycommand.commandtext = procname
mycommand.connection = myconn
'定义一个数据适配器,并设置参数
dim mydapter as new sqlclient.sqldataadapter(mycommand)
mydapter.selectcommand.parameters.add("@name", sqldbtype.varchar, 20).value = param1
'定义一个数据集对象,并填充数据集
dim mydataset as new dataset
try
mydapter.fill(mydataset)
catch ex as exception
end try
return mydataset
end function
'存储过程代码
create proc test @name varchar(20) as
select * from ec_grade where cgradename=@name
go
***如果将存储过程修改部分内容,可以做为查询使用
create proc test
@name varchar(200)=''
--此处应该注意200为查询条件的长度,可以根据实际情况而定;但不建议用于过长的查询条件
as
declare @sql1 varchar(8000)
if @name<>''
select @sql1='select * from ec_grade where '+ @name
else
select @sql1='select * from ec_grade'
exec(@sql1)
go
复制代码 代码如下:
private function sqlproc1(byval procname as string) as boolean
'定义数据链接部分省略, myconn为链接对象 procname为存储过程名
dim mycommand as new sqlclient.sqlcommand(procname, myconn)
with mycommand
.commandtype = commandtype.storedprocedure
.parameters.add("@codetype", sqldbtype.varchar, 20).value = "年级编码"
try
.executenonquery()
return true
catch ex as exception
return false
end try
end function
2.调用存储过程,返回普通值
复制代码 代码如下:
private function sqlproc1(byval procname as string) as string
'定义数据链接部分省略, myconn为链接对象
dim mycommand as new sqlclient.sqlcommand(procname, myconn)
with mycommand
.commandtype = commandtype.storedprocedure
.parameters.add("@codetype", sqldbtype.varchar, 20).value = "年级编码"
.parameters.add("@newcode", sqldbtype.varchar, 20).direction = parameterdirection.output
try
.executenonquery()
return .parameters(1).value()
catch ex as exception
return "无编码生成"
end try
end function
3.调用存储过程,返回数据集
'vb.net代码
复制代码 代码如下:
private function sqlproc2(byval procname as string, byval param1 as string) as dataset
'定义命令对象,并使用储存过程
dim mycommand as new sqlclient.sqlcommand
mycommand.commandtype = commandtype.storedprocedure
mycommand.commandtext = procname
mycommand.connection = myconn
'定义一个数据适配器,并设置参数
dim mydapter as new sqlclient.sqldataadapter(mycommand)
mydapter.selectcommand.parameters.add("@name", sqldbtype.varchar, 20).value = param1
'定义一个数据集对象,并填充数据集
dim mydataset as new dataset
try
mydapter.fill(mydataset)
catch ex as exception
end try
return mydataset
end function
'存储过程代码
create proc test @name varchar(20) as
select * from ec_grade where cgradename=@name
go
***如果将存储过程修改部分内容,可以做为查询使用
create proc test
@name varchar(200)=''
--此处应该注意200为查询条件的长度,可以根据实际情况而定;但不建议用于过长的查询条件
as
declare @sql1 varchar(8000)
if @name<>''
select @sql1='select * from ec_grade where '+ @name
else
select @sql1='select * from ec_grade'
exec(@sql1)
go
上一篇: C# 通用文件上传类