如何获得唯一的ID自动 [英] how to get unique id auto

查看:91
本文介绍了如何获得唯一的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自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆