Simple Questtion ADO.NET/Stored procedures

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can i invoke a stored procedure using ado.net?
is it simple using the sqlcommand object with the "exec" key word? please
let me know. a simple example/sample code would be great
Thanks
 
Lottoman2000 NEWBE said:
How can i invoke a stored procedure using ado.net?
is it simple using the sqlcommand object with the "exec" key word? please
let me know. a simple example/sample code would be great
Thanks

Language?

SqlConnection conn = new SqlConnection(ConnectionStringHere);
conn.Open();

try {
SqlCommand cmd = new SqlCommand("nameofsp", conn);

// If the command selects data and you want to fill a dataset.
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds, "tblTable"); // Adds table tblTable to dataset and fills
it with select results.

// If the command is an update/delete and you want to retrieve the
results.
int rowsAffected = cmd.ExecuteNonQuery();

// If the command selects data and you want the first column of the
first row's value.
object value = cmd.ExecuteScalar(); // Might have this and
ExecuteNonQuery mixed up.
} catch (SqlException ex) {
// Database exception occurred.
throw new CustomDatabaseExceptionMaybe("A database exception has been
thrown.", ex);
return;
} finally {
// Cleanup.
conn.Close();
}

// do anything you want with the results dataset or any other results you
received.


Hope this helps :)

Mythran
 
SqlConnection conn = new SqlConnection(ConnectionStringHere);
conn.Open();

try {
SqlCommand cmd = new SqlCommand("nameofsp", conn);

You might want to add this line, too:
cmd.CommandType = CommandType.StoredProcedure;
// If the command selects data and you want to fill a dataset.
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds, "tblTable"); // Adds table tblTable to dataset and fills
it with select results.

Marc
 
Back
Top