Discussion Locked

Database Stored Procedures in .NET

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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.

[pre]
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";
insertCatCMD.ExecuteNonQuery();
[/pre]

7 replies

by m_a_r_k

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.

by stargazerr

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....:D

by m_a_r_k

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

by robert.coster

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.

by alaniane

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.

by cyberhacker

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.

This discussion is closed to new replies.