如何获得唯一的ID自动

编程入门 行业动态 更新时间:2024-10-14 02:20:35
本文介绍了如何获得唯一的ID自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨! 我有一个来自其中有两个文本框的 texboxid texboxname 我想在每次输入新条目时在textboxid中获得自动编号 使用sql server 2008

解决方案

不要在表单中输入ID.通过在sql2008的列属性中将ID的字段设置为整数列和ID为1的字段,在表中自动生成它.桌子

private int NewId(){ SqlConnection con = new SqlConnection(//your ConnectionStrings); SqlDataAdapter adapter = new SqlDataAdapter("Select * from yourtablename", con); SqlCommandBuilder cmd = new SqlCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds); int ID = 0; if (ds.Tables[0].Rows.Count > 0) { int len = ds.Tables[0].Rows.Count - 1; ID = int.Parse(ds.Tables["Arcticle"].Rows[len]["arcId"].ToString()) + 1; } else ID = 1; return ID; }

您可以在需要新ID的地方调用上述函数.

从表中选择最后一个条目,并在e上增加计数,并将其设置为id.自动在id字段中增加 问候, shefeek

hi! i have a from in which i have two text boxes texboxid texboxname i want to get auto number in textboxid whenever i want to enter new entry using sql server 2008 how it will be done

解决方案

Don''t have an ID entered in the form. Generate it automatically in the table by having the field for ID set up as integer column and as Identity with increment 1 in Column properties in sql2008

you try this code which retrieve the last row form your table

private int NewId(){ SqlConnection con = new SqlConnection(//your ConnectionStrings); SqlDataAdapter adapter = new SqlDataAdapter("Select * from yourtablename", con); SqlCommandBuilder cmd = new SqlCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds); int ID = 0; if (ds.Tables[0].Rows.Count > 0) { int len = ds.Tables[0].Rows.Count - 1; ID = int.Parse(ds.Tables["Arcticle"].Rows[len]["arcId"].ToString()) + 1; } else ID = 1; return ID; }

you call above function where you need to new id.

hi, pick the last entry from the table and incriment the the count by on eand set it as id.auto incrimen tthe id field regards, shefeek

更多推荐

如何获得唯一的ID自动

本文发布于:2023-11-28 15:10:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!