Generate a new guid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason L James

    Generate a new guid

    Hi all,

    is there a method or property of a class that will
    return a GUID in VB.NET that I can use as
    the PK of my DB. I do not want SQL to generate
    it as I can not then use

    SELECT @@IDENTITY to retrieve it.

    I want to try and mimic the functionality of

    newID()

    that SQL server has.

    Thanks,

    Jason.
  • Shiva

    #2
    Re: Generate a new guid

    Guid.NewGuid()

    "Jason L James" <jason@no-spam.dive-master.org> wrote in message
    news:412f2b38.2 0346136@news.de mon.co.uk...
    Hi all,

    is there a method or property of a class that will
    return a GUID in VB.NET that I can use as
    the PK of my DB. I do not want SQL to generate
    it as I can not then use

    SELECT @@IDENTITY to retrieve it.

    I want to try and mimic the functionality of

    newID()

    that SQL server has.

    Thanks,

    Jason.


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Generate a new guid

      * jason@no-spam.dive-master.org (Jason L James) scripsit:[color=blue]
      > is there a method or property of a class that will
      > return a GUID in VB.NET that I can use as
      > the PK of my DB. I do not want SQL to generate
      > it as I can not then use
      >
      > SELECT @@IDENTITY to retrieve it.
      >
      > I want to try and mimic the functionality of
      >
      > newID()[/color]

      Take a look at the 'SqlGuid' class and the 'Guid.NewGuid' method.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • CJ Taylor

        #4
        Re: Generate a new guid

        I ran into this exact same problem in which case the GUID was generated
        after the procedure had been called.

        select @@IDENTITY will not return the guid because you cannot have a sqlguid
        (uniquieidentif ier in T-SQL) as an identifier row (at least as far as SQL is
        concerned).

        I wrote my stored procedures to have both an identity column and a row guid
        problem. the reason I use row guids is mainly for users ( a little social
        eng if you will), I don't like sequences..

        however, I do use them.. i.e, I have a stored procedure that does

        Insert into MyTable (name1, name2, name3) values (value1, value2, value3)

        then

        SELECT gRowID FROM myTable Where iID = SCOPE_IDENTITY( )

        tends to work a little better and I get my GUID.

        where iID is an identity type (of decimal, big number, but gives you a lot
        fo flexibility) and I hide it from the user. its an extra T-SQL step, but I
        have yet to find a better way to do it other than providing my own GUID.

        HTH,
        CJ

        "Jason L James" <jason@no-spam.dive-master.org> wrote in message
        news:412f2b38.2 0346136@news.de mon.co.uk...[color=blue]
        > Hi all,
        >
        > is there a method or property of a class that will
        > return a GUID in VB.NET that I can use as
        > the PK of my DB. I do not want SQL to generate
        > it as I can not then use
        >
        > SELECT @@IDENTITY to retrieve it.
        >
        > I want to try and mimic the functionality of
        >
        > newID()
        >
        > that SQL server has.
        >
        > Thanks,
        >
        > Jason.[/color]


        Comment

        • Jason L James

          #5
          Re: Generate a new guid

          CJ,

          can I do this using SQL CE? I don't think I can!

          Any ideas?

          Thanks,

          Jason.

          P.S. At the moment I am considering returning to int IDs and make
          things work using the replication options of SQL 2K.

          On Fri, 27 Aug 2004 09:38:51 -0500, "CJ Taylor" <[cege] at [tavayn]
          dit commmmm> wrote:
          [color=blue]
          >I ran into this exact same problem in which case the GUID was generated
          >after the procedure had been called.
          >
          >select @@IDENTITY will not return the guid because you cannot have a sqlguid
          >(uniquieidenti fier in T-SQL) as an identifier row (at least as far as SQL is
          >concerned).
          >
          >I wrote my stored procedures to have both an identity column and a row guid
          >problem. the reason I use row guids is mainly for users ( a little social
          >eng if you will), I don't like sequences..
          >
          >however, I do use them.. i.e, I have a stored procedure that does
          >
          >Insert into MyTable (name1, name2, name3) values (value1, value2, value3)
          >
          >then
          >
          >SELECT gRowID FROM myTable Where iID = SCOPE_IDENTITY( )
          >
          >tends to work a little better and I get my GUID.
          >
          >where iID is an identity type (of decimal, big number, but gives you a lot
          >fo flexibility) and I hide it from the user. its an extra T-SQL step, but I
          >have yet to find a better way to do it other than providing my own GUID.
          >
          >HTH,
          >CJ
          >
          >"Jason L James" <jason@no-spam.dive-master.org> wrote in message
          >news:412f2b38. 20346136@news.d emon.co.uk...[color=green]
          >> Hi all,
          >>
          >> is there a method or property of a class that will
          >> return a GUID in VB.NET that I can use as
          >> the PK of my DB. I do not want SQL to generate
          >> it as I can not then use
          >>
          >> SELECT @@IDENTITY to retrieve it.
          >>
          >> I want to try and mimic the functionality of
          >>
          >> newID()
          >>
          >> that SQL server has.
          >>
          >> Thanks,
          >>
          >> Jason.[/color]
          >
          >[/color]

          Comment

          • CJ Taylor

            #6
            Re: Generate a new guid

            Unfortunatly I don't have any experience with SQL CE. Given the way
            Microsoft does things, I would be tempted to say yes, however, its a
            platform I've only toyed with, and very mildely at that.

            does SQL CE support Stored Procedures?

            It may be easier to go back to numeric for you, thats up to you. Like I
            said, I did it to trick my users so they didn't try to get crafty and break
            something. Then again, I'm a really paranoid guy.

            HTH,
            CJ

            "Jason L James" <jason@no-spam.dive-master.org> wrote in message
            news:412f5a30.3 2369935@news.de mon.co.uk...[color=blue]
            > CJ,
            >
            > can I do this using SQL CE? I don't think I can!
            >
            > Any ideas?
            >
            > Thanks,
            >
            > Jason.
            >
            > P.S. At the moment I am considering returning to int IDs and make
            > things work using the replication options of SQL 2K.
            >
            > On Fri, 27 Aug 2004 09:38:51 -0500, "CJ Taylor" <[cege] at [tavayn]
            > dit commmmm> wrote:
            >[color=green]
            > >I ran into this exact same problem in which case the GUID was generated
            > >after the procedure had been called.
            > >
            > >select @@IDENTITY will not return the guid because you cannot have a[/color][/color]
            sqlguid[color=blue][color=green]
            > >(uniquieidenti fier in T-SQL) as an identifier row (at least as far as SQL[/color][/color]
            is[color=blue][color=green]
            > >concerned).
            > >
            > >I wrote my stored procedures to have both an identity column and a row[/color][/color]
            guid[color=blue][color=green]
            > >problem. the reason I use row guids is mainly for users ( a little[/color][/color]
            social[color=blue][color=green]
            > >eng if you will), I don't like sequences..
            > >
            > >however, I do use them.. i.e, I have a stored procedure that does
            > >
            > >Insert into MyTable (name1, name2, name3) values (value1, value2, value3)
            > >
            > >then
            > >
            > >SELECT gRowID FROM myTable Where iID = SCOPE_IDENTITY( )
            > >
            > >tends to work a little better and I get my GUID.
            > >
            > >where iID is an identity type (of decimal, big number, but gives you a[/color][/color]
            lot[color=blue][color=green]
            > >fo flexibility) and I hide it from the user. its an extra T-SQL step,[/color][/color]
            but I[color=blue][color=green]
            > >have yet to find a better way to do it other than providing my own GUID.
            > >
            > >HTH,
            > >CJ
            > >
            > >"Jason L James" <jason@no-spam.dive-master.org> wrote in message
            > >news:412f2b38. 20346136@news.d emon.co.uk...[color=darkred]
            > >> Hi all,
            > >>
            > >> is there a method or property of a class that will
            > >> return a GUID in VB.NET that I can use as
            > >> the PK of my DB. I do not want SQL to generate
            > >> it as I can not then use
            > >>
            > >> SELECT @@IDENTITY to retrieve it.
            > >>
            > >> I want to try and mimic the functionality of
            > >>
            > >> newID()
            > >>
            > >> that SQL server has.
            > >>
            > >> Thanks,
            > >>
            > >> Jason.[/color]
            > >
            > >[/color]
            >[/color]


            Comment

            Working...