VB.Net调用存储过程并得到返回值
【打印文章】
//存储过程代码
CREATE PROCEDURE [AddNewInvoiceInfo] (@sUserID int,@sClaimMethod varchar(50),@sInvoiceNum
varchar(50),@sInvoiceDate datetime,@sBuyCity varchar(50))AS
Insert Into SMB_InvoiceInfo (MemberID,ClaimMethod,InvoiceNum,InvoiceDate,InvoiceCity) Values
(@sUserID,@sClaimMethod,@sInvoiceNum,@sInvoiceDate,@sBuyCity)
return @@IDENTITY
GO
//调用存储过程代码
Dim CN As New SqlConnection()
Dim InvoiceID As String
Try
CN.ConnectionString = DBConStr
CN.Open()
Dim CMD As SqlCommand
CMD = New SqlCommand(AddNewInvoiceInfo, CN)
CMD.CommandType = CommandType.StoredProcedure
CMD.Parameters.Add(New SqlParameter(@sUserID, SqlDbType.Int))
CMD.Parameters(@sUserID).Value = CInt(UserID.Text)
CMD.Parameters.Add(New SqlParameter(@sClaimMethod, SqlDbType.VarChar, 50))
CMD.Parameters(@sClaimMethod).Value = TransName.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceNum, SqlDbType.VarChar, 50))
CMD.Parameters(@sInvoiceNum).Value = InvoiceNum.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceDate, SqlDbType.DateTime))
CMD.Parameters(@sInvoiceDate).Value = InvoiceDate.SelectedDate.Date
CMD.Parameters.Add(New SqlParameter(@sBuyCity, SqlDbType.VarChar, 50))
CMD.Parameters(@sBuyCity).Value = BuyCity.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceID, SqlDbType.Int))
CMD.Parameters(@sInvoiceID).Direction = ParameterDirection.ReturnValue
CMD.ExecuteNonQuery()
InvoiceID = CMD.Parameters(@sInvoiceID).Value.ToString()
CMD.Dispose()
Catch ex As Exception
Response.Write(ex.ToString())
Finally
CN.Close()
End Try
CREATE PROCEDURE [AddNewInvoiceInfo] (@sUserID int,@sClaimMethod varchar(50),@sInvoiceNum
varchar(50),@sInvoiceDate datetime,@sBuyCity varchar(50))AS
Insert Into SMB_InvoiceInfo (MemberID,ClaimMethod,InvoiceNum,InvoiceDate,InvoiceCity) Values
(@sUserID,@sClaimMethod,@sInvoiceNum,@sInvoiceDate,@sBuyCity)
return @@IDENTITY
GO
//调用存储过程代码
Dim CN As New SqlConnection()
Dim InvoiceID As String
Try
CN.ConnectionString = DBConStr
CN.Open()
Dim CMD As SqlCommand
CMD = New SqlCommand(AddNewInvoiceInfo, CN)
CMD.CommandType = CommandType.StoredProcedure
CMD.Parameters.Add(New SqlParameter(@sUserID, SqlDbType.Int))
CMD.Parameters(@sUserID).Value = CInt(UserID.Text)
CMD.Parameters.Add(New SqlParameter(@sClaimMethod, SqlDbType.VarChar, 50))
CMD.Parameters(@sClaimMethod).Value = TransName.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceNum, SqlDbType.VarChar, 50))
CMD.Parameters(@sInvoiceNum).Value = InvoiceNum.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceDate, SqlDbType.DateTime))
CMD.Parameters(@sInvoiceDate).Value = InvoiceDate.SelectedDate.Date
CMD.Parameters.Add(New SqlParameter(@sBuyCity, SqlDbType.VarChar, 50))
CMD.Parameters(@sBuyCity).Value = BuyCity.Text
CMD.Parameters.Add(New SqlParameter(@sInvoiceID, SqlDbType.Int))
CMD.Parameters(@sInvoiceID).Direction = ParameterDirection.ReturnValue
CMD.ExecuteNonQuery()
InvoiceID = CMD.Parameters(@sInvoiceID).Value.ToString()
CMD.Dispose()
Catch ex As Exception
Response.Write(ex.ToString())
Finally
CN.Close()
End Try
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】