2

私は英数字のIDを持っています。数値を自動インクリメント (+1) するにはどうすればよいですか?

調査IDはSVY0001~SVY9999に設定することになっています。

新しいアンケートを追加するたびに数字を 1 ずつ増やすにはどうすればよいですか?

現在、SVY が必須であるために、調査 ID テキスト ボックスに正規表現バリデーターがあります。

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;
    }
}
4

4 に答える 4

3

主キーには複合キーを使用できます。1 つの列には英数字コード、もう 1 つの列には数字が表示されます。整数列では、ID として設定でき、SQL がその仕事をしてくれます。

複合キーを設定します。1 つの整数と 1 つの varchar の 2 つの列を作成します。シフトダウンで列の1つを選択し、2番目を選択してから、メニューのキーのボタンを選択してそれらをキーとして設定します。

次に、プロパティ > ID 列に移動し、ドロップダウンから整数列を選択します。

メニューでキーを選択すると、両方の列にキーが表示されます。

ここに画像の説明を入力

ID の例として列を選択

ここに画像の説明を入力

于 2013-11-12T01:52:30.137 に答える
2

そのSVY部分が単なるプレフィックスの場合は、整数を保存し、表示のためにプレフィックスと整数を連結します。固定されている場合はキーに含める必要はありません

于 2013-11-12T01:54:02.757 に答える
1

1) sql から合計行数を取得します。

2) Inside btnCreateSurvey_Click、合計行数 + 1

3)Session["SurveyID"] = txtBoxSurveyID.Text + totalRowCount.ToString().PadLeft(4, '0'); //Assuming your textBoxSurvey ID is always "SVY"

于 2013-11-12T02:07:51.713 に答える
0

次のようなことができます。

class SurveyId
{
  static int counter = 0 ;
  static readonly object Syncroot = new object() ;

  public override string ToString()
  {
    return string.Format( "SVY{0:0000}" , this.Value )
  }

  public int Value { get ; private set ; }

  private SurveyId( int value )
  {
    this.Value = value ;
  }

  public static SurveyId GetNext()
  {
    lock ( Syncroot )
    {
      ++counter ;
    }
    return new SurveyId(counter) ;
  }

}

ただし、アプリ ドメインがリサイクルされるたびに調査 ID が SVY0000 から再開されるという問題があります。

データベースでこれを行うことができ、データベースが調査 ID を管理できるようにします。

create table dbo.survey
(
  id        int  not null identity(1,1) ,
  survey_id as 'SVY' + right('0000'+convert(varchar,id),4) ,
  name      varchar(255) not null ,
  -- other
  -- attributes
  -- here

  constraint Survey_PK primary key clustered ( id ) ,
  constraint Survey_AK01 unique nonclustered ( survey_id ) ,

)

挿入ステートメントは次のようになります

insert dbo.survey (name) values( 'foobar')
select scope_identity()

SQLCommand.ExecuteScalar()挿入したばかりの id 列の値を返す whichを使用する必要があります。

于 2013-11-12T02:10:22.797 に答える