存储过程进阶(vb.net+SQL Server2008环境)

写过一篇存储过程入门的博客,那仅仅是入门,下面和大家一起深入学习存储过程(也许以后还会有更深入^_^

以经典的注册为例子,篇幅有限只写了核心部分,其他略过。

  • 无参数无返回值的存储过程

以默认的信息进行注册

image

其中存储过程代码如下

   1:  USE [Test]
   2:  GO
   3:  /****** Object:  StoredProcedure [dbo].[MyProcedure1]    Script Date: 04/04/2011 20:55:13 ******/
   4:  SET ANSI_NULLS ON
   5:  GO
   6:  SET QUOTED_IDENTIFIER ON
   7:  GO
   8:  ALTER proc [dbo].[MyProcedure1]
   9:  as 
  10:  begin
  11:  insert into Login values ('admin','admin')
  12:  end

程序代码如下

   1:      Dim Str As String = "Data Source=******;Initial Catalog=Test;User ID=sa;Password=*****"
   2:      Dim conn As SqlConnection = New SqlConnection(Str)
   3:      Private Sub btnNoParNoOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNoParNoOut.Click
   4:          Dim sql As String = "execute MyProcedure1"
   5:          Dim cmd As SqlCommand = New SqlCommand(sql, conn)
   6:          Try
   7:              conn.Open()
   8:              If cmd.ExecuteNonQuery >= 0 Then
   9:                  MsgBox("Success",,"无参数无返回值")
  10:              Else
  11:                  MsgBox("Fail")
  12:              End If
  13:          Catch ex As Exception
  14:              MsgBox(ex.Message)
  15:          End Try
  16:          conn.Close()
  17:      End Sub

以上是最简单的存储过程的使用,其中Sqlcommand只是传递了字符串,并且其类型也是默认的类型未作更改,简单的可以理解为像SQL Server中传递了一个可以执行的语句。

  • 有参数有返回值的存储过程

用户自定义信息注册

image

其中存储过程代码如下

   1:  USE [Test]
   2:  GO
   3:  /****** Object:  StoredProcedure [dbo].[MyProcedure2]    Script Date: 04/04/2011 21:00:18 ******/
   4:  SET ANSI_NULLS ON
   5:  GO
   6:  SET QUOTED_IDENTIFIER ON
   7:  GO
   8:  ALTER proc [dbo].[MyProcedure2]
   9:  @UserID varchar(12),
  10:  @Pwd varchar(12)
  11:  as 
  12:  begin
  13:  insert into Login values (@UserID,@Pwd )
  14:  end

<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

程序代码如下

   1:   Private Sub btnParNoOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParNoOut.Click
   2:          Dim sql As String = "execute MyProcedure2 @User, @Pwd"
   3:          Dim cmd As SqlCommand = New SqlCommand(sql, conn)
   4:          Dim parameter As SqlParameter
   5:          parameter = New SqlParameter("@User", SqlDbType.VarChar)
   6:          parameter.Value = Trim(txtParameter.Text)
   7:          cmd.Parameters.Add(parameter)
   8:  
   9:          parameter = New SqlParameter("@Pwd", SqlDbType.VarChar)
  10:          parameter.Value = Trim(txtPwd.Text)
  11:          cmd.Parameters.Add(parameter)
  12:          Try
  13:              conn.Open()
  14:              If cmd.ExecuteNonQuery >= 0 Then
  15:                  MsgBox("Success", , "有参数无返回值")
  16:              Else
  17:                  MsgBox("Fail")
  18:              End If
  19:          Catch ex As Exception
  20:              MsgBox(ex.Message)
  21:          End Try
  22:          conn.Close()
  23:      End Sub
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

上面的存储过程是比较复杂的,但是并不难。只是加入了两个参数@UserID@Pwd,既然有参数应该传递进入我们想要的值,vb.net 为我们提供了相应的方法(cmd.Parameters.Add)。这样一来程序以我们想要的值替换了字符串中的参数,然后交给SQL Server进行执行。

  • 有参数有返回值的存储过程

用户输入ID后进行密码查询

image

其中存储过程代码如下

   1:  USE [Test]
   2:  GO
   3:  /****** Object:  StoredProcedure [dbo].[MyProcedure3]    Script Date: 04/04/2011 21:05:14 ******/
   4:  SET ANSI_NULLS ON
   5:  GO
   6:  SET QUOTED_IDENTIFIER ON
   7:  GO
   8:  ALTER proc [dbo].[MyProcedure3]
   9:  @UserID varchar(12),
  10:  @Pwd varchar(12) output
  11:  as 
  12:  begin
  13:  set @Pwd =( select Pwd from Login where UserID =@UserID)
  14:  end
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

程序代码如下

   1:  Private Sub btnParOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParOut.Click
   2:          Dim sql As String = "MyProcedure3"
   3:          Dim cmd As SqlCommand = New SqlCommand(sql, conn)
   4:          cmd.CommandType = CommandType.StoredProcedure
   5:  
   6:          cmd.Parameters.Add(New SqlParameter("@UserID", SqlDbType.VarChar))
   7:          cmd.Parameters("@UserID").Value = Trim(txtParameter.Text)
   8:  
   9:          cmd.Parameters.Add(New SqlParameter("@Pwd", SqlDbType.VarChar, 12))
  10:          cmd.Parameters("@Pwd").Direction = ParameterDirection.Output
  11:  
  12:          Try
  13:              conn.Open()
  14:              cmd.ExecuteReader()
  15:              MsgBox(cmd.Parameters("@Pwd").Value.ToString(), , "有参数有返回值")
  16:          Catch ex As Exception
  17:              MsgBox(ex.Message)
  18:          End Try
  19:          conn.Close()
  20:  End Sub
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

其中需要格外注意的地方

1、 返回值的字符长度必须写,否则会出错。

2、 存储过程中返回值可以直接set@**=……,也可以最后return@**

上面的存储过程比之前的更有深度,但也不是不可理解。首先改变了SqlCommand的类型,使其只针对于存储过程(即直接执行,无需在字符串中写明参数)。由于字符串(存储过程名)中没有参数,所以相应的参数直接通过SqlCommand传入SQL Server中。返回值是通过相应的方法(cmd.Parameters(“Pwd”).Direction=ParamterDIrection.OutPut)从数据库中取得。得到返回值后转换成字符串就可以显示出来。

你可能感兴趣的:(sql,server)