ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
谁能帮我解释下,如果我要用这个方法,commandParameters这里应该填什么样的参数阿?最好能给我举个例子,不甚感激!!!

解决方案 »

  1.   


    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","中国"));//如果CommandType是StoredProcedure,则string commandText就是相应的存储过程名
      

  2.   

    插入语句
    是SQL语句参数. string _SqlCommand = "Insert into Table(a,b,c,d)values(@a,@b,@c,@d)";            System.Data.SqlClient.SqlParameter[] _Parameter = new SqlParameter[]
                {
                    new SqlParameter("@a",SqlDbType.Int),
                    new SqlParameter("@b",SqlDbType.Int),
                    new SqlParameter("@c",SqlDbType.Int),
                    new SqlParameter("@d",SqlDbType.Int)
                };            _Parameter[0].Value = 10;
                _Parameter[1].Value = 20;
                _Parameter[2].Value = 30;
                _Parameter[3].Value = 40;
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Siemens.BPM.Proposal.DAO;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;namespace Siemens.BPM.Proposal.Services
    {
        public class ProjectService
        {
                  public SqlDataReader GetProjectInfo(string RefID)
            {
                string sql = "select * from view_Project where ProjectRefID =@projectRefID";
                SqlDataReader dr = SqlHelper.ExecuteReader(ConnectionService.ConnectionString, CommandType.Text, sql, new System.Data.SqlClient.SqlParameter("@projectRefID", "RefID"));
                return dr;
            }
        }
    }这是我的一个service.cs的文档,这样写对吗?
    这个“new System.Data.SqlClient.SqlParameter”会不会有冲突??
      

  4.   

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

  5.   


    比如 SqlDataReader dr = SqlHelper.ExecuteReader(ConnectionService.ConnectionString, CommandType.Text, sql, ("@projectRefID", "RefID")); ????
      

  6.   

    可以不填,或者SqlParameter数组,SqlParameter一个或者多个实例!
      

  7.   

    可以填null或是按我1楼那样写.或是提前定义SqlParameter[]数组,然后传进来
      

  8.   

    比如 SqlDataReader dr = SqlHelper.ExecuteReader(ConnectionService.ConnectionString, CommandType.Text, sql, ("@projectRefID", "RefID")); ???? 
    -------------
    很明显,你的Sql语句,需要两个参数,当然不能不传,单对这个方法本身来说,是可以不传的。
    可以传个数为2的SqlParameter数组,也可以直接使用两个实例化的SqlParameter对象传入,如:SqlDataReader dr = SqlHelper.ExecuteReader(ConnectionService.ConnectionString, CommandType.Text, sql, new SqlParameter("@projectRefID",value), new SqlParameter("RefID", value2)));以params 声明的参数是说明参数的个数是可选的,可以为0个或多个。
      

  9.   

    不好意思,上面有行写错了,应该是string sql = "select * from view_Project where ProjectRefID =@RefID"; 
    应该是只有一个参数吧,下面这样写可以伐??
    SqlHelper.ExecuteReader(ConnectionService.ConnectionString, CommandType.Text, sql, new SqlParameter("@projectRefID", "RefID")); 
      

  10.   

    commandText是参数名也可以是存储过程名CSDN里有个大气象的B2C源码下载的,你下来看一下就行了,那里面写的很清楚,还有注释当然,网上有个通用的DBHelper.cs