Variable params parameters and variable params Parameters
Namespace params variable parameters
{
Class Program
{
Static void Main (string [] args)
{
Int [] num = {66,99, 55,44 ,};
Test ("Old Wang", num );
Test ("Old Wang", 55); // you can know where the parameters of the method are. The Old Wang should be followed by an array, but the content of the array cannot be modified once it is determined.
// But to change the array content, add a params variable parameter to the method (but the variable content type must be the same as the array type), but this should be placed at the end of the method,
// For example, to add a student ID
Console. ReadKey ();
// Int B = Sum (56, 56, 56 );
// Console. WriteLine (B );
Console. ReadKey ();
}
Public static void Test (string name, params int [] score)
{
Int sum = 0;
For (int I = 0; I <score. Length; I ++)
{
Sum + = score [I];
}
Console. WriteLine ("My name is {0}, my total score is {1}", name, sum );
}
Public static int Sum (params int [] sum)
{
Int a = 0;
For (int I = 0; I <sum. Length; I ++)
{
A + = sum [I];
}
Return;
// Console. WriteLine ();
}
}
}
========================================================== ========================================================== ====================
Sometimes for the convenience of graphs, the sqlhelper file will be used directly for related operations, and the following classes will appear:
public
static
object
ExecuteScalar(
string
sqlStr,
params
SqlParameter[] parameters)
{
using
(SqlConnection conn =
new
SqlConnection(connStr))
{
conn.Open();
using
(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sqlStr;
cmd.Parameters.AddRange(parameters);
return
cmd.ExecuteScalar();
}
}
}
Generally, there are two calling methods:
I. Add Method
SqlParameter sp = new SqlParameter("@name", "Pudding");cmd.Parameters.Add(sp);sp = new SqlParameter("@ID", "1");cmd.Parameters.Add(sp);
This method can only add one SqlParameter at a time. The function of the above Code is to update the name of the field with the ID value equal to 1 to Pudding ).
Ii. AddRange Method
SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name", "Pudding"), new SqlParameter("@ID", "1") };cmd.Parameters.AddRange(paras);
In my actual operation, cmd. Parameters. AddRange (paras) is not found because Parameters have been added to the ExecuteScalar method. Obviously, the Add method is inconvenient when multiple SqlParameter values are added. In this case, you can use the AddRange method.