Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

executing an sql stored procedure 1

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
I have the following code to execute a stored procedure. I receive no errors when debugging but it does not update my database. Note this stored procedure does run fine within sql so the problem is in the code below: (Thanks)

private void approveestBN_Click(object sender, EventArgs e
{


int @estno;
int.TryParse(startestnoCB.Text,out estno);


SqlCommand cmd = new SqlCommand();
cmd.CommandText = "EstimateApproval";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@estno", SqlDbType.Int);
}
 
you have the command, but you haven't done anything with the command. it's like sitting in a car, but your not getting to your destination. you have to drive the car to get there.

you will need a database connection and associate the connection to the command. you will also need to execute the command.

there are plenty of example online.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I already have a database connection but Im not sure what you mean by an execution command.
 
you need to execute the command. depending on what the stored proc does:
var table = new DataTable();
table.Load(command.ExecuteReader());
or
var obj = command.Scalar();
or
command.ExecuteNonReader();

anyone of these commands will make the call to the db to execute the sql provided. in this case a stored proc. the simpilest (although not recommended for production use) example is
Code:
using(var connection = new sqlconnection(...)
using(var command = connection.createcommand())
{
   command.CommandType =  CommandType.StoredProcedure;
   command.CommandText =  "proc_to_modify_data";
   //add parameters if necessary
   command.ExecuteNonReader();
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top