Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm new to object oriented programmin in C#. We are asked to use only the stored procedures to program. Can tou please explin how to do it by using a simple windows form application.Can you please explin how to do the insert operation as an example.
Posted
Comments
krumia 27-Mar-12 3:05am    
There is no such thing called 'stored procedures' in C#. Stored procedures exist in database languages such as MS/SQL and Oracle PL/SQL.

What you might want to do is build an object oriented system with a database to achieve persistence. If so, let us know what you have already done. If you haven't done anything, try to reword your question and try to tell us clearly what you want to do.

We can't really do that. Stored Procedures are SQL, not C#, and Sql has no concept of Object Oriented Programming - it's SPs are strictly procedural, rather than object oriented.

So how could we give you any example from that description?
 
Share this answer
 
You can create procedures first after that for every procedure you can create C# methods. It is called Mapping. You can look at the example below

Create Procedure prcAddCategory
@catName nvarchar(50)
as
Insert into Categories(CategoryName) values (@catName)

and for this procedure you can create the C# method below to execute procedure

public void AddCategory(string categoryName)
{
SqlConnection conn = new SqlConnection(yourConnectionString);
SqlCommand cmd = new SqlCommand(prcAddCategory,conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@catName,categoryName);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

}

If you have error,I can check...
 
Share this answer
 
Comments
integralsun 29-Jul-13 11:20am    
Solution 2 could be improved by applying Dependency Inversion Principle (DIP). Passing in a connection that is already configured allows for future changes to types of persistence stores that can be added or replaced.
Legacy applications built using a client-server model where the stored procedures have much of the business domain logic need to be redesigned to an n-tier architecture so that you can get/reap the benefit of OO designs.
 
Share this answer
 
Try this Create a stored procedure in SQL.......







SqlConnection con = new SqlConnection("Data Source=USER-VAIO;Initial Catalog=info;Integrated Security=True");
SqlCommand cmd = new SqlCommand("login2_test",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter ss = new SqlParameter();
SqlParameter ss1 = new SqlParameter();
ss.ParameterName = "@name";
ss1.ParameterName = "@age";
ss.Value = textBox17.Text;
ss1.Value = textBox18.Text;
cmd.Parameters.Add(ss);
cmd.Parameters.Add(ss1);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900