执行这个sql,返回一个datareader

解决方案 »

  1.   

    试试:
    SqlConnection cnn = DBConnection.GetConnection();
    cnn.Open();
    SqlTransaction t = cnn.BeginTransaction();
    SqlHelper.ExecuteNonQuery(t,CommandType.Text,strQ);
    strQ = " SELECT @@identity as RowID";
    Object obj = SqlHelper.ExecuteScalar(t,CommandType.Text,strQ);
    if (obj != null) this.strAttemperID = obj.ToString();
    在同一个事务中就行。
      

  2.   

    cmd.Text=@"
    INSERT  INTO  jobs  (name)  
    VALUES  ('aaa')  
    SELECT  @@IDENTITY  AS  'Identity'  
    ";
    int id=Convert.ToInt32(cmd.ExecuteScalar());
      

  3.   


    create PROCEDURE dbo.spWarehouse_outInsert
    (@vcSheetNO varchar(50),
     @nWarehousePK numeric(9), 
     @nCustomerPK numeric(9), 
     @vcNote varchar(250), 
     @vcConsigner varchar(50), 
         @dtConsignmentTime datetime, 
         @nEditor numeric(9),
         @nResult numeric(9) output)
    AS
    INSERT INTO Warehouse_out
          (vcSheetNO, nWarehousePK, nCustomerPK, vcNote, vcConsigner, 
          dtConsignmentTime, nEditor)
    VALUES (@vcSheetNO, @nWarehousePK, @nCustomerPK, @vcNote, @vcConsigner, 
          @dtConsignmentTime, @nEditor)
          select @nResult=@@identity
           RETURN 
    把@@identity赋值给输出变量@nResult,然后在程序中获得输出参数的值:
    //新增一张出库单
    public System.Decimal InsertWarehouseOut(DataRow drWarehouseOut,SqlTransaction sta)
    {
    DataRow dr=drWarehouseOut;
    SqlParameter[] sps=new SqlParameter[8];
    sps[0]=new SqlParameter("@vcSheetNO",dr["vcSheetNO"]);
    sps[1]=new SqlParameter("@nWarehousePK",dr["nWarehousePK"]);
    sps[2]=new SqlParameter("@nCustomerPK",dr["nCustomerPK"]);
    sps[3]=new SqlParameter("@vcNote",dr["vcNote"]);
    sps[4]=new SqlParameter("@vcConsigner",dr["vcConsigner"]);
    sps[5]=new SqlParameter("@dtConsignmentTime",dr["dtConsignmentTime"]);
    sps[6]=new SqlParameter("@nEditor",dr["nEditor"]);
    sps[7]=new SqlParameter("@nResult",SqlDbType.Decimal,9);
    sps[7].Direction=ParameterDirection.Output;
    SqlHelper.ExecuteNonQuery(sta,CommandType.StoredProcedure,"spWarehouse_outInsert",sps); //执行存储过程
    System.Decimal temp=(System.Decimal)sps[7].Value;//获得输出参数的值
    return temp;
    }