473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to excecute a stored procedure

I have a SQL stored procedure that I need to run by pressing a button on a .Net form. I've created a SQLConnection, the form and a button, what code do I need to run the procedure.
Jul 21 '05 #1
3 1514
Oldhandandy <Ol*********@discussions.microsoft.com> wrote:
I have a SQL stored procedure that I need to run by pressing a button
on a .Net form. I've created a SQLConnection, the form and a button,
what code do I need to run the procedure.


Set the command text of a SqlCommand to the stored procedure call, use
parameters as normal, and set the command type to StoredProcedure.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Oldhandandy <Ol*********@discussions.microsoft.com> wrote:
The command now works. What coding do I need behind the button to run
the procedure.


Just use the command exactly as you would any other - use
ExecuteNonQuery, or use it in a DataAdapter etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Hi there

I remember first getting to grips with ADO.NET after using ADO and found it a nightmare so I thought I'd give you a couple of examples to get you going:

// This example populates a dataset with the results of the stored procedure
// i.e. an SP that returns recordsets.

SqlCommand _sqlcom = new SqlCommand("uspOrderRetrieval");
_sqlcom.CommandType = CommandType.StoredProcedure;

SqlDataAdapter _sqladp = new SqlDataAdapter();

// The following code adds a parameter to be used by the stored procedure
// if you have no parameters then obviously ignore it.
SqlParameter _sqlpar = new SqlParameter("@OrderID", SqlDbType.Int);
_sqlpar.Value = orderid;
_sqlcom.Parameters.Add(_sqlpar);
// end of parameter addition

// Execute Stored Procedure
_sqlcon.Open();
_sqlcom.Connection = _sqlcon;
_sqladp.SelectCommand = _sqlcom;
_sqladp.Fill(_ds);

// The following example executes a nonquery stored procedure i.e. an SP
// that does some updates but does not return recordsets.

System.Data.SqlClient.SqlParameter _sqlpar;

SqlCommand _sqlcom = new SqlCommand("uspOrderPrinted");
_sqlcom.CommandType = CommandType.StoredProcedure;

// OrderID
_sqlpar = new SqlParameter("@OrderID", SqlDbType.Int);
_sqlpar.Value = orderid;
_sqlcom.Parameters.Add(_sqlpar);

// Execute Stored Procedure
_sqlcon.Open();
_sqlcom.Connection = _sqlcon;
_sqlcom.ExecuteNonQuery();

Please note I have not created the _sqlcon (SqlConnection) object in the examples and have not closed the connection afterwards etc etc.

Hope that helps

Andy
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
3
by: Oldhandandy | last post by:
I have a SQL stored procedure that I need to run by pressing a button on a .Net form. I've created a SQLConnection, the form and a button, what code do I need to run the procedure.
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.