我需要在C#中自动生成id [英] I need auto generated id in C#

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

问题描述

这是我自动生成ID的代码我希望我的自动ID获取我的ID的日期时间和年份。它将在文本框中生成类似050-2018-0001,就像C#中的新格式一样。抱歉英文不好

  private   void  GenerateID()
{

con.Open();
cmd = new MySqlCommand( 选择最大值(StudID)+1来自student_registration,con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{

while (dr.Read())
{
txtStudID.Text = dr [ 0 ]。ToString();
if (txtStudID.Text ==
{
txtStudID.Text = 1 ;

}
}
}
else
{
txtStudID.Text = 1;
return ;
}
con.Close();
}





我的尝试:



i需要在C#中自动生成id

解决方案

你的问题不是很清楚,但也许你的意思是这样的:

 txtStudID.Text =050-+ DateTime.Now.Year + - + dr [0] .ToString()。PadLeft(4,'0'); 


你需要解析你 StudId 因为它是字符串



尝试这样



 private void GenerateID()
{

con.Open() ;
cmd = new MySqlCommand(从student_registration中选择Max(StudID),con);
dr = cmd.ExecuteReader();
string newId = string.Format(050- {0} -0001,DateTime.Now.Year);
if(dr.HasRows)
{
string prefix = string.Format(050- {0},DateTime.Now.Year);
while(dr.Read())
{

string maxId = dr [0] .ToString();
if(!string.IsNullOrWhiteSpace(maxId)&& maxId.StartsWith(prefix))
{
int count = Convert.ToInt32(maxId.Split(' - ')[2 ]);
newId = string.Format(050- {0} - {1:0000},DateTime.Now.Year,count + 1);
}
}
}
txtStudID.Text = newId;
con.Close();
}


在C#代码中使用自动生成ID的最佳方法如下。



公共班级员工

{

public Guid Id {get;私人集; } = Guid.NewGuid();

公共字符串EmployeeName {get;组; }

公共字符串地址{get;组; }

}



因此,无论何时创建Employee类的对象,它都会为ID属性分配一个唯一的GUID,因此无需执行任何操作其他

this is my code for auto generated id i want my auto id to get the datetime and year for my id. it will generate like 050-2018-0001 in the textbox like this format im new in C#. sorry for the bad english

private void GenerateID()
        {
           
            con.Open();
            cmd = new MySqlCommand("Select Max(StudID)+1 from student_registration", con);
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {

                while (dr.Read())
                {
                    txtStudID.Text = dr[0].ToString();
                    if (txtStudID.Text == "")
                    {
                        txtStudID.Text = "1";

                    }
                }
            }
            else
            {
                txtStudID.Text = "1";
                return;
            }
            con.Close();
        }



What I have tried:

i need auto generated id in C#

解决方案

Your question is not very clear, but maybe you mean something like this:

txtStudID.Text = "050-" + DateTime.Now.Year + "-" + dr[0].ToString().PadLeft(4, '0');


You need to parse you StudId because is it string

try like this

private void GenerateID()
{
	
	con.Open();
	cmd = new MySqlCommand("Select Max(StudID) from student_registration", con);
	dr = cmd.ExecuteReader();            
	string newId = string.Format("050-{0}-0001", DateTime.Now.Year);
	if (dr.HasRows)
	{
		string prefix = string.Format("050-{0}", DateTime.Now.Year);
		while (dr.Read())
		{
			
			string maxId= dr[0].ToString();
			if (!string.IsNullOrWhiteSpace(maxId) && maxId.StartsWith(prefix))                  
			{                        
				int count = Convert.ToInt32(maxId.Split('-')[2]);
				newId = string.Format("050-{0}-{1:0000}", DateTime.Now.Year, count+1);
			}                    
		}
	}
	txtStudID.Text = newId;
	con.Close();
}


The best of way of having Auto Generated Id in C# code is below.

public class Employee
{
public Guid Id { get; private set; } = Guid.NewGuid();
public string EmployeeName { get; set; }
public string Address { get; set; }
}

So whenever you create an object of Employee class it will assign an Unique GUID to ID property so no need to do anything else.


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

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