C# 如何修复此语句以递增指定的变量?
我有一个问题,在我的代码中,在字段中不是自动递增的;我将在下面发布代码:C# 如何修复此语句以递增指定的变量?,c#,winforms,C#,Winforms,我有一个问题,在我的代码中,在字段中不是自动递增的;我将在下面发布代码: else { using (conn = new SqlConnection(Helper.CnnVal("ProfilingDB"))) { conn.Open(); string GetLastRow = "SELECT CONCAT(MAX(YEAR(applied_date)), " + "MAX(MONTH(applied_
else
{
using (conn = new SqlConnection(Helper.CnnVal("ProfilingDB")))
{
conn.Open();
string GetLastRow = "SELECT CONCAT(MAX(YEAR(applied_date)), " +
"MAX(MONTH(applied_date)) , MAX(DAY(applied_date))) as applied_date FROM personal_info"; //get last row of request date"
using (cmd = new SqlCommand(GetLastRow, conn))
{
dr = cmd.ExecuteReader();
while (dr.Read())
{
string AppliedNow = DTAppliedYear + DTAppliedMonth + DTAppliedToday;
DTApplied = (dr["applied_date"].ToString());
if (AppliedNow == DTApplied)
{ //compare the last row request date to datetime.now
string MaxAppliedNumber = "SELECT MAX(applicant_number) as applicant_number FROM personal_info"; // get max applied applicant
using (cmd = new SqlCommand(MaxAppliedNumber, conn))
{
while (dr.Read())
{
applicantNumber = Int64.Parse(dr["applicant_number"].ToString());
applicantNumber++;
}
}
}
else
{
string appliedNum = AppliedNow + "000"; //set ticket number and add string 0000
applicantNumber = Int64.Parse(appliedNum);
db.Insert_info(applicantNumber, txtbox_fname.Text, txtbox_lname.Text, txtbox_midname.Text, txtbox_address.Text, cb_gender.Text, dt_bday.Value.Date,
int.Parse(txtbox_age.Text), txtbox_email.Text, Int64.Parse(txtbox_contact.Text), applying_for, DateTime.Now);
}
}
}
}
“我希望申请人编号字段中的输出增加”在sql数据库中插入新列时,您可以尝试使用identity(1,1),从1增加到1。插入新行时,只需跳过id列
create table #table1
(
id int identity(1,1),
name varchar(10)
)
insert into #table1
values ('Test1'), ('Test2'), ('Test3'), ('Test4')
select * from #table1
如果已经创建了表,则可以在对象资源管理器中右键单击表名(如果使用SQL Server),单击“设计”,然后选择id列。
[1]:这是可能输出的链接,首先,如果您有其他信息,您应该编辑问题,而不是添加注释。第二,如果可能的话,最好是复制和粘贴文本,而不是使用图像。@juharr感谢您的编辑。顺便说一句,我本来打算编辑它的,但无论如何,谢谢。您一定要研究一下您的日期逻辑。你确定要把你所有的约会都弄混吗?如果最大日期为1月31日,最大月份为2月,会发生什么情况?2月31日你想做什么?是的。我想它没有问题,因为它从另一个字段获取数据。