0

+1から最大値までの1002の値を表示したい場合はmax(pid)=1001、どうすればよいですか?

これは、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();

    }
4

1 に答える 1

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();
        }
    }
}
于 2013-01-09T22:32:01.217 に答える