我想從用戶輸入的日期自動生成Id。 實施例:日期是2017年12月5日比ID必須170001或2014年5月10日比ID必須140001如何在asp.net中自動生成id號c#
private void autogeneratedocno()
{
con.Open();
string check="select count(Doc_no) from
complaint";
SqlCommand cmd2 = new SqlCommand(check,con);
int a =Convert.ToInt32(cmd2.ExecuteScalar());
string date = txtdate.Text.ToString();
string year = date.Substring(8, 2);
//string[] arrDate = date.Split('/');
//string day = arrDate[0].ToString();
//string month = arrDate[1].ToString();
//string year = arrDate[2].ToString();
//string y = year.Substring(2, 2);
if (a == 0)
{
txtdocno.Text = string.Concat(year.ToString()
+ "00000");
}
else
{
string count = "select ISNULL(MAX(Doc_no),0)+1
from complaint";
SqlCommand cmd = new SqlCommand(count, con);
int doc_no =
Convert.ToInt32(cmd.ExecuteScalar());
txtdocno.Text = doc_no.ToString();
}
con.Close();
}
通過使用上述代碼。我能夠生成2017年的ID,但是當我改變年頭兩個數字不會改變。
請給我建議改變爲自動生成ID
歡迎SO,這個問題太廣,請花一分鐘閱讀[如何問一個問題(https://stackoverflow.com/help/how-to-ask)也許[寫作完美問題,](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/),並嘗試寫一個更好的問題,詳細說明更多要求,告訴我們你已經嘗試了什麼,以及你在哪裏卡住了。祝你好運! – ThrowingSpoon
你可以請添加一些更多的細節。爲什麼必須12/05/2017 = 170001,如果兩個人輸入相同的日期,Id應該一樣嗎? – Scrobi
不,id始終自動遞增。 –