SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to run a stored procedure from C# application


how to run a stored procedure from C# application

Author
Message
sankar18
sankar18
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 8
how to run a stored procedure from C# application
Programmer07
Programmer07
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 59
Try this. Just paste the code into a button's click event. You'll need to change the items shown bold to match your database settings.

string oConnString = "Data Source=localhost;Initial Catalog=ALocalDataBase;Integrated Security=True";
SqlConnection oConn = new SqlConnection(oConnString);
SqlCommand oCmd = new SqlCommand("Exec sp_AnSProc", oConn);
SqlDataAdapter oAdptr = new SqlDataAdapter(oCmd);
DataSet oDS = new DataSet();
oConn.Open();
oCmd.ExecuteNonQuery();
oCmd.CommandTimeout = 600;
oAdptr.Fill(oDS);
//uncomment the following line to view the xml representation of the data returned
//System.Diagnostics.Debug.WriteLine(oDS.GetXml());
oConn.Close();

Note: If your stored procedure requires that parameters be passed in, just use String Concatenation.

Good luck. I hope this helps.

Software Developer
I teach; therefore, I learn.
dmbaker
dmbaker
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1394 Visits: 3591
If all you want to do is execute a stored proc that returns no results then you don't need a DataAdapter/DataSet (you don't necessarily need one even if you do want a resultset -- it depends on what you want to do with the results). You also don't need the "Exec" in your command text is you set the CommandType property of your SqlCommand to "StoredProcedure".

string connectionString = "(your connection string here)";
string commandText = "usp_YourStoredProc";

using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(commandText, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 600;

conn.Open();
int affectedRows = cmd.ExecuteNonQuery();
conn.Close();

}


If you want to pass parameters, it'd also be better to define SqlParameter objects for the parameters, set their values, then add those to the command object, rather than concatenating them into the command text.



shr_khr
shr_khr
SSC Rookie
SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)SSC Rookie (38 reputation)

Group: General Forum Members
Points: 38 Visits: 54
You can check this article HOW TO: SQL & C# to learn how to call a stored procedure in a MS SQL database from a C# application as well as a Java application.

You will learn how to pass parameters to the stored procedures as well as read Out parameters returned by the sp.

Also, you will learn to how pass parameterized SQL queries from the application and read data returned by the queries.

Shahriar Nour Khondokar
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search