检索最后插入的记录

时间:2019-07-08 09:45:26

标签: c# mysql asp.net-mvc

我想检索插入数据的最后一条记录。我研究了一下,知道不是必须使用SELECT LAST_INSERT_ID()。我已经尝试了,但是没有工作。所以我在下面提供了我的代码

<TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.019"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textViewNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="312dp"
        android:layout_marginBottom="668dp"
        android:text="Number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView11"
        app:layout_constraintTop_toBottomOf="@+id/textViewName"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="July"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />`

//“预览”页面

  //Inserting into Student Table
            string query = "INSERT INTO student(name) Values('" + txtboxname.Text + "' )";
          // query= "SELECT LAST_INSERT_ID()";
            // query += " SELECT SCOPE_IDENTITY()";
            //Inserting into course table
            string query1 = "INSERT INTO course(title) Values('" + txttitle.Text + "')";
          //  query1 = " SELECT LAST_INSERT_ID()";

            //Execute Insert Statement queries
            MySqlCommand command = new MySqlCommand(query, con);
            command.ExecuteNonQuery();
            command = new MySqlCommand(query1, con);
            command.ExecuteNonQuery();

            //Close and dispose off connections
            command.Dispose();
            con.Close();

2 个答案:

答案 0 :(得分:0)

如果您已将表ID设置为自动递增,那么根据我的知识,如果可行,则从表中查找将是您最后插入的最大ID。

答案 1 :(得分:0)

在您的代码更改如下,

行号:3 query="set @std_id=last_insert_id();"

行号:7 query1="set @title_id=last_insert_id();"

预览页面:

    string query1 = "SELECT (name) FROM student WHERE ID = query"; 
    string query2 = "SELECT (title) FROM course WHERE ID = query1";