简体   繁体   English

表中最后一条记录的ID

[英]ID of last record in table

I have an ODBC example that connects to a MySQL database using the dsn DSN=RDBMSTest (I've created this to connect to my database. 我有一个ODBC示例使用dsn DSN = RDBMSTest连接到MySQL数据库(我创建它连接到我的数据库。

I'm trying to find the Last inserted ID but I am not getting the last ID from my table. 我正在尝试找到最后插入的ID,但我没有从我的表中获取最后一个ID。

Here is my code: 这是我的代码:

using System;
using System.Data.Odbc;

namespace TestClient
{
    class Program
    {
        private static OdbcCommand command;
        static void Main(string[] args)
        {
            // test odbc connection and get last insert id
            string dsn = "DSN=RDBMSTest";
            using(OdbcConnection connection = new OdbcConnection(dsn))
            {
                connection.Open();
                OdbcTransaction transaction = null;
                command = new OdbcCommand();
                try
                {
                    transaction = connection.BeginTransaction();
                    command.Connection = connection;
                    command.Transaction = transaction;
                    command.CommandText = "SELECT * FROM test.new_table ORDER BY ID DESC";
                    command.ExecuteNonQuery();
                    command.CommandText = "SELECT last_insert_id()";
                    command.ExecuteNonQuery();
                    transaction.Commit();

                    OdbcDataReader reader = command.ExecuteReader();
                    while(reader.Read())
                    {
                        for(int i = 0; i < reader.FieldCount; i++)
                        {
                            Console.WriteLine(reader.GetName(i) + ": " + reader.GetValue(i));
                        }
                    }
                    reader.Close();
                    transaction.Dispose();
                    command.Dispose();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            Console.Read();
        }
    }
}

I am getting an ID of 0 and my last ID should be 186 according to the table (test_table) I am using. 根据我正在使用的表(test_table),我得到的ID为0,我的最后一个ID应该是186。

last_insert_id() applies to INSERT queries only. last_insert_id()仅适用于INSERT查询。 The last insert performed by a particular connection. 最后一次插入由特定连接执行。 It will not report on inserts done in other connections, it will not report on inserts you did with your account 10 days ago. 它不会报告在其他连接中执行的插入操作,它不会报告您在10天前对您的帐户执行的插入操作。

You're not doing an insert, just a SELECT . 你没有做插入,只是一个SELECT

You need to use SELECT MAX(id) instead... and note that this is highly racy. 你需要使用SELECT MAX(id)代替......并注意这是非常活泼的。 If you're using it to generate client-side IDs, don't. 如果您使用它来生成客户端ID,请不要。 Another parallel client can swoop in behind your back and steal away the ID you're generating, or create an even newer/bigger ID. 另一个并行客户端可以在你的背后俯冲并偷走你正在生成的ID,或者创建一个更新/更大的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM