Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

I want to use stored procedure in my web based application. It is develop on Visual Studio 2005 and using database Sql Server 2005.
Please also provide C# code to easy understanding.

It is provide for all operation as Create procedure, insertion, Updateion, Deletion and Select result(select query)
Posted

 
Share this answer
 
Comments
[no name] 27-Sep-14 22:10pm    
This is a nice step by step article.It will help you a lot.
 
Share this answer
 
Comments
[no name] 27-Sep-14 16:03pm    
I think your set of links are great. A 5. Regards, Bruno
Maciej Los 29-Sep-14 10:45am    
Thank you, Bruno ;)
Hi,

you can try some thing like this


create procedure insert_data
(
    @Id int, @Name Varchar(20) , @address varchar(50)
)
begin

Insert into table1(Id,Name,address) values(@Id,@Name,@address)

end



you can use the following c# code on button click to insert data

C#
try
  {
     sqlConnection  con = new SqlConnection(dbConnectionString);
     SqlCommand comm= new SqlCommand("insert_data", con);
     comm.CommandType = CommandType.StoredProcedure;
     comm.Parameters.Add("@Id", SqlDbType.VarChar).Value = txtId.Text;
     comm.Parameters.Add("@Name", SqlDbType.DateTime).Value = txtName.Text;
     comm.Parameters.Add("@Address", SqlDbType.DateTime).Value = txtaddress.Text;
     sqlConnection.Open();
     return comm.ExecuteNonQuery();
     sqlConnection.Close();
  }
catch (SqlException ex)
  {
     Console.WriteLine("SQL Error" + ex.Message.ToString());
     return 0;
  }


I hope it will help you..
 
Share this answer
 
v2
You can try this-
C#
SqlCommand command = new SqlCommand("dbo.sp_name", (SqlConnection)session.Connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@param_name1", "Param Value 1"));
command.Parameters.Add(new SqlParameter("@param_name2", "Param Value 1"));
command.ExecuteNonQuery();

Thanks.
 
Share this answer
 
//this is write in your button event
static SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DbCon"].ConnectionString);

con.open();
SqlCommand cmd = new con.createcommand;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@paramName1", "Param Value 1"));
cmd.Parameters.Add(new SqlParameter("@paramName2", "Param Value 1"));
cmd.ExecuteNonQuery();
con.close();

//create store procedure
CREATE PROCEDURE [dbo].[ProcedureName]
@paramName1 int = 0,
@paramName2 int
AS
SELECT @paramName1, @paramName2
RETURN 0

hope it useful to you...
 
Share this answer
 
 
Share this answer
 
string constr=@"Data Source=Server Name;Initial Catalog=Db name;Integrated Security=true;
SqlConnection con= new SqlConnection(constr);
SqlCommand com = new SqlCommand("StoredProcedureName,constr")
SqlCommandType = commandType.StoredProcedure;
con.Open();
com.Parameters.AddWithValue(@Param,Value);
com.Parameters.AddWithValue(@Param,Value);
com.Parameters.AddWithValue(@Param,Value);
com.ExecuteNonQuary();
con.Close();
 
Share this answer
 
Comments
[no name] 4-Oct-14 7:17am    
The question is over a year old. Already has plenty of answers. Your solution is just a repeat of already posted solutions and does not add anything. Your code is unformatted. Your code will not compile anyway.
victowork 4-Oct-14 7:29am    
hei man will u neglect the things in ur coding if it was one year old, there are lot more freshers who need these type of ques & ans this is what the site is build for if its annoying to you go to any other where you can have only Fresh things in the world
[no name] 4-Oct-14 7:54am    
Sorry. You are wrong. Does this solution add anything to the solution already here? No it does not. Is the code readable? No it is not. Will the code compile/be useful? No it will not.
If you think that writing childish phrases like "hei", "u", "ques", "ans" makes you sound professional, you are mistaken. Don't think for a minute that you are able to tell me what to do.
CHill60 4-Oct-14 11:31am    
Shame I can't up vote your comment. Well said anyway
[no name] 5-Oct-14 6:54am    
Thanks. It's the thought that counts. :-)

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