Home All Groups Group Topic Archive Search About

Adding rows to a dataset where the primary key is type System.Guid

Author
9 Jan 2007 5:06 PM
michael
So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A table
in the SQL Server db has a column, which is the primary key, of type
uniqueidentifier. The RowGuid attribute is "yes" and the default value is
newid().

In my application I have a dataset that has a table with the same schema
(actually I created a typed dataset which has the table). I wonder how to
approach adding new rows to the dataset and protect the integrity of this
primary key field:

1) Should I first insert a row to SQL Server and then read the row back into
the dataset to get the value of the field as set by the newid() of SQLServer
(is there an equivalent @@Identity?).

2) Is there an equivalent newid() in ADO.NET to create the guid within the
application and then insert the row into SQL Server?



--
Michael Hockstein

Author
9 Jan 2007 6:39 PM
William (Bill) Vaughn
Actually, there are a number of ways to do this. First, you can create your
own GUIDs with the Framework. NewID comes to mind.
Dim x As Guid
x = Guid.NewGuid

This can be done in the new row event or when building up the INSERT
statement. And yes, one approach I use it to create a block of rows in a SP
on the server and simply do UPDATES on the rows...

hth


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"michael" <howlinghound@nospam.nospam> wrote in message
news:772FC597-BB47-4B16-AE5B-D415E3251CAD@microsoft.com...
> So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A
> table
> in the SQL Server db has a column, which is the primary key, of type
> uniqueidentifier. The RowGuid attribute is "yes" and the default value is
> newid().
>
> In my application I have a dataset that has a table with the same schema
> (actually I created a typed dataset which has the table). I wonder how to
> approach adding new rows to the dataset and protect the integrity of this
> primary key field:
>
> 1) Should I first insert a row to SQL Server and then read the row back
> into
> the dataset to get the value of the field as set by the newid() of
> SQLServer
> (is there an equivalent @@Identity?).
>
> 2) Is there an equivalent newid() in ADO.NET to create the guid within the
> application and then insert the row into SQL Server?
>
>
>
> --
> Michael Hockstein
Author
10 Jan 2007 1:56 AM
michael
Thanks Bill

I used the Guid.NewGuid. It worked great.

Curious though, how is this number generated and what makes it virtually
unique? It doesn't seem to rely on any of the data in the record, so is it
based on date/time/Karma?


--
Michael Hockstein


Show quote
"William (Bill) Vaughn" wrote:

> Actually, there are a number of ways to do this. First, you can create your
> own GUIDs with the Framework. NewID comes to mind.
> Dim x As Guid
> x = Guid.NewGuid
>
> This can be done in the new row event or when building up the INSERT
> statement. And yes, one approach I use it to create a block of rows in a SP
> on the server and simply do UPDATES on the rows...
>
> hth
>
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> INETA Speaker
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no rights.
> __________________________________
> Visit www.hitchhikerguides.net to get more information on my latest book:
> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> -----------------------------------------------------------------------------------------------------------------------
>
> "michael" <howlinghound@nospam.nospam> wrote in message
> news:772FC597-BB47-4B16-AE5B-D415E3251CAD@microsoft.com...
> > So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A
> > table
> > in the SQL Server db has a column, which is the primary key, of type
> > uniqueidentifier. The RowGuid attribute is "yes" and the default value is
> > newid().
> >
> > In my application I have a dataset that has a table with the same schema
> > (actually I created a typed dataset which has the table). I wonder how to
> > approach adding new rows to the dataset and protect the integrity of this
> > primary key field:
> >
> > 1) Should I first insert a row to SQL Server and then read the row back
> > into
> > the dataset to get the value of the field as set by the newid() of
> > SQLServer
> > (is there an equivalent @@Identity?).
> >
> > 2) Is there an equivalent newid() in ADO.NET to create the guid within the
> > application and then insert the row into SQL Server?
> >
> >
> >
> > --
> > Michael Hockstein
>
>
>
Author
10 Jan 2007 3:43 AM
William (Bill) Vaughn
It calls a routine that extracts it from a pile of numbers kept in a big
vault in the Antarctic--that's why it takes so long to generate. Dunno. They
keep changing the algorithm to make it unique. At one time they used your
NIC's MAC address but I don't think they do anymore. It's probably unique
enough for most uses. It's a lot better than an SSAN...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------

Show quote
"michael" <howlinghound@nospam.nospam> wrote in message
news:CC45D996-94A0-4F61-8BE6-8CAD40F482CF@microsoft.com...
> Thanks Bill
>
> I used the Guid.NewGuid. It worked great.
>
> Curious though, how is this number generated and what makes it virtually
> unique? It doesn't seem to rely on any of the data in the record, so is it
> based on date/time/Karma?
>
>
> --
> Michael Hockstein
>
>
> "William (Bill) Vaughn" wrote:
>
>> Actually, there are a number of ways to do this. First, you can create
>> your
>> own GUIDs with the Framework. NewID comes to mind.
>> Dim x As Guid
>> x = Guid.NewGuid
>>
>> This can be done in the new row event or when building up the INSERT
>> statement. And yes, one approach I use it to create a block of rows in a
>> SP
>> on the server and simply do UPDATES on the rows...
>>
>> hth
>>
>>
>> --
>> ____________________________________
>> William (Bill) Vaughn
>> Author, Mentor, Consultant
>> Microsoft MVP
>> INETA Speaker
>> www.betav.com/blog/billva
>> www.betav.com
>> Please reply only to the newsgroup so that others can benefit.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> __________________________________
>> Visit www.hitchhikerguides.net to get more information on my latest book:
>> Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
>> and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
>> -----------------------------------------------------------------------------------------------------------------------
>>
>> "michael" <howlinghound@nospam.nospam> wrote in message
>> news:772FC597-BB47-4B16-AE5B-D415E3251CAD@microsoft.com...
>> > So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A
>> > table
>> > in the SQL Server db has a column, which is the primary key, of type
>> > uniqueidentifier. The RowGuid attribute is "yes" and the default value
>> > is
>> > newid().
>> >
>> > In my application I have a dataset that has a table with the same
>> > schema
>> > (actually I created a typed dataset which has the table). I wonder how
>> > to
>> > approach adding new rows to the dataset and protect the integrity of
>> > this
>> > primary key field:
>> >
>> > 1) Should I first insert a row to SQL Server and then read the row back
>> > into
>> > the dataset to get the value of the field as set by the newid() of
>> > SQLServer
>> > (is there an equivalent @@Identity?).
>> >
>> > 2) Is there an equivalent newid() in ADO.NET to create the guid within
>> > the
>> > application and then insert the row into SQL Server?
>> >
>> >
>> >
>> > --
>> > Michael Hockstein
>>
>>
>>
Author
10 Jan 2007 4:00 AM
dsandor
Michael,

Guid.Newid() calls the native CoCreateGuid() method which in turn uses
UuidCreate() method to create the guid.  The number is based on time and some
properties of your computer mainly the MAC address of your primary network
card.

http://msdn2.microsoft.com/en-us/library/ms688568.aspx

Whenever I am curious about how a particular .net class works I always check
out the Rotor Shared Source CLI code.  You can download it here and see
pretty much how the System namespace works.
http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en
-David Sandor

Show quote
"michael" wrote:

> Thanks Bill
>
> I used the Guid.NewGuid. It worked great.
>
> Curious though, how is this number generated and what makes it virtually
> unique? It doesn't seem to rely on any of the data in the record, so is it
> based on date/time/Karma?
>
>
> --
> Michael Hockstein
>
>
> "William (Bill) Vaughn" wrote:
>
> > Actually, there are a number of ways to do this. First, you can create your
> > own GUIDs with the Framework. NewID comes to mind.
> > Dim x As Guid
> > x = Guid.NewGuid
> >
> > This can be done in the new row event or when building up the INSERT
> > statement. And yes, one approach I use it to create a block of rows in a SP
> > on the server and simply do UPDATES on the rows...
> >
> > hth
> >
> >
> > --
> > ____________________________________
> > William (Bill) Vaughn
> > Author, Mentor, Consultant
> > Microsoft MVP
> > INETA Speaker
> > www.betav.com/blog/billva
> > www.betav.com
> > Please reply only to the newsgroup so that others can benefit.
> > This posting is provided "AS IS" with no warranties, and confers no rights.
> > __________________________________
> > Visit www.hitchhikerguides.net to get more information on my latest book:
> > Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> > and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> > -----------------------------------------------------------------------------------------------------------------------
> >
> > "michael" <howlinghound@nospam.nospam> wrote in message
> > news:772FC597-BB47-4B16-AE5B-D415E3251CAD@microsoft.com...
> > > So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A
> > > table
> > > in the SQL Server db has a column, which is the primary key, of type
> > > uniqueidentifier. The RowGuid attribute is "yes" and the default value is
> > > newid().
> > >
> > > In my application I have a dataset that has a table with the same schema
> > > (actually I created a typed dataset which has the table). I wonder how to
> > > approach adding new rows to the dataset and protect the integrity of this
> > > primary key field:
> > >
> > > 1) Should I first insert a row to SQL Server and then read the row back
> > > into
> > > the dataset to get the value of the field as set by the newid() of
> > > SQLServer
> > > (is there an equivalent @@Identity?).
> > >
> > > 2) Is there an equivalent newid() in ADO.NET to create the guid within the
> > > application and then insert the row into SQL Server?
> > >
> > >
> > >
> > > --
> > > Michael Hockstein
> >
> >
> >
Author
10 Jan 2007 5:22 AM
michael
I don't know Dave. That Antarctic explanation of Bill's makes a lot of sense.

I will check out the resources you listed. Thanks a bunch.

--
Michael Hockstein


Show quote
"dsandor" wrote:

> Michael,
>
> Guid.Newid() calls the native CoCreateGuid() method which in turn uses
> UuidCreate() method to create the guid.  The number is based on time and some
> properties of your computer mainly the MAC address of your primary network
> card.
>
> http://msdn2.microsoft.com/en-us/library/ms688568.aspx
>
> Whenever I am curious about how a particular .net class works I always check
> out the Rotor Shared Source CLI code.  You can download it here and see
> pretty much how the System namespace works.
>
> http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-AE17-3121B4F51D4D&displaylang=en
> -David Sandor
>
> "michael" wrote:
>
> > Thanks Bill
> >
> > I used the Guid.NewGuid. It worked great.
> >
> > Curious though, how is this number generated and what makes it virtually
> > unique? It doesn't seem to rely on any of the data in the record, so is it
> > based on date/time/Karma?
> >
> >
> > --
> > Michael Hockstein
> >
> >
> > "William (Bill) Vaughn" wrote:
> >
> > > Actually, there are a number of ways to do this. First, you can create your
> > > own GUIDs with the Framework. NewID comes to mind.
> > > Dim x As Guid
> > > x = Guid.NewGuid
> > >
> > > This can be done in the new row event or when building up the INSERT
> > > statement. And yes, one approach I use it to create a block of rows in a SP
> > > on the server and simply do UPDATES on the rows...
> > >
> > > hth
> > >
> > >
> > > --
> > > ____________________________________
> > > William (Bill) Vaughn
> > > Author, Mentor, Consultant
> > > Microsoft MVP
> > > INETA Speaker
> > > www.betav.com/blog/billva
> > > www.betav.com
> > > Please reply only to the newsgroup so that others can benefit.
> > > This posting is provided "AS IS" with no warranties, and confers no rights.
> > > __________________________________
> > > Visit www.hitchhikerguides.net to get more information on my latest book:
> > > Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
> > > and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
> > > -----------------------------------------------------------------------------------------------------------------------
> > >
> > > "michael" <howlinghound@nospam.nospam> wrote in message
> > > news:772FC597-BB47-4B16-AE5B-D415E3251CAD@microsoft.com...
> > > > So, I'm working with VS 2005 (VB) and SQL Server(both 2000 and 2005). A
> > > > table
> > > > in the SQL Server db has a column, which is the primary key, of type
> > > > uniqueidentifier. The RowGuid attribute is "yes" and the default value is
> > > > newid().
> > > >
> > > > In my application I have a dataset that has a table with the same schema
> > > > (actually I created a typed dataset which has the table). I wonder how to
> > > > approach adding new rows to the dataset and protect the integrity of this
> > > > primary key field:
> > > >
> > > > 1) Should I first insert a row to SQL Server and then read the row back
> > > > into
> > > > the dataset to get the value of the field as set by the newid() of
> > > > SQLServer
> > > > (is there an equivalent @@Identity?).
> > > >
> > > > 2) Is there an equivalent newid() in ADO.NET to create the guid within the
> > > > application and then insert the row into SQL Server?
> > > >
> > > >
> > > >
> > > > --
> > > > Michael Hockstein
> > >
> > >
> > >
Author
12 Jan 2007 8:48 AM
WenYuan Wang
Hi Michael,
I agree with Bill that you can use "Guid.NewGuid" method to generate a
unique GUID.
This method is an equivalent newid() in ADO.net.

I just want to check if there is anything we can help with, please feel
free to reply me and we will follow up.
I'm glad to work with you.

Have a great day!
Wen Yuan
Microsoft Online Support
Microsoft Global Technical Support Center
Author
12 Jan 2007 11:38 AM
michael
Everything works fine. Thanks!

--
Michael Hockstein


Show quote
"WenYuan Wang" wrote:

> Hi Michael,
> I agree with Bill that you can use "Guid.NewGuid" method to generate a
> unique GUID.
> This method is an equivalent newid() in ADO.net.
>
> I just want to check if there is anything we can help with, please feel
> free to reply me and we will follow up.
> I'm glad to work with you.
>
> Have a great day!
> Wen Yuan
> Microsoft Online Support
> Microsoft Global Technical Support Center
>
>

AddThis Social Bookmark Button