@H_403_3@写过一篇存储过程入门的博客,那仅仅是入门,下面和大家一起深入学习存储过程(也许以后还会有更深入^_^@H_403_3@)
以经典的注册为例子,篇幅有限只写了核心部分,其他略过。
无参数无返回值的存储过程以默认的信息进行注册
其中存储过程代码如下
@H_301_67@ 1: USE [Test]@H_301_67@ 2: GO@H_301_67@ 3: /****** Object: StoredProcedure [dbo].[MyProcedure1] Script Date: 04/04/2011 20:55:13 ******/@H_301_67@ 4: SET ANSI_NULLS ON@H_301_67@ 5: GO@H_301_67@ 6: SET QUOTED_IDENTIFIER ON@H_301_67@ 7: GO@H_301_67@ 8: ALTER proc [dbo].[MyProcedure1]@H_301_67@ 9: as@H_301_67@ 10: begin@H_301_67@ 11: insert into Login values ('admin','admin')@H_301_67@ 12: end程序代码如下
@H_703_301@ @H_403_3@以上是最简单的存储过程的使用,其中sqlcommand@H_403_3@只是传递了字符串,并且其类型也是默认的类型未作更改,简单的可以理解为像sql Server@H_403_3@中传递了一个可以执行的语句。@H_301_67@ 1: Dim Str As String = "Data Source=******;Initial Catalog=Test;User ID=sa;Password=*****"@H_301_67@ 2: Dim conn As sqlConnection = New sqlConnection(Str)@H_301_67@ 3: Private Sub btnNoParNoOut_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnNoParNoOut.Click@H_301_67@ 4: Dim sql As String = "execute MyProcedure1"@H_301_67@ 5: Dim cmd As sqlCommand = New sqlCommand(sql,conn)@H_301_67@ 6: Try@H_301_67@ 7: conn.Open()@H_301_67@ 8: If cmd.ExecuteNonQuery >= 0 Then@H_301_67@ 9: MsgBox("Success",,"无参数无返回值")@H_301_67@ 10: Else@H_301_67@ 11: MsgBox("Fail")@H_301_67@ 12: End If@H_301_67@ 13: Catch ex As Exception@H_301_67@ 14: MsgBox(ex.Message)@H_301_67@ 15: End Try@H_301_67@ 16: conn.Close()@H_301_67@ 17: End Sub
有参数有返回值的存储过程
@H_703_301@其中存储过程代码如下
@H_301_67@ 1: USE [Test]@H_301_67@ 2: GO@H_301_67@ 3: /****** Object: StoredProcedure [dbo].[MyProcedure2] Script Date: 04/04/2011 21:00:18 ******/@H_301_67@ 4: SET ANSI_NULLS ON@H_301_67@ 5: GO@H_301_67@ 6: SET QUOTED_IDENTIFIER ON@H_301_67@ 7: GO@H_301_67@ 8: ALTER proc [dbo].[MyProcedure2]@H_301_67@ 9: @UserID varchar(12),@H_301_67@ 10: @Pwd varchar(12)@H_301_67@ 11: as@H_301_67@ 12: begin@H_301_67@ 13: insert into Login values (@UserID,@Pwd )@H_301_67@ 14: end程序代码如下
@H_703_301@ @H_403_3@上面的存储过程是比较复杂的,但是并不难。只是加入了两个参数@UserID@H_403_3@和@Pwd@H_403_3@,既然有参数应该传递进入我们想要的值,vb.net @H_403_3@为我们提供了相应的方法(cmd.Parameters.Add@H_403_3@)。这样一来程序以我们想要的值替换了字符串中的参数,然后交给sql Server@H_403_3@进行执行。@H_301_67@ 1: Private Sub btnParNoOut_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnParNoOut.Click@H_301_67@ 2: Dim sql As String = "execute MyProcedure2 @User,@Pwd"@H_301_67@ 3: Dim cmd As sqlCommand = New sqlCommand(sql,conn)@H_301_67@ 4: Dim parameter As sqlParameter@H_301_67@ 5: parameter = New sqlParameter("@User",sqlDbType.VarChar)@H_301_67@ 6: parameter.Value = Trim(txtParameter.Text)@H_301_67@ 7: cmd.Parameters.Add(parameter)@H_301_67@ 8:@H_301_67@ 9: parameter = New sqlParameter("@Pwd",sqlDbType.VarChar)@H_301_67@ 10: parameter.Value = Trim(txtPwd.Text)@H_301_67@ 11: cmd.Parameters.Add(parameter)@H_301_67@ 12: Try@H_301_67@ 13: conn.Open()@H_301_67@ 14: If cmd.ExecuteNonQuery >= 0 Then@H_301_67@ 15: MsgBox("Success","有参数无返回值")@H_301_67@ 16: Else@H_301_67@ 17: MsgBox("Fail")@H_301_67@ 18: End If@H_301_67@ 19: Catch ex As Exception@H_301_67@ 20: MsgBox(ex.Message)@H_301_67@ 21: End Try@H_301_67@ 22: conn.Close()@H_301_67@ 23: End Sub
有参数有返回值的存储过程
原文链接:https://www.f2er.com/vb/261318.html其中存储过程代码如下
@H_301_67@ 1: USE [Test]@H_301_67@ 2: GO@H_301_67@ 3: /****** Object: StoredProcedure [dbo].[MyProcedure3] Script Date: 04/04/2011 21:05:14 ******/@H_301_67@ 4: SET ANSI_NULLS ON@H_301_67@ 5: GO@H_301_67@ 6: SET QUOTED_IDENTIFIER ON@H_301_67@ 7: GO@H_301_67@ 8: ALTER proc [dbo].[MyProcedure3]@H_301_67@ 9: @UserID varchar(12),@H_301_67@ 10: @Pwd varchar(12) output@H_301_67@ 11: as@H_301_67@ 12: begin@H_301_67@ 13: set @Pwd =( select Pwd from Login where UserID =@UserID)@H_301_67@ 14: end程序代码如下
@H_301_67@ 1: Private Sub btnParOut_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnParOut.Click@H_301_67@ 2: Dim sql As String = "MyProcedure3"@H_301_67@ 3: Dim cmd As sqlCommand = New sqlCommand(sql,conn)@H_301_67@ 4: cmd.CommandType = CommandType.StoredProcedure@H_301_67@ 5:@H_301_67@ 6: cmd.Parameters.Add(New sqlParameter("@UserID",sqlDbType.VarChar))@H_301_67@ 7: cmd.Parameters("@UserID").Value = Trim(txtParameter.Text)@H_301_67@ 8:@H_301_67@ 9: cmd.Parameters.Add(New sqlParameter("@Pwd",sqlDbType.VarChar,12))@H_301_67@ 10: cmd.Parameters("@Pwd").Direction = ParameterDirection.Output@H_301_67@ 11:@H_301_67@ 12: Try@H_301_67@ 13: conn.Open()@H_301_67@ 14: cmd.ExecuteReader()@H_301_67@ 15: MsgBox(cmd.Parameters("@Pwd").Value.ToString(),"有参数有返回值")@H_301_67@ 16: Catch ex As Exception@H_301_67@ 17: MsgBox(ex.Message)@H_301_67@ 18: End Try@H_301_67@ 19: conn.Close()@H_301_67@ 20: End Sub其中需要格外注意的地方
1、 @H_403_3@返回值的字符长度必须写,否则会出错。
2、 @H_403_3@存储过程中返回值可以直接set@**=@H_403_3@……,也可以最后return@**@H_403_3@。
@H_703_301@ @H_403_3@上面的存储过程比之前的更有深度,但也不是不可理解。首先改变了sqlCommand@H_403_3@的类型,使其只针对于存储过程(即直接执行,无需在字符串中写明参数)。由于字符串(存储过程名)中没有参数,所以相应的参数直接通过sqlCommand@H_403_3@传入sql Server@H_403_3@中。返回值是通过相应的方法(cmd.Parameters(“Pwd”).Direction=ParamterDIrection.OutPut@H_403_3@)从数据库中取得。得到返回值后转换成字符串就可以显示出来。