473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Default value of a GUID field?!

I have a column named "ID" and its type is "uniqueidentifier" in SQL Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?

System.Guid.NewGuid()

Oct 26 '06 #1
9 5925
Hi Leon

You might want to have a look at this article:
http://www.informit.com/articles/art...p?p=25862&rl=1

Sam

Oct 26 '06 #2
You can seed these values, as a GUID is a GUID (not quite true, but as long
as you are not using a CLUSTERED key on the GUID (and sequential guids (SQL
SERver 2005)). BUt, if you do not want to, alter the INSERT query to not
include the key. This can get tricky if you are running related data and
need to redisplay, so think the full set of use cases through before making
a decision or you will be altering.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>I have a column named "ID" and its type is "uniqueidentifier" in SQL Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?

System.Guid.NewGuid()

Oct 26 '06 #3

"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>I have a column named "ID" and its type is "uniqueidentifier" in SQL Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?
You'd best let SQL generate the GUID, that's the only way you can be 100%
sure of no collisions.
System.Guid.NewGuid()

Oct 26 '06 #4
I would argue that the odds of generating the same GUID on 2 different
clients is so small, that it shouldn't be a consideration. Realistically,
it's just not going to happen, although yes, it's not 100% guaranteed.

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>
"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>>I have a column named "ID" and its type is "uniqueidentifier" in SQL
Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?

You'd best let SQL generate the GUID, that's the only way you can be 100%
sure of no collisions.
>System.Guid.NewGuid()


Oct 26 '06 #5

"Marina Levit [MVP]" <so*****@nospam.comwrote in message
news:um****************@TK2MSFTNGP03.phx.gbl...
>I would argue that the odds of generating the same GUID on 2 different
clients is so small, that it shouldn't be a consideration. Realistically,
it's just not going to happen, although yes, it's not 100% guaranteed.
The chance of any two GUIDs being equal are practically zero.

The chance of the one new GUID colliding in a database with millions (or
more) of records is still incredibly small.

The chance of any one of a million new GUIDs colliding with millions of
existing records, is getting to be enough to worry about, to the point where
I suspect SQL specifically checks for collision and generates a new GUID.
http://en.wikipedia.org/wiki/Birthday_paradox

True, GUIDs are 128-bits, and if you needed to add 2^32 records, you'd only
have 2^63 possible collisions, leaving you with a 2^-64 chance of collision.
But some of the bits in the GUID aren't random but tied to the specific
hardware (usually MAC address) in order to guarantee that different
workstations don't collide. So your chance of collision is now on the order
of 2^-20, or one-in-a-million. Obviously the number of records is very
important.
>
"Ben Voigt" <rb*@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>>
"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>>>I have a column named "ID" and its type is "uniqueidentifier" in SQL
Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the
DataSet)?

You'd best let SQL generate the GUID, that's the only way you can be 100%
sure of no collisions.
>>System.Guid.NewGuid()



Oct 26 '06 #6
Hello Ben
True, GUIDs are 128-bits, and if you needed to add 2^32 records, you'd
only have 2^63 possible collisions, leaving you with a 2^-64 chance of
collision. But some of the bits in the GUID aren't random but tied to the
specific hardware (usually MAC address) in order to guarantee that
different workstations don't collide. So your chance of collision is now
on the order of 2^-20, or one-in-a-million. Obviously the number of
records is very important.
Actually MS discontinued the use of MAC address from Win2000 and now uses
the Crypt random number generator to produce most of the Guid. This article
http://msdn2.microsoft.com/en-us/library/aa446557.aspx is about a Windows CE
guid generator but includes some interesting information about Guid
generation generally. In particular it looks like the chance of collision is
probably way smaller than you suggest.

Cheers
Doug Forster
Oct 26 '06 #7
Ben,
http://en.wikipedia.org/wiki/Birthday_paradox
Yes but how is that article as it is not about a birthday, but about the
same moment of birth in a millisecond. It is possible, but when will those
people meet each other.

Cor
Oct 27 '06 #8
chances of collsion may be a problem but simplicity of management of dataset
and code is more important to me I would rather prefer to use something like
newid() function in default value of the field in the typed dataset. because
requires less tricks and code than for example ommiting the Primary Key or
attaching a handler to the TableNewRow and DataBound controls are aware of
the difference between default values and user Entered values in firing
their event handlers or raising Data Error Exceptions and .....

"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>I have a column named "ID" and its type is "uniqueidentifier" in SQL Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?

System.Guid.NewGuid()

Oct 29 '06 #9
I have more than 5 DBs in Sql Server that each has more that 150 tables with
complex Relations that use GUID primary Keys and Foreign Keys very heavily
and I need to create data Entry Forms for them

so the smooth running of the created forms and ease of data Entry for users
while fast , robust designation of the forms are important so I heavily rely
on InBuilt bahvior of data bound controls rather than changing them or
creating user controls (besides the end users are more familiar with the
..Net Controls behaviors)

because of that some function that gengerates defualt values for the GUID
fields is the prefered way if possible so it will require less event handler
coding and coding for detection of false data Error exceptions or coding for
correcting the unwanted behaviors caused by these?!

besides where is the Data Form Generator that was in New Item window in
previous versions of VS .Net ?
"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:OA****************@TK2MSFTNGP03.phx.gbl...
chances of collsion may be a problem but simplicity of management of
dataset and code is more important to me I would rather prefer to use
something like newid() function in default value of the field in the typed
dataset. because requires less tricks and code than for example ommiting
the Primary Key or attaching a handler to the TableNewRow and DataBound
controls are aware of the difference between default values and user
Entered values in firing their event handlers or raising Data Error
Exceptions and .....

"Leon_Amirreza" <am**************@yahoo.comwrote in message
news:eA****************@TK2MSFTNGP03.phx.gbl...
>>I have a column named "ID" and its type is "uniqueidentifier" in SQL
Server
2005 Express.
this column is the primary key and its default value is "newid()" in SQL
Server.

I have generated a DataSet in C# that has this column but its default
value
is DBNull what can I do to make the default value be the return value of
this function ( or I want the Id be generated by default in the DataSet)?

System.Guid.NewGuid()


Oct 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom L | last post by:
How can I create a guid value in vb.net? I'm creating a sql procedure, and I want to pass the new guid value over to SQL instead of SQL creating it. thx
2
by: Jiho Han | last post by:
I generated a class from a schema. One of the fields are typed as System.Guid. Perfect. The only problem is when this class serializes, the guid field serializes as...
7
by: headware | last post by:
Is the GUID generated by the .NET System.Guid.NewGuid() function compatible with Access' Replication ID field type? Dave
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
29
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default...
3
by: Anders Borum | last post by:
Hello! When declaring variables without immediately assigning a value, the CLR (please correct here) assigns a default value according to the (reference) type of the variable (e.g. string =...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
8
by: Leon_Amirreza | last post by:
I have a column named "ID" and its type is "uniqueidentifier" in SQL Server 2005 Express. this column is the primary key and its default value is "newid()" in SQL Server. I have generated a...
7
by: =?Utf-8?B?Y291Z2FyaXN0aWM=?= | last post by:
I am trying to convert an C# application to VB and have one issue which is converting the generic value to the default. C# uses return default(T) as the return value how would I translate this in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.