1,在asp.net中取的存储过程的返回值和输出参数 (只能ExecuteNonQuery())
设参数和直接return时都可接收到
string sConnectionString =User ID=zdp;pwd=zdp;Initial Catalog=aaa;Data Source=(local);
SqlConnection conn=new SqlConnection(sConnectionString);
conn.Open();
SqlCommand cm=new SqlCommand(aaaa,conn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add(@id,SqlDbType.Int,4);
cm.Parameters.Add(@test,SqlDbType.Int,4);
cm.Parameters.Add(@ouid,SqlDbType.Int,4);
cm.Parameters[@id].Value=Convert.ToInt32(newid.Text.ToString());
cm.Parameters[@test].Direction=System.Data.ParameterDirection.ReturnValue;
cm.Parameters[@ouid].Direction=System.Data.ParameterDirection.Output;
cm.ExecuteNonQuery();
//OUTRET为存储过程中输出参数给变量outret赋予的值
string outret=cm.Parameters[@ouid].Value.ToString();
//returnvalue为存储过程给返回值变量returnvalue赋予的值
string returnvalue=cm.Parameters[@test].Value.ToString();
//mes,ret是两个LABEL的ID
mes.Text=outret.ToString();
ret.Text=returnvalue.ToString();
//-----------------------------------存储过程
CREATE PROCEDURE aaaa
@id INT,
@ouid int output
AS
SET NOCOUNT ON
begin
if exists(select * from yg where id=@id)
BEGIN
set @ouid=@id
return 0
END
set @ouid=@id
RETURN 1
end
GO
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
vb中调用存储过程时,return 返回的值接收不到,要接收返回值必须设输出参数
Public Function CheckDataPOR() As Boolean
Dim cmd As ADODB.Command
Dim param As ADODB.Parameter
Dim iniInfo As clsClientIni
Dim connStr As String
Set cmd = New ADODB.Command
Set iniInfo = New clsClientIni
connStr = iniInfo.ConnectString()
cmd.ActiveConnection = connStr
cmd.CommandText = "CheckDataPOR"
cmd.CommandType = adCmdStoredProc
Set param = cmd.CreateParameter("m_PDC", adInteger, adParamInput, 4, m_PDC)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("ERRORCODE", adInteger, adParamOutput)
cmd.Parameters.Append param
cmd.Execute
If cmd.Parameters("ERRORCODE").Value = 1 Then
CheckDataPOR = False
Else
CheckDataPOR = True
End If
Set cmd = Nothing
Set iniInfo = Nothing
End Function
//存储过程代码
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