Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SqlParameter without parameter names

Posted on 2013-11-16
7
Medium Priority
?
273 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 70

Expert Comment

by:Éric Moreau
ID: 39653619
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
ID: 39653625
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 70

Expert Comment

by:Éric Moreau
ID: 39653635
does your SP returns a resultset?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39653646
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
ID: 39668844
To clarify, I am looking for positional specification with SQLParameter.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1200 total points
ID: 39668872
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 71

Expert Comment

by:Qlemo
ID: 39669124
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

879 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