【问题标题】:Find MAX() in database and plus 1在数据库中查找 MAX() 并加 1
【发布时间】:2015-02-19 17:42:24
【问题描述】:
        int hid = 0;
        int hid_auto = 0;
        //Đổ dữ liệu
        //HID
        if (conn_class.OpenConnection() == true)
        {
            MySqlCommand hid_sql = new MySqlCommand("SELECT MAX(hid) FROM hotel", conn_class.connection);
            hid = Convert.ToInt16(hid_sql.ExecuteScalar());
            hid_auto = hid + 1;

            conn_class.CloseConnection();
        }
        txt_Hid.Text = hid_auto.ToString();
//code that insert new record into database successfully

....... 更新:插入语句

if (general.TestEmail.IsEmail(txt_Email.Text) == true)
            {
                //Insert dữ liệu vào database
                email = txt_Email.Text;
                MySqlCommand them_hotel = new MySqlCommand("INSERT INTO quan_li.hotel (hid, name, star, address, province, phone, fax, email) VALUES (@hid, @name, @star, @address, @province, @phone, @fax, @email)");
                them_hotel.CommandType = CommandType.Text;
                them_hotel.Connection = conn_class.connection;
                them_hotel.Parameters.AddWithValue("@hid", txt_Hid.Text);
                them_hotel.Parameters.AddWithValue("@name", txt_Ten.Text);
                them_hotel.Parameters.AddWithValue("@star", txt_Sao.Text);
                them_hotel.Parameters.AddWithValue("@address", txt_DiaChi.Text);
                them_hotel.Parameters.AddWithValue("@province", txt_Tinh.Text);
                them_hotel.Parameters.AddWithValue("@phone", txt_DienThoai.Text);
                them_hotel.Parameters.AddWithValue("@fax", txt_Fax.Text);
                them_hotel.Parameters.AddWithValue("@email", txt_Email.Text);

                conn_class.OpenConnection();
                them_hotel.ExecuteNonQuery();
                conn_class.CloseConnection();
                thanh_cong = "Đã thêm khách sạn thành công";
            }

我想找到隐藏列的 MAX() 值然后加 1 并发送到文本框。此事件在一个按钮中,但我第二次按下该按钮时,该值始终显示为第一次按下该按钮。

例如: 第一个:MAX() = 9 -> +1 -> 10 在文本框中。 -> 10 插入数据库 2nd ...n:文本框中的 10 和 10 插入数据库(2 行具有相同的值)。

如何将数据库中的 MAX() 值 +1?

感谢您的帮助。

P/s:对不起我的英语不好。

【问题讨论】:

  • 这是一种方法吗?您发布的代码中不存在您的插入,所以它是否插入到酒店表中? hid 是自动递增的 ID 吗?
  • 你的插入逻辑一定有问题。在插入语句中隐藏是否正确?为什么不从中找出一个身份,而不是一开始就为最大值烦恼?
  • 你确定第二次conn_class.OpenConnection()true吗?
  • @Kritner 是的,一种方法。插入代码很长,但它可以正常工作,因为新记录已添加到数据库中。唯一的问题是隐藏列的值。 hid 是一个自动递增的 ID。我是一个初学者,我不知道如何处理自动增量ID,所以我考虑一下......
  • @DannyHai an identity 列(对于 sql server,可能对其他数据库有不同的术语)是一个自动递增的数字。如果表中没有记录,则插入一条记录,然后第一条记录的 ID 为 1,但不指定列的值,第二条记录为 2,依此类推。请注意,前面的陈述有一些注意事项,但这是基本思想。

标签: c# mysql


【解决方案1】:

我读了你写的所有东西,包括在 cmets 中。

您看起来好像有一个表单,其中有一个与表中的隐藏字段相关的字段。您正在尝试向使用表单的人显示下一个 ID(隐藏)的值。

好吧,您可以显示 ID (hid) 的值,但是知道您的表有一个带有自动增量的 hid,那么您不需要在插入时包含该字段。

应该是这样的:

if (general.TestEmail.IsEmail(txt_Email.Text) == true)
{
    //Insert dữ liệu vào database
    email = txt_Email.Text;
    MySqlCommand them_hotel = new MySqlCommand("INSERT INTO quan_li.hotel (name, star, address, province, phone, fax, email) VALUES (@name, @star, @address, @province, @phone, @fax, @email)");
    them_hotel.CommandType = CommandType.Text;
    them_hotel.Connection = conn_class.connection;
    //them_hotel.Parameters.AddWithValue("@hid", txt_Hid.Text);
    them_hotel.Parameters.AddWithValue("@name", txt_Ten.Text);
    them_hotel.Parameters.AddWithValue("@star", txt_Sao.Text);
    them_hotel.Parameters.AddWithValue("@address", txt_DiaChi.Text);
    them_hotel.Parameters.AddWithValue("@province", txt_Tinh.Text);
    them_hotel.Parameters.AddWithValue("@phone", txt_DienThoai.Text);
    them_hotel.Parameters.AddWithValue("@fax", txt_Fax.Text);
    them_hotel.Parameters.AddWithValue("@email", txt_Email.Text);

    conn_class.OpenConnection();
    them_hotel.ExecuteNonQuery();
    conn_class.CloseConnection();
    thanh_cong = "Đã thêm khách sạn thành công";
}

【讨论】:

  • 我不能投票,因为我没有足够的代表。我是初学者,我不知道自动增量。谢谢你让我知道。一切正常。 :)
  • 但是您可以像正确答案一样投票。不客气。正如我所看到的,您已经有了自动增量列,您不需要更改数据库中的任何内容,只需更改 C# 脚本即可。
【解决方案2】:

如果您正在计算获取最大值并将其加一,然后保存回 dB 仅用于主键将其保存回来并增加一个值,那么您应该选择 AUTO-INCREMENT LIKE。它的语法如下:

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
)

它会自动将值以 1 的增量保存到该字段,并在保存新记录时保存,在保存到 dB 时,您无需在查询中传递该自动增量字段。

【讨论】:

  • 我不能投票,因为我没有足够的代表。我是初学者,我不知道自动增量。谢谢你让我知道。一切正常。 :)
  • @DannyHai 您仍然可以使用复选标记(在向下箭头下方)将答案标记为“正确”,这将有助于您和 Jenish 的声誉;)
猜你喜欢
  • 2021-12-06
  • 2015-08-23
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多