Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello!

i have one stored prosedure with two parameters
proc_udatabase (@ string1, @string2)

how to call from c sharp
i do this like
string querry = ("proc_urdatabase" + "name" + "24")

i want to check weather it exist in database or not
how to do it in c sharp
sql server side its clear can u help me out please
Posted
Updated 5-Jul-11 22:44pm
v2

Try:
using (SqlCommand cmd = new SQLCommand("proc_urdatabase", con))
   {
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@string1", "hello!");
   cmd.Parameters.AddWithValue("@string2", "goodbye");
   SqlDataReader reader = cmd.ExecuteReader();
   while (reader.Read())
      {
      ...
      }
   }



"StoredProcedureCommand.ExecuteReader();
this statement is not working
command.ExecuteReader(); is working but after this while condition is not working"


It depends on your stored procedure: if it does not return any values, then the reader will be empty. Are you returning a single scalar value instead? If so, this may help: Retrieving Scalar Data from a Stored Procedure[^]

[edit]StoredProcedureCommand changed to cmd - oops![/edit]
 
Share this answer
 
v2
Comments
kami124 6-Jul-11 5:27am    
StoredProcedureCommand.ExecuteReader();
this statement is not working
command.ExecuteReader(); is working but after this while condition is not working
OriginalGriff 6-Jul-11 6:10am    
Answer updated
You need to add parameters to the command.
SqlParameter inparm = cmd.Parameters.Add("@strng1", SqlDbType.String);
 inparm.Direction = ParameterDirection.Input;
 inparm.Value ="name";


More details shown in this[^] tutorial.
 
Share this answer
 
Are you looking for this?

Using Stored Procedures with a Command[^]
 
Share this answer
 
Try this code:

using (SqlCommand cmd = new SQLCommand("proc_urdatabase", con))
   {
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@string1", "Hello");
   cmd.Parameters.AddWithValue("@string2", "World");
   SqlDataReader reader = StoredProcedureCommand.ExecuteReader();
   while (reader.Read())
      {
  Your Logic
}
   }
 
Share this answer
 
v3
Comments
kami124 6-Jul-11 5:09am    
StoredProcedureCommand.ExecuteReader();
this statement is not working
command.ExecuteReader(); is working but after this is now executing while condition
kami124 6-Jul-11 5:10am    
StoredProcedureCommand.ExecuteReader();
this statement is not working
command.ExecuteReader(); is working but after this while condition is not working

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