Solved

SqlParameter without parameter names

Posted on 2013-11-16
7
244 Views
Last Modified: 2013-11-22
I need an example of using SqlParameter  in a C# program that does not specify the Parameter name when calling a stored procedure.
0
Comment
Question by:metro156
  • 4
  • 2
7 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
 

Author Comment

by:metro156
Comment Utility
exec dbo.YourSPName Param1Value can be executed in SQL Server studio code window.
I am asking for an example in a C# program.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
does your SP returns a resultset?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
 

Author Comment

by:metro156
Comment Utility
To clarify, I am looking for positional specification with SQLParameter.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 400 total points
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now