假设我有max(pid)= 1001,如果我想显示1002的值,通过+1到最大值,所以我该怎么办

时间:2013-01-09 22:26:20

标签: sql

假设我有max(pid)=1001,如果我想将1002的值显示为+1到最大值,那我该怎么办

这是我的整个代码,我想选择max(pid)并希望将其显示到文本框

public PatientRegistration()
    {
        InitializeComponent();
        string connectionstring = "DATABASE=hmanagmentsystem;UID=root;PASSWORD=;SERVER=localhost";
        con = new MySqlConnection(connectionstring);
        con.Open();
    }

  private void PatientRegistration_Load(object sender, EventArgs e)
    {

        MySqlCommand command = new MySqlCommand("select max(pid) from patientreg",con);
        int cmd=Int32.Parse(command.ExecuteScalar().ToString());
        con.Close();

    }

1 个答案:

答案 0 :(得分:2)

怎么样:

MySqlCommand command = new MySqlCommand("select max(pid) + 1 from patientreg", 
    con);

更全面:

string connectionString;
public PatientRegistration()
{
    InitializeComponent();
    connectionString = "DATABASE=hmanagmentsystem;UID=root;PASSWORD=;SERVER=localhost";
}

private void PatientRegistration_Load(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open;

        using (SqlCommand = new SqlCommand("select max(pid) + 1 from patientreg",conn))
        {
            // this assumes an asp:TextBox called IDTextBox
            IDTextBox.Text = command.ExecuteScalar().ToString();
        }
    }
}