使用C#在文本框中自动生成数字 [英] Auto generating numbers in text box using c#

查看:223
本文介绍了使用C#在文本框中自动生成数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQL 2005作为后端的Windows应用程序.
我想自动生成数字并将其显示在文本框中
我想按顺序生成数字并将其保存在数据库中.

在此先感谢!!!

I am working on a windows application with SQL 2005 as backend.
I want to auto generate numbers and show it in textbox
I want to generate the numbers in sequence and save it in DB.

Thanks in Advance !!!

推荐答案

如果要将值保存到主列中,则只需要在SQL Server中进行设置即可.

1.右键单击表名称.
2.选择列名称
3.进入属性窗口
4.双击身份说明.
5.保存表格
现在,当您运行任何插入查询时,您无需提供此列值,它将自动增加.

如果要从C#应用程序中获得自定义序列号,则需要执行select查询以使用scope_Identity()获取最后插入的记录ID,并需要增加该值.

请参阅以下链接.

http://msdn.microsoft.com/en-us/library/ms190315.aspx [ ^ ]
If you want to save the value into primary column then you just need to do the setting into SQL server.

1. Right click on table name.
2. Select the column name
3. Go into property window
4. Double click on identity specification.
5. Save the table
Now when you run any insert query you dont need to provide this column value it will automatically incremented.

If you want custom sequence number from C# application then you need to execute select query to fetch the last inserted record id with scope_Identity() and need to increment the value.

refer the below link.

http://msdn.microsoft.com/en-us/library/ms190315.aspx[^]


您可以尝试以下代码...它可以帮助您了解一些想法...

you can try this code... it may help you to get some idea...

private static Random rnd;
static void random()
{
    rnd = new Random();
}

private string GenerateId()
{

    string allowedChars = "ABCDEFGHJKLM0123456789NOPQRSTUVWXYZ";

    char[] chars = new char[6];

    for (int i = 0; i < 6; i++)
    {
        chars[i] = allowedChars[rnd.Next(0, allowedChars.Length)];
    }

    return new string(chars);

}


这篇关于使用C#在文本框中自动生成数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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