相关文章推荐
){outline:none;box-shadow:none;}select::-ms-expand{;}:root,:host{--chakra-vh:100vh;}@supports (height: -webkit-fill-available){:root,:host{--chakra-vh:-webkit-fill-available;}}@supports (height: -moz-fill-available){:root,:host{--chakra-vh:-moz-fill-available;}}@supports (height: 100dvh){:root,:host{--chakra-vh:100dvh;}}
Link to home
Create Account Log in
Avatar of Lawrence Avery
Lawrence Avery 🇺🇸

asked on

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.
Avatar of Éric Moreau
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
Avatar of Lawrence Avery
exec dbo.YourSPName Param1Value can be executed in SQL Server studio code window.
I am asking for an example in a C# program.
Avatar of Éric Moreau
does your SP returns a resultset?
Avatar of Éric Moreau
here is an example:
// Create and Open the SQL server connection object
using (SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=HedgeFund_PRD;Inte grated 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.For mat("There are {0} rows.", count));
catch (Exception e)
MessageBox.Show(e.Message) ;
 
推荐文章