用下面的OleDb连接方式 ds = GetDs("pName('p1','p2')")可以传递参数,运行成功.public DataSet GetDs(String prd)
{
OleDbConnection conn=new OleDbConnection(connstr);
conn.Open();//打开连接
              
OleDbDataAdapter comm=new OleDbDataAdapter();
comm.SelectCommand=new OleDbCommand();
//设置参数
comm.SelectCommand.Connection=conn;
comm.SelectCommand.CommandText=procedure;
comm.SelectCommand.CommandType=CommandType.StoredProcedure;

comm.Fill(ds);//将结果加入dataSet对象
conn.Close();//连接关闭return ds;
}但是如果采用SqlClient的方式(如下所示),ds = GetDs("pName('p1','p2')"),出错,提示错误信息为:“找不到存储过程 pName('p1','p2')”,好像是把这个当成了一个整体了? public DataSet GetDs(String prd)
{
SqlConnection conn=new SqlConnection(connstr);
conn.Open();//打开连接
              
SqlDataAdapter comm=new SqlDataAdapter();
comm.SelectCommand=new SqlCommand();
//设置参数
comm.SelectCommand.Connection=conn;
comm.SelectCommand.CommandText=procedure;
comm.SelectCommand.CommandType=CommandType.StoredProcedure;

comm.Fill(ds);//将结果加入dataSet对象
conn.Close();//连接关闭return ds;
}
注:用上面的方法运行不需要传递参数的存储过程是正常的,可以通过得到ds 。

解决方案 »

  1.   

    SqlDataAdapter sda=new SqlDataAdapter();
    SqlParameter sp=new SqlParameter("@p1",SqlDbType.Int);
    sp.Value=3;
    sda.SelectCommand.Parameters.Add(sp);
      

  2.   

    public override bool Update()
    {
    SqlCommand objDBCommand;
    SqlParameter objParam; using( SqlConnection Conn = new SqlConnection( strCon ))
    {
    //重新获得需要的存储过程
    GetTriggerName( ObjectOfBBS.DataOperateName.Update, 1 ); objDBCommand = new SqlCommand( strTriggerName, Conn );
    objDBCommand.CommandType = CommandType.StoredProcedure; objParam = objDBCommand.Parameters.Add( "@IP", SqlDbType.VarChar );
    objParam.Value = _IP ; objParam = objDBCommand.Parameters.Add( "@Act", SqlDbType.Bit ); objParam.Value = _Act; try
    {
    Conn.Open(); objDBCommand.ExecuteScalarExecuteNonQuery();

    Conn.Close();
    }
    catch( Exception )
    {
    /*$$$把出错信息记录到LOG文件*/
    Conn.Close();
    return false;
    }
    return true;
    }
    }
      

  3.   

    to  chenyuming2004(一切从2004开始)  :还有一点疑问:为什么用OleDb就可以,而用SqlClient就不能自动解析 字符串 prd 的参数?
    在此处,二者为什么会有这样的差别?
      

  4.   

    string SQL = "exec procedure";
    DataTable dt = new DataTable();
    SqlDataAdaper sda = new SqlDataAdapter(SQL,dbConn);
    sda.Fill(dt);
      

  5.   

    还有一点疑问:为什么用OleDb就可以,而用SqlClient就不能自动解析 字符串 prd 的参数?
    在此处,二者为什么会有这样的差别?
      

  6.   

    最简单的方法
    EXEC SPLPRODURENAME "?",?,"?".......