×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

ASP question please help!! thanks!!

How should i do to get the rs(0) value?

..................
sql="select * from studentinfor"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.open sql,connstr,3,2
rs.addnew ( rs(0) is an auto number from 1, step is 1 )

rs(1)="123"
rs(2)="abc"
rs(3)="picture_" & rs(0) (but the rs(3) get nothing, why i can not get rs(0) value )
(rs(3) photo file)

rs.update

rs.close
set rs = nothing
.....................


How should i do to get the rs(0) value?
Report

Replies, comments and Discussions:

  • 工作学习 / IT杂谈 / ASP question please help!! thanks!!
    How should i do to get the rs(0) value?

    ..................
    sql="select * from studentinfor"
    Set rs = Server.CreateObject("ADODB.Recordset")
    rs.open sql,connstr,3,2
    rs.addnew ( rs(0) is an auto number from 1, step is 1 )

    rs(1)="123"
    rs(2)="abc"
    rs(3)="picture_" & rs(0) (but the rs(3) get nothing, why i can not get rs(0) value )
    (rs(3) photo file)

    rs.update

    rs.close
    set rs = nothing
    .....................


    How should i do to get the rs(0) value?
    • 你update之后,再retrieve rs(0)的值为多少?
      • this is the program look like after i change. but It still does not work.
        this is the look after i change.
        It still does not work.
        please show me how! thank you!!


        sql="select * from studentinfor"
        Set rs = Server.CreateObject("ADODB.Recordset")
        rs.open sql,connstr,3,2
        rs.addnew

        rs(1)="123"
        rs(2)="abc"

        rs.update


        rs(3)="picture_" & rs(0) (but the rs(3) get nothing, why i can not get rs(0) value )
        rs.update

        rs.close
        set rs = nothing
        • 不好意思,我没用过ASP.帮你up一下.看你的code,update之后,你在用一次selec to get the value of rs(0), i think you may not be able to get the rs(0) value before you write it back to database.
    • 你这样试试看
      sql="select * from studentinfor"
      Set rs = Server.CreateObject("ADODB.Recordset")
      rs.open sql,connstr,3,2
      rs.movelast
      id=rs(0)+1
      rs.addnew ( rs(0) is an auto number from 1, step is 1 )

      rs(1)="123"
      rs(2)="abc"
      rs(3)="picture_" &id (but the rs(3) get nothing, why i can not get rs(0) value )
      (rs(3) photo file)

      rs.update

      rs.close
      set rs = nothing
      • one person use ok, but if many people add new date at same time. it may have problem
    • try this
      sql="select * from studentinfor"
      Set rs = Server.CreateObject("ADODB.Recordset")
      rs.open sql,connstr,2,3 (this line has been modified)
      rs.addnew
      rs(1)="123"
      rs(2)="abc"
      rs.update
      rs(3)="picture_" & rs(0)
      rs.update

      rs.close
      set rs = nothing
      'not tested...
      • should add rs.movelast between rs.update and rd(3)="picture"&rs(0)
      • not work !!
        • well tested...working fine
          sql="select * from studentinfor order by (your auto id field name)"
          Set rs = Server.CreateObject("ADODB.Recordset")
          rs.open sql,connstr,2,3 (this line has been modified)
          rs.addnew
          rs(1)="123"
          rs(2)="abc"
          rs.update
          rs.movelast
          temp=rs(0)
          rs(3)="picture_" & temp
          rs.update

          rs.close
          set rs = nothing
          • 你这个是不保险的。你用lock了没有?什么模式的recordset?如果在你update和movelast之间有另外一个人也作了insert,那么你得到的是谁的new ID? That's why I said we should use trigger.
            • I agree !
            • ok,you are right,change to rs.open sql,connstr,2,2---------2 means recoed locked before and after you edit utill you destroy the recordset
              • by the way,what is the version of ASP?if you have 3.0(win2000),i will give you better code
                • ASP 3。0
                  • here you go....
                    sql="select * from studentinfor"
                    Set rs = Server.CreateObject("ADODB.Recordset")
                    with rs
                    .open sql,connstr,2,2
                    .addnew
                    .fields(1)="123"
                    .fields(2)="abc"
                    .update
                    temp=.fields(0)
                    .fields(3)="picture_" & temp
                    .update
                    END with
                    ..............

                    not tested,you may test again
    • 这样试试
      AutoID时数据存到数据库的时候自动产生的,你这样是取不到值的
      你这样试
      .................
      sql="select * from studentinfor"
      Set rs = Server.CreateObject("ADODB.Recordset")
      rs.open sql,connstr,3,2

      rs.movelastrecord(这句话你自己去看help,我记得不是太清楚)
      ID=int(trim(rs(0)))+1

      rs.addnew

      rs(1)="123"
      rs(2)="abc"
      rs(3)="picture_" & cstr(id)

      rs.update

      rs.close
      set rs = nothing
      .....................


      How should i do to get the rs(0) value?
    • 你是想要获得 identity field 的自增值, 然后用在下一条record? 如果条件容许, you can use "store procedure", plus "command" object 使用 global variable "@@identity "
      • 我是想要获得 identity field 的自增值, 然后用在本条record
        • 看我下面的贴子,这样的话,你就要用trigger了。
    • 我的小字报:
      1。you can not get the identity number before you finish the 'Update' or 'Insert'.
      in your code, you was using like this way:
      rs(1)="123"
      rs(2)="abc"
      rs(3)="picture_" & rs(0) (but the rs(3) get nothing, why i can not get rs(0) value )
      Of cause you can NOT get rs(0), it has not been inserted yet!

      2. in this case, you should use another column(field) to store your rs(0) (i.e, uni_num) as soon as it is intered, (you can put this in a trigger), then do a 'Update' to get the value of uni_num, combine it with your rs(3) and save it into DB.

      so your code should like this:
      rs(0) (auto number)
      ...
      rs(3) = ' ' (it's blank in this Insert)
      rs(4) (new field, same as rs(0))
      .update

      then get this record again.
      rs(1)="123"
      rs(2)="abc"
      rs(3)="picture_" & rs(4)
      .update

      3. of cause you can put all of them into a Insert trigger, so when you do the insert, you can generate the rs(3) immdieatly.
      • 谢谢!!!
    • Depend on different database
      If Access or Ms SQL
      right after insert query, run SELECT @@identity to get the autoinc field's value.

      If Oracle or Interbase, directly get the value from
      sequence or generator