如何为我的ID设置一个guid值,该ID是字符串类型 [英] How to set a guid value to my ID which is of string type
问题描述
当我添加一条记录时,我已经定义了Id值。但是每次它都需要默认的guid值。只有00000000-0000-0000-0000-000000000000.Hence在db中它存储两个具有相同id.again的数据当我删除添加的记录时,所有记录都被删除为id = 00000000-0000-0000-0000-000000000000,你能告诉我如何避免这个问题,以便在添加时记录它将使用一些新的guid值存储数据..
我的代码如下
Hi,
While I am adding a record I have defined the Id value.But every time it takes the default guid value..means only 00000000-0000-0000-0000-000000000000.Hence in db it store two data with same id.again when i delete the added record all the record are deleted as id=00000000-0000-0000-0000-000000000000,Can you please tell me how to avoid this prolem so that while adding a record it will store the data with some new guid values..
my codes are as follows
//Add Buttoon for adding record
protected void btnAdd_Click(object sender, EventArgs e)
{
Guid id = new Guid();
string Id = id.ToString();
Member member = new Member();
member.ID = Id;
member.MemberID = int.Parse(txtMemberID.Text);
member.ReceiptNo = txtReceiptNo.Text;
member.SurName = txtSurName.Text;
member.Name = txtName.Text;
member.DateOfBirth = txtDateOFBirth.Text;
if (memberService.AddMember(member) == true)
LoadData();
//load data method as follows
public void LoadData()
{
List<Member> members = memberService.GetAllMembers();
gvMembers.DataSource = members;
gvMembers.DataBind();
推荐答案
System.Guid.NewGuid()
http://msdn.microsoft.com/en-us/library/system.guid.newguid(v = vs.110).aspx [ ^ ]
call
Guid id = Guid.NewGuid();
new Guid()使得一个空全0-guid(00000000-0000-0000-0000-000000000000不是很有用)。
Guid.NewGuid()使用一个独特的值制作一个实际的guid,你可能想要的。
new Guid() makes an "empty" all-0 guid (00000000-0000-0000-0000-000000000000 is not very useful).
Guid.NewGuid() makes an actual guid with a unique value, what you probably want.
http://stackoverflow.com/questions/11938151/guid-newguid-vs-new-guid [ ^ ]
查看链接在这里,如果你想把guid转换为字符串,为什么我的最后一个版权投票我不知道,我甚至几乎完成了这个
http://stackoverflow.com/questions/1700361/how-to-convert-a-guid -to-a-string-in-c [ ^ ]
see the link here, if you want to covert guid to string, why my last ans downvoted i dont know, i even practically done this
http://stackoverflow.com/questions/1700361/how-to-convert-a-guid-to-a-string-in-c[^]
这篇关于如何为我的ID设置一个guid值,该ID是字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!