SqlParameter without parameter names

I need an example of using SqlParameter  in a C# program that does not specify the Parameter name when calling a stored procedure.
metro156Asked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
if you look at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx, you will find that all the overloads of the SQLParameter constructor requires the name of the parameter, none allows the position.
0
 
Éric MoreauSenior .Net ConsultantCommented:
I wander if it can be done using SqlParameter.

One thing for sure, you can do it by calling your SP in a plain query:

exec dbo.YourSPName Param1Value
0
 
metro156Author Commented:
exec dbo.YourSPName Param1Value can be executed in SQL Server studio code window.
I am asking for an example in a C# program.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Éric MoreauSenior .Net ConsultantCommented:
does your SP returns a resultset?
0
 
Éric MoreauSenior .Net ConsultantCommented:
here is an example:

try
{
    // Create and Open the SQL server connection object
    using (SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=HedgeFund_PRD;Integrated Security=SSPI;"))
    {
        con.Open();
        ////  Create a command object and specify the Stored Procedure name and connection as well
        SqlCommand cmd = new SqlCommand("exec dbo.spTest 1, 1, '1960-01-01', '2013-12-31'", con);
        //  Set the command object
        cmd.CommandType = CommandType.Text;
        // Execute the command
        using (SqlDataReader rd = cmd.ExecuteReader())
        {
            int count = 0;
            while (rd.Read())
            {
                count++;
            }
            MessageBox.Show(string.Format("There are {0} rows.", count));
        }
    }
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
0
 
metro156Author Commented:
To clarify, I am looking for positional specification with SQLParameter.
0
 
QlemoDeveloperCommented:
In the SP call, you can use parameter names you define yourself, like @P1 and following, and then use the self-defined names for filling a SQLParameter collection.
0
All Courses

From novice to tech pro — start learning today.