PC Review


Reply
Thread Tools Rate Thread

Calling a Stored Procedure from Code

 
 
Woody Splawn
Guest
Posts: n/a
 
      11th Dec 2003
I am using SQL Server 2000 as the back-end. I have created a stored
procedure in SQL server called usp_AddContract. This Stored procedure
inserts a new contract into a contracts table. I have tested the code in
Enterprise Manager and it works correctly. At the time that I run the SP I
pass a contract number and a SSN to the SP and it creates a new contract in
the contracts table with a unique value in the ConNum field and SSN field.
In order to run the stored procedure from within SQL Server, in Query
Analizer I do the following:

DECLARE @retCode int
DECLARE @ConNum nvarchar(10)
DECLARE @SSN nvarchar(11)
SET @ConNum = 'T00003'
SET @SSN = '554319132'
EXECUTE @retCode = usp_AddContract @ConNum,@SSN
PRINT @retCode

My question is, how do I do call the stored procedure under code in Visual
Studio. If I had a button on a form, what code would I write to call and
execute the stored procedure above?

I have fiddled with it myself and tried the following code in a button but I
seem to be missing something with regard to how to actually execute the SP.
(mySQLConnection and myConnectionString are public variables declared
elsewhere in my form)

Dim cmdContracts As New SqlCommand
mySqlConnection = New SqlConnection(myConnectionString)
mySqlConnection.Open()
cmdContracts = mySqlConnection.CreateCommand
cmdContracts.CommandType = CommandType.StoredProcedure
cmdContracts.CommandText = "usp_AddContract"
cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
mySqlConnection.Close()

Thank You




 
Reply With Quote
 
 
 
 
Mike Bulava
Guest
Posts: n/a
 
      11th Dec 2003
Yeah, you're are not ever executing the Stored Procedure
add line
cmdContracts.ExecuteNonQuery
after you've filled the parameters

"Woody Splawn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am using SQL Server 2000 as the back-end. I have created a stored
> procedure in SQL server called usp_AddContract. This Stored procedure
> inserts a new contract into a contracts table. I have tested the code in
> Enterprise Manager and it works correctly. At the time that I run the SP

I
> pass a contract number and a SSN to the SP and it creates a new contract

in
> the contracts table with a unique value in the ConNum field and SSN field.
> In order to run the stored procedure from within SQL Server, in Query
> Analizer I do the following:
>
> DECLARE @retCode int
> DECLARE @ConNum nvarchar(10)
> DECLARE @SSN nvarchar(11)
> SET @ConNum = 'T00003'
> SET @SSN = '554319132'
> EXECUTE @retCode = usp_AddContract @ConNum,@SSN
> PRINT @retCode
>
> My question is, how do I do call the stored procedure under code in Visual
> Studio. If I had a button on a form, what code would I write to call and
> execute the stored procedure above?
>
> I have fiddled with it myself and tried the following code in a button but

I
> seem to be missing something with regard to how to actually execute the

SP.
> (mySQLConnection and myConnectionString are public variables declared
> elsewhere in my form)
>
> Dim cmdContracts As New SqlCommand
> mySqlConnection = New SqlConnection(myConnectionString)
> mySqlConnection.Open()
> cmdContracts = mySqlConnection.CreateCommand
> cmdContracts.CommandType = CommandType.StoredProcedure
> cmdContracts.CommandText = "usp_AddContract"
> cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
> cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
> mySqlConnection.Close()
>
> Thank You
>
>
>
>



 
Reply With Quote
 
Josh Moody [MSFT]
Guest
Posts: n/a
 
      11th Dec 2003
You're pretty close; now you need to execute your sp. Before you close your
connection, do this:
dim ret as object = cmdContracts.ExecuteScalar()
Which will give you the return value that you want.

HTH

Josh Moody
VSU Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
>From: "Woody Splawn" <(E-Mail Removed)>
>Subject: Calling a Stored Procedure from Code
>Date: Thu, 11 Dec 2003 12:45:18 -0800
>Lines: 41
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <(E-Mail Removed)>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: 168.158-60-66-fuji-dsl.static.surewest.net 66.60.158.168
>Path:

cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.
phx.gbl
>Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:163333
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb
>
>I am using SQL Server 2000 as the back-end. I have created a stored
>procedure in SQL server called usp_AddContract. This Stored procedure
>inserts a new contract into a contracts table. I have tested the code in
>Enterprise Manager and it works correctly. At the time that I run the SP I
>pass a contract number and a SSN to the SP and it creates a new contract in
>the contracts table with a unique value in the ConNum field and SSN field.
>In order to run the stored procedure from within SQL Server, in Query
>Analizer I do the following:
>
>DECLARE @retCode int
>DECLARE @ConNum nvarchar(10)
>DECLARE @SSN nvarchar(11)
>SET @ConNum = 'T00003'
>SET @SSN = '554319132'
>EXECUTE @retCode = usp_AddContract @ConNum,@SSN
>PRINT @retCode
>
>My question is, how do I do call the stored procedure under code in Visual
>Studio. If I had a button on a form, what code would I write to call and
>execute the stored procedure above?
>
>I have fiddled with it myself and tried the following code in a button but

I
>seem to be missing something with regard to how to actually execute the SP.
>(mySQLConnection and myConnectionString are public variables declared
>elsewhere in my form)
>
> Dim cmdContracts As New SqlCommand
> mySqlConnection = New SqlConnection(myConnectionString)
> mySqlConnection.Open()
> cmdContracts = mySqlConnection.CreateCommand
> cmdContracts.CommandType = CommandType.StoredProcedure
> cmdContracts.CommandText = "usp_AddContract"
> cmdContracts.Parameters.Add(New SqlParameter("@ConNum", "T00005"))
> cmdContracts.Parameters.Add(New SqlParameter("@SSN", "554319132"))
> mySqlConnection.Close()
>
>Thank You
>
>
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling stored procedure using c++ =?Utf-8?B?Qm95ZA==?= Microsoft Dot NET 0 17th Aug 2007 01:44 AM
Get an error code after calling a SQL Server Stored Procedure. ab Microsoft ASP .NET 1 2nd Aug 2006 11:37 AM
calling stored procedure in ADO.NET 2.0 David Sagenaut Microsoft ASP .NET 1 24th Oct 2005 03:19 PM
Stored Procedure Calling VB.NET code =?Utf-8?B?QW5vbnltb3Vz?= Microsoft ADO .NET 7 6th Feb 2004 01:55 AM
Calling a Stored Procedure in a Stored Procedure Group DJM Microsoft ADO .NET 1 21st Jan 2004 08:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:24 PM.