Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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.
0
Lawrence Avery
Asked:
Lawrence Avery
  • 4
  • 2
1 Solution
 
É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
 
Lawrence AverySystem DeveloperAuthor 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
 
Éric MoreauSenior .Net ConsultantCommented:
does your SP returns a resultset?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
É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
 
Lawrence AverySystem DeveloperAuthor Commented:
To clarify, I am looking for positional specification with SQLParameter.
0
 
Éric MoreauSenior .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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now