Legacy ADO Setup: params sqlparameter[] Commandparameters

Source: Internet
Author: User

C # code
ExecuteReader (String connectionString, CommandType commandtype, string commandtext, params sqlparameter[] Commandparameters)

The parameters declared with the params indicate that the number of arguments is optional and can be 0 or more.
The parameter array must be at the end of the formal parameter list if the parameter in the params declaration contains an array of arguments.
The parameter array declared with the params must be a one-dimensional array;
When the parameter declared by the params is multiple, it is passed in "," separately.

One parameter:
C # code
ExecuteReader (System.Data.CommandType.Text, "select * from table1 where ID [email protected]", new System.Data.SqlClient.SqlParameter ("@id", 12));

Multiple parameters:
C # code
ExecuteReader (System.Data.CommandType.Text, "select * from table1 where ID [email protected] and [email protected]",
New System.Data.SqlClient.SqlParameter ("@id", New System.Data.SqlClient.SqlParameter ("@name", "China"));
Or:
public static int ExecuteNonQuery (SqlConnection connection, CommandType Cmdtype, String cmdtext, params sqlparameter[] C Ommandparameters)
{
//...
}
SqlParameter a = new SqlParameter ("@id", "12");
SqlParameter B = new SqlParameter ("@name", "China");
ExecuteNonQuery (conn, Cmdtype, Cmdtext, A, b);
Or:
Sqlparameter[] Paras =new sqlparameter[]{
New SqlParameter ("@id", "12"),
New SqlParameter ("@name", "China")
};
ExecuteNonQuery (conn, Cmdtype, Cmdtext, paras);

This parameter can be filled out because it is a params declaration. or pass in an array of pre-set SqlParameter types.

Attention:

When the SqlParameter constructor is: SqlParameter (String, object)
When you specify object in the value parameter, SqlDbType is inferred from the. NET Framework type of object.

Be careful to use this overload of the SqlParameter constructor to specify the integer parameter value. Because this overload accepts value of type object, when this value is zero, the integer value must be converted to the object type, as shown in the following C # example.

Parameter = new SqlParameter ("@pname", Convert.ToInt32 (0));

If the conversion is not performed, the compiler will assume that you are attempting to invoke the SqlParameter (string, SqlDbType) constructor overload

Explanation: 1: The popular point is that 0 is the default SqlDbType set.
Almost equivalent to using NULL when setting default parameters in other languages
The Convert.ToInt32 (0) is equivalent to escaping, converting 0 of the default parameter to the True value 0
2: The type of 0 represented in programming: can be true, can be considered null, or it can be an integer.
So it must be given a certain type.


The first type:
SQLCOMMAND1.PARAMETERS.ADD (parameter name, parameter value);
The parameter value is 0 o'clock and must be converted to type Object
If the conversion is not performed, the compiler will assume that you are attempting to invoke the SqlParameter (string, SqlDbType) constructor overload
Or use the second, determine the parameter type, and assign a value that does not require conversion
The second type:
SQLCOMMAND1.PARAMETERS.ADD (parameter name, parameter type, parameter length, name of the column in which the parameter is located);
Then sqlcommand1.parameters[the value of the "parameter name"].value= parameter;
or SqlCommand1.Parameters.Add (parameter name, parameter type, parameter length, name of the column in which the parameter is located). The value of the value= parameter;

Legacy ADO Setup: params sqlparameter[] Commandparameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.