bianma是变量,等于M000000040040300
SqlCommand cmd = new SqlCommand("zhengyingcancencu", cn);
            cmd.CommandType = CommandType.StoredProcedure; //用的方式设为存储
            cmd.Parameters.Add("@abc", SqlDbType.VarChar, 20); //填入参数
            cmd.Parameters[0].Value = bianma;
            cn.Open(); //打开连接
            SqlDataReader rdr = cmd.ExecuteReader();  //获得储存过程的行            
            dt = rdr.GetSchemaTable(); //把sqldatareader转换为datatable
            cn.Close();
出来的dt结果行数为零.
但是我在查询分析器里
exec zhengyingcancencu 'M000000040040300' 是有8条数据, 
不知道ado.net执行存储过程的语法错在那里

解决方案 »

  1.   

    我的存储过程如下
    SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GOALTER  proc zhengyingcancencu @abc varchar(20)
    as
    --先判断一下是否存在zhengyingcanlinsi临时表,如果存在就删除掉
    if object_id('zhengyingcanlinsi') is not null
    drop table zhengyingcanlinsi
    select b.invcode as 材料料号,c.cinvname as 材料名称,isnull(c.cinvdefine7,c.cinvstd)  + ' ' + isnull(c.cinvdefine6,'')  as 规格描述,e.ccomunitname as 单位,
    (f.baseqtyn / f.baseqtyd) * (1 + f.compscrap/100) as 用量,c.iSupplyType as 是否虚拟件 into zhengyingcanlinsi
    from bom_parent a inner join bas_part d on a.parentid = d.partid inner join bom_opcomponent f on 
    a.bomid = f.bomid inner join bas_part b on f.componentid = b.partid 
    inner join inventory c on b.invcode = c.cinvcode inner join ComputationUnit e on c.ccomunitcode = e.ccomunitcode and c.cgroupcode = e.cgroupcode 
    where d.invcode =@abc order by b.invcode DESC
    --如果存在虚拟件,就执行以下操作
    if exists(select * from zhengyingcanlinsi where 是否虚拟件 = 3)
      begin --先追加虚拟件材料
    insert into zhengyingcanlinsi
    select b.invcode as 材料料号,c.cinvname as 材料名称,isnull(c.cinvdefine7,c.cinvstd)  + ' ' + isnull(c.cinvdefine6,'')  as 规格描述,e.ccomunitname as 单位,
    (f.baseqtyn / f.baseqtyd) * (1 + f.compscrap/100)*g.用量 as 用量,c.iSupplyType as 是否虚拟件 
    from bom_parent a inner join bas_part d on a.parentid = d.partid inner join bom_opcomponent f on 
    a.bomid = f.bomid inner join bas_part b on f.componentid = b.partid 
    inner join inventory c on b.invcode = c.cinvcode inner join ComputationUnit e on c.ccomunitcode = e.ccomunitcode and c.cgroupcode = e.cgroupcode 
    inner join (select 材料料号,用量 from zhengyingcanlinsi where 是否虚拟件 = 3) g on d.invcode = g.材料料号
     order by b.invcode DESC
    --删除虚拟件
    delete zhengyingcanlinsi
    where 是否虚拟件 = 3
      end
    --最后查询所有的材料出来
    select * from zhengyingcanlinsiGO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
    GO
      

  2.   

    你用错方法了,应该用dt.Load(rdr);
      

  3.   

    SqlCommand cmd = new SqlCommand("zhengyingcancencu", cn); 
                cmd.CommandType = CommandType.StoredProcedure; //用的方式设为存储 
                cmd.Parameters.Add("@abc", SqlDbType.VarChar, 20); //填入参数 
                cmd.Parameters[0].Value = bianma; 
                cn.Open(); //打开连接 
                cmd.ExecuteNonQuery();
                cn.Close(); 
      

  4.   

    这样子是可以,但是我想返回一个datatable如何做呢
      

  5.   

     string strcon = "Data Source=.;Initial Catalog=test;uid=sa;pwd=11";
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand com = new SqlCommand("CheckAcc", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];        //con.Open();
            //SqlDataReader dr = com.ExecuteReader();
            //DataTable dt;
            //con.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
    不妨试下SqlDataAdapter 
      

  6.   

    不是这样的。 
    dt = rdr.GetSchemaTable(); //把sqldatareader转换为datatable 
    应该这样。
    dt.load(dr).
      

  7.   

    给你一个小小的建议,其实执行存储过程也可以这样string str ="exec zhengyingcancencu '"+bianliang+"' "
      

  8.   

    发现楼主的眼睛长天上的,我3楼明明给了正确的做法了,居然视而不见,反而看着人家的SqlDataAdapter倒去用了,dt.Load()不要太方便啊,无视就算了,我敢肯定你学不好程序。
      

  9.   

    也许是我的存储过程有问题吧,用cmd.ExecuteReader();  的方法,不能返回行,