`

params SqlParameter[] commandParameters的理解

阅读更多
ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters) 



以params 声明的参数是说明参数的个数是可选的,可以为0个或多个。当多个时,以“,”分开传入


一个参数:
ExecuteReader(System.Data.CommandType.Text,"select * from table1 where id =@id",new System.Data.SqlClient.SqlParameter("@id",12));

多个参数:
ExecuteReader(System.Data.CommandType.Text,"select * from table1 where id =@id and name=@name",
new System.Data.SqlClient.SqlParameter("@id",12),new System.Data.SqlClient.SqlParameter("@name","中国"));


可以这样调用
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params   SqlParameter[] commandParameters)
        {
            //...
        }

SqlParameter   a   =   SqlParameter a = new SqlParameter("@name", "中国");
SqlParameter   b   =   SqlParameter a = new SqlParameter("@name", "中国");
SqlParameter   c   =   SqlParameter a = new SqlParameter("@name", "中国");
SqlParameter   d   =   SqlParameter a = new SqlParameter("@name", "中国");
ExecuteNonQuery(conn,   cmdType,   cmdText,   a,   b,   c,   d);



因为是params 声明的,所以这个参数你可以不填。或传入一个事先设置好的SqlParameter类型的数组。

分享到:
评论

相关推荐

    SQLHepler源代码

    public static int ExecteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection ...

    SqlHelper基类.rar

    public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, trans...

    sqlhelper类

    public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { SqlCommand command = connection.CreateCommand(); ...

    sql连接C#通用类

    public SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(sql...

    DBHelper方便连接数据库 DBHelper

    public static int ExecuteCommand(string sql, params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } ...

    .net 数据库访问类

    /// <param name="commandParameters">SqlParameter[]参数数组 /// <returns>SqlDataReader:执行结果的记录集 public SqlDataReader GetSqlReader(CommandType cmdType, string cmdText, params SqlParameter[] ...

    c#自定义异步操作数据库示例

    public async Task<int> ExecuteNonQueryAsync(string commandText, params SqlParameter[] parameters) { await _connection.OpenAsync(); using var command = new SqlCommand(commandText, _connection); ...

    C#三层架构

    public SqlDataReader ExecuteDataReader(string procName, SqlParameter[] params) { SqlCommand command = new SqlCommand(procName, conn); command.CommandType = CommandType.StoredProcedure; if ...

    ADO.NET自己封装SqlHelper类

    public static void ExecuteNonQuery(string connectionString, string sql, params SqlParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection....

    .net数据访问类 SQL Helper 类

    CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) 除这些重载以外,除 ...

    关于三层架构的操作方法

    private static void PreCommand(SqlConnection conn, SqlCommand cmd, string sql, params SqlParameter[] Parms) { //打开数据库 if (conn.State == ConnectionState.Closed) { conn.Open(); } cmd....

    阅读器关闭时 Read 的尝试无效。

    public override SqlDataReader GetSqlDataReader(string sql, params SqlParameter[] commandParameters) { SqlConnection connection = GetSqlConnection(); SqlCommand command = new SqlCommand(sql, ...

    asp.net DBHelper类的写法实例

    public DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters) { DataTable dt = new DataTable(); using (SqlConnection conn = OpenConnection()) { using (SqlCommand cmd = new ...

    封装sql类方法

    public static int ExecuteNonQuery(string connectionString, string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new ...

    数据库连接读写非常使用

    public int ExecuteNonQuery(SqlConnection connection, string sql, params SqlParameter[] parameters) { SqlCommand command = new SqlCommand(sql, connection); command.Parameters.AddRange(parameters); ...

    SQLHelper.cs

    CommandType commandType, string commandText, params SqlParameter[] commandParameters) Execute* (SqlConnection connection, string spName, params object[] parameterValues) 除这些重载以外,除 ...

    .net所有项目都用的到的一个基类

    public static int ExecuteCommand(params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(); cmd.Connection = Connection; cmd.CommandText = "Pro_UpdateBooksCategory"; cmd.CommandType = ...

    C#的SQL操作类(通用)

    public DataTable Select(string sql, params SqlParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand command = new SqlCommand(sql, ...

    CMS.DBUtility.dll

    public static bool Exists(string strSql, params SqlParameter[] cmdParms) { object obj = GetSingle(strSql, cmdParms); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System....

    params的总结

    在SQL查询中,`params`通常用于预编译的语句中,如PreparedStatement,以防止SQL注入攻击。 10. **参数对象(Parameter Objects)** 有时候,一组相关的参数可以被封装到一个对象中,形成参数对象,这有助于提高...

Global site tag (gtag.js) - Google Analytics