Web Development

General discussion


Database Stored Procedures in .NET

By M_a_r_k ·
I am having a problem using .NET to implement a stored procedure for inserting a row into a database. FYI, I am using C# and SQL Server. None of the code examples in MSDN show the specific table that is being accessed. What parameter of what object do I set to provide the table name. The code shown below is directly from MSDN. It somehow inserts into the table 'Category'. But where in the world is this table name given in the code? This example is from MSDN 2003, .NET Framework->Programming wiht .NET Framework->Using .NET Framework Data Providers...->Performing Database Operations...->Modifying Data in a Database. Thanks in advance.

SqlCommand insertCatCMD = new SqlCommand("InsertCategory" , nwindConn);
insertCatCMD.CommandType = CommandType.StoredProcedure;

SqlParameter workParm;

workParm = insertCatCMD.Parameters.Add("@RowCount", SqlDbType.Int);
workParm.Direction = ParameterDirection.ReturnValue;

workParm = insertCatCMD.Parameters.Add("@CategoryName", SqlDbType.NChar, 15);

workParm = insertCatCMD.Parameters.Add("@Identity", SqlDbType.Int);
workParm.Direction = ParameterDirection.Output;

insertCatCMD.Parameters["@CategoryName"].Value = "New Category";

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

Never mind. I figured it out.

by M_a_r_k In reply to Database Stored Procedure ...

I couldn't see the forest through the trees. I figured out what I was doing wrong. Dumb mistake. I thought I had to create my stored procedure within my own code. I didn't realize I could just write code to call an existing stored procedure in the database. It makes sense now. So my problem was that of course the DB table isn't in the .NET code; it's in the stored procedure in the database.

Collapse -

There you go !!!

by stargazerr In reply to Never mind. I figured it ...

Good that I saw your second post in this thread ... I was trying to work my head around the procedure too (completely forgotten was the fact that we can call existing stored procedures).....Silly me....

Collapse -

Thanks for your efforts

by M_a_r_k In reply to There you go !!!

It's my first time to write code to access a database. Do learning curves ever get any flatter?

Collapse -

They do get flat

by geoswill In reply to Thanks for your efforts

Learning curves do get very flat!

Soon you'll be able to code this in your sleep.

Collapse -

Check out LINQ

by robert.coster In reply to They do get flat

You should look at the new abilities in SQL Server 2005 - putting C# code in the DB adds a whole new dimension to dealing with these kind of issues.

Also google on Microsoft and LINQ. Its like embedded SQL on steroids.

Collapse -

If you need performance

by alaniane In reply to Check out LINQ

then avoid using the C# or VB within SQL Server 2005 to write simple procs. Stored procs still perform much more efficient. However, for something complex like graphic manipulations then using C# or VB within SQL Server maybe the only option or it at least it will make it simpler to write the code.

Collapse -

What table?

by cyberhacker In reply to Database Stored Procedure ...

There is a stored procedure in your first line called "InsertCategory". Find this stored procedure inside of sql server enterprise manager,
open it and see what table it uses. It seems to me that the stored proc name is indicating that it has its eyes onthe Category table.

Related Discussions

Related Forums