我有一个字母数字 ID。我如何自动增加(+1)数字?
我应该将调查 ID 设置为 SVY0001 到 SVY9999。
每次添加新调查时,如何将数字增加 1?
目前,我的调查 id 文本框中有一个正则表达式验证器,SVY 是强制性的。
ValidationExpression="^SVY([0-9]{4})$"
请给我提意见。
这是我的 .cs 代码。
protected void btnCreateSurvey_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
SqlCommand command = null;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["SurveyFdDBConnString"].ConnectionString;
connection = new SqlConnection(connectionString);
connection.Open();
string sql = "Insert into Survey (SurveyID, SurveyName, CreatedBy, DateCreated, Anonymous) Values " + "(@SurveyID, @SurveyName, @CreatedBy, @DateCreated, @Anonymous)";
command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@SurveyID", txtBoxSurveyID.Text);
command.Parameters.AddWithValue("@SurveyName", txtBoxSurveyName.Text);
command.Parameters.AddWithValue("@CreatedBy", (string)Session["UserID"]);
command.Parameters.AddWithValue("@DateCreated", DateTime.Now);
command.Parameters.AddWithValue("@Anonymous", txtBoxAnonymous.Text);
int rowCount = command.ExecuteNonQuery();
if (rowCount != 0)
{
Session["SurveyID"] = txtBoxSurveyID.Text;
Response.Write("Survey created successfully.<br/>");
Response.Redirect("~/Admin/Select.aspx");
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
finally
{
connection.Close();
txtBoxSurveyID.Text = string.Empty;
txtBoxSurveyName.Text = string.Empty;
txtBoxAnonymous.Text = string.Empty;
}
}