Databases Reference
In-Depth Information
Try It Out: Handling a Database Exception (Part 2):
Stored Procedure Error
Now you'll see what happens when a statement in a stored procedure encounters an
error. You'll create a stored procedure that attempts an illegal
INSERT
, and then you'll
extract information from the
SqlException
object.
1.
Using SSMSE, create a stored procedure in Northwind named
sp_DbException_2
,
as follows:
create procedure sp_DBException_2
as
set nocount on
insert into employees
(
employeeid,
firstname
)
values (50, 'Cinderella')
2.
Insert the code in Listing 16-5 into the
button4_Click
method.
Listing 16-5.
button4_Click()
// create connection
SqlConnection conn = new SqlConnection(@"
data source = .\sqlexpress;
integrated security = true;
database = northwind
");
// create command
SqlCommand cmd = conn.CreateCommand();
// specify stored procedure to be executed
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_DbException_2";
Search WWH ::
Custom Search