4

--Stored procedure

  ALTER PROCEDURE [dbo].[Test]             
@USERID varchar(25)              

 AS               
 BEGIN                  
SET NOCOUNT ON                    
IF NOT EXISTS Select * from Users where USERID = @USERID)         
    BEGIN                          
        INSERT INTO Users (USERID,HOURS) Values(@USERID, 0);                   
    END

I have this stored procedure in sql server 2005 and want to pass userid from a C# application. How can I do that. Many Thanks.

Ani
  • 598
  • 5
  • 13
  • 29
  • possible duplicate of [How to execute a stored procedure from c# program](http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-from-c-program) – George Stocker Sep 27 '10 at 17:29
  • @John Fisher We encourage questions that could be answered with a Google search. That way when people Google, an authoritative source like Stack Overflow comes up, instead of the traditional hundreds of forums. – George Stocker Sep 27 '10 at 17:31
  • 1
    @George: That makes some sense, but don't you think MSDN is authoritative enough? – John Fisher Sep 27 '10 at 17:39
  • @John/@George. I asked a q on the metasite about this a while back. Personally I don't think it's SO's job to include by value all relevant code samples on the entire Internet, but to act as an authoritative source for technical information, sometimes by val and sometimes by ref. Still, opinions vary - that's one big reason why SO is so cool. – Steve Townsend Sep 27 '10 at 17:59
  • @Steve Townsend the reason having it here is better than having it elsewhere is: What happens if that site goes down? is unavailable? The person just takes everything down? In this case, it's MSDN, but we're not always so lucky. – George Stocker Sep 27 '10 at 18:53

1 Answers1

9

This topic is extensively covered in MSDN here. See the section entitled "Using Parameters with a SqlCommand and a Stored Procedure" for a nice sample:

static void GetSalesByCategory(string connectionString, 
    string categoryName)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "SalesByCategory";
        command.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@CategoryName";
        parameter.SqlDbType = SqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = categoryName;

        // Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);

        // Open the connection and execute the reader.
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • Stack Overflow should be the place where we keep the code; not a third party site if at all possible. This question is also a duplicate of another question: http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-from-c-program – George Stocker Sep 27 '10 at 17:30
  • 1
    @George Stocker: But the link you added doesn't have any accepted answer, and is about *calling* stored procedures, not passing arguments to them.. – Patrick Sep 27 '10 at 17:32
  • @George - OK, hope MSFT is not territorial about their sample code though. – Steve Townsend Sep 27 '10 at 17:33
  • I was looking for this approach. I found a good article on this link.http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx but this example was more helpful.@Steven Many Thanks. – Ani Sep 27 '10 at 17:39
  • @Patrick Doesn't matter whether there's an accepted answer or not: This answer on that question answers the question: http://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-from-c-program/1260998#1260998 – George Stocker Sep 27 '10 at 18:47
  • @George - just because you dug thru the answers to a related question does not mean OP will. Patrick is correct - that's a different question from this, which makes your dup call on this questionable, imo – Steve Townsend Sep 27 '10 at 19:05
  • @Steve I picked the first duplicate in a mountain of duplicates: http://www.google.com/#q=site:stackoverflow.com+add+parameters+to+stored+procedure+C%23&hl=en&prmd=iv&ei=1e2gTIKbM8OblgfCma3XCg&start=10&sa=N&fp=4e781b66e30e329a – George Stocker Sep 27 '10 at 19:18