自动生成列的SQL命令 [英] Sql command for autogenerate columns

查看:72
本文介绍了自动生成列的SQL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要数据要插入列ID,该如何自动生成列ID?
前任. ID,名称,地址等
我要输入名称和地址,但ID应该自动生成.如何在asp.net/c#

How generate column id automaticaly whnever data goes to insert into it?
Ex. id,name,address etc
I am going to put name and address but id should be generate auto How in asp.net /c#

推荐答案



如果尚未添加ID列,请首先将其添加到数据库中.通过单击键图标将其设置为主键.然后在底部的属性"部分中找到标识并将其设置为是".种子是您想要的起始编号.例如,如果您希望您的第一个ID为1000,则将其放入种子中.默认值是1.同样,在您希望ID随每个条目增加的数字中增加一个数字.再次默认为1.因此,这会使您的ID从1000,1001,1002等增加.

将数据保存到表时,将自动创建ID,并且您无需自己进行任何计算.

如果需要在屏幕上显示ID,则需要创建一个存储过程来进行更新并返回@@identity,这将是最后创建的记录的ID值.

Hi,

First add in ID column to your database if you have not already done so. Make this the primary key by clicking the key icon. Then in the properties section at the bottom find identity and set it to yes. The seed is the starting number you would like. for example if you want your first id to be 1000 put that in the seed. the default is 1. Also in the increment put in the number you want your id to increase by with every entry. Default is 1 again. so this will increase your id from say 1000,1001,1002 etc.

When you save data to the table the id is created automatically and you do not need to do anything to calculate this yourself.

If you require to show the id on screen you will need to create a stored procedure to do the update and return @@identity which will be the id value of the last created record.

Create Procedure InsertUser
    @Name varchar(50),
    @address varchar (50)
as
begin

insert into MyUserTable(name, address)
values(@name,@address)

Return @@identity
End


如果使用SQL Server,则与ASP.NET/C#无关;)

请查看以下主题及相关主题:
http://www.w3schools.com/sql/sql_autoincrement.asp [ http://msdn.microsoft.com/en-us/library/ms186775.aspx [ ^ ]
http://social.msdn.microsoft.com/论坛/en-AU/csharpgeneral/thread/e6946a5f-8b29-4932-bbf8-577eac374ca2 [
If you use SQL server, ASP.NET/C# has nothing to do with it ;)

Please, see these and related topics:
http://www.w3schools.com/sql/sql_autoincrement.asp[^]
http://msdn.microsoft.com/en-us/library/ms186775.aspx[^]
http://social.msdn.microsoft.com/Forums/en-AU/csharpgeneral/thread/e6946a5f-8b29-4932-bbf8-577eac374ca2[^]


嘿,请使用此代码.单击textbox3和textbox4中的按钮时,这将生成ID和密码.

我用了这段代码.也许这会对您有所帮助.
Hey, use this code. This will generate id and password when you click the button in textbox3 and textbox4

I used this code. Maybe this will help you.
protected void Button1_Click(object sender, EventArgs e)//BUTTON TOGENERATE ID AS ROPDM0,ROPDM1..... AND PASSWORD AS NAME+RANDOM NUMBER
{
   if (TextBox1.Text == "")//YOUR TEXT BOX IN WHICH WE ENTER NAME
   {
      cid = "";
   }
   else
   {
      con.Open();
      SqlCommand cmd = new SqlCommand("select COUNT(CUSTOMER_NAME) from customer", con);//I M USING A CUSTOMER TABLE TO GENERATE ID AND PASSWORD
      int r = Convert.ToInt32(cmd.ExecuteScalar());
      if (r > 0)
      {
         string n = TextBox1.Text;
         string pas = GenerateNumber();
         TextBox4.Text = n + pas;//FOR PASSWORD PLEAES USE YOUR TEXTBOX IN WHICH YOU WANT TO GENERATE PASSWORD
               
         SqlDataAdapter da = new SqlDataAdapter("select distinct(CUSTOMER_ID) from customer", con);
         DataTable dt = new DataTable();
         da.Fill(dt);
         var query = from row in dt.AsEnumerable() select row["CUSTOMER_ID"].ToString();
         string[] ides = query.ToArray();
         int rt = 0;
         int m = 0;
         foreach (string s in ides)
         {
            int l2 = s.Length;
            if (l2 > m){ m = l2; }
            foreach (string s in ides)
            {
               int l3 = s.Length;
               if (l3 == m)
               {
                  string hc = "";
                  for (int i = 5; i < l3; i++){ hc = hc + s[i]; }
                  int hh = Convert.ToInt32(hc);
                  rt = hh + 1;
               }
            }
            TextBox3.Text = "ROPDM" + rt.ToString();//THIS WILL SHOW YOUR ID            
         }
         else
         {
            cid = "ROPDM" + "0";//WHEN THERE IS NO RECORD IN TABLE
            TextBox3.Text = cid;//ID
            string pas = GenerateNumber();
            string n = TextBox1.Text;
            TextBox4.Text = n + pas;//PASSWORD            
         }
         con.Close();
      }
  }


这篇关于自动生成列的SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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