VB.NET调用SQL Server存储过程

开发 后端
VB.NET调用SQL Server存储过程时,会有三种情况出现,分别为无返回值的调用,返回普通值的调用,以及返回数据集的调用等等。

VB.NET编程语言对于数据库的操作,我们51CTO以前也介绍了不少。比如VB.NET数据库压缩的实现方法,VB.NET操作MySql数据库的具体操作技巧等等。那么今天大家将会了解到VB.NET调用SQL Server存储过程的相关应用方法。

定义数据链接部分省略, myConn为链接对象 ProcName为存储过程名

1.VB.NET调用SQL Server存储过程时无返回值

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

2.VB.NET调用SQL Server存储过程返回普通值

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

3.VB.NET调用SQL Server存储过程返回数据集

'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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

存储过程代码

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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

VB.NET调用SQL Server存储过程的相关应用方法就为大家介绍到这里。

【编辑推荐】

  1. VB.NET添加自动查询功能实现技巧概述
  2. VB.NET关于对话框制作技巧分享
  3. VB.NET Mid函数相关应用方法解析
  4. VB.NET获取当前URL方法浅谈
  5. VB.NET初始化网格实现方法解析
责任编辑:曹凯 来源: itwis.com
相关推荐

2010-01-11 11:02:27

VB.NET调用存储过

2010-01-19 09:48:22

VB.NET调用过程

2010-01-19 14:42:43

VB.NET调用过程重

2010-01-15 15:39:14

VB.NET Sub过

2009-10-13 11:22:46

VB.NET调用Web

2010-01-19 18:24:29

VB.NET调用Win

2010-01-15 16:46:05

VB.NET集合存储

2010-11-12 09:46:55

Sql Server存

2010-01-19 17:26:37

VB.NET创建过程

2009-11-02 10:36:16

VB.NET Sub

2010-01-19 13:36:16

VB.NET可选参数

2009-11-03 12:52:38

VB.NET Wind

2009-10-29 11:26:28

VB.NET调用Web

2009-11-10 16:55:05

VB.NET调用API

2010-01-11 17:24:19

VB.NET异步调用

2010-01-18 17:29:35

VB.NET函数调用

2009-10-15 17:50:48

VB.NET调用API

2010-01-15 16:12:40

VB.NET调用DLL

2009-10-26 18:11:47

VB.NET调用Exc

2009-10-29 14:22:59

VB.NET集合存储
点赞
收藏

51CTO技术栈公众号