views:

98

answers:

1

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

+4  A: 

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
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
+1: Even if I would have preferred a small example. :-)
Patrick
@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
@George - OK, hope MSFT is not territorial about their sample code though.
Steve Townsend
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
@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
@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
George Stocker