如何在mysql / C#中选择最后一个ID [英] How do I select the last ID in mysql/C#

查看:113
本文介绍了如何在mysql / C#中选择最后一个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MySqlConnection connection = new MySqlConnection(datasource = 123.456.789; port = 3306; username = blabla; password = blabla);

MySqlCommand命令;

MySqlDataReader mdr;





connection.Open();

string selectQuery =SELECT_LAST_INSERT_ID()* FROM mysqlcshap .Alamierung;

command = new MySqlCommand(selectQuery,connection);

mdr = command.ExecuteReader();

if(mdr。阅读())



我尝试过:



怎么样我修好了吗?







 SELECT_LAST_INSERT_ID()* FROM 

解决方案

简单:不要。

这根本不相关,特别是你使用它的方式,或计划使用它。



问题是MySql本质上是一个多用户系统 - whi如果你试图使用最后一个ID值,ch会给你带来问题,无论你想用它做什么。



如果你想用它来访问数据你刚刚创建,你不能像那样可靠地做到这一点,因为你获得的价值可能与你的用户无关 - 它很可能是由一个完全不同的用户创建的。获取所需ID的唯一方法是使用 LAST_INSERTED_ID [ ^ ]作为同一命令的一部分,或者在非常非常相同的连接中运行。



如果你想用它来通过在实际执行INSERT之前向其添加一个来提供下一个ID值,这更加危险,因为您无法知道在使用它之前需要多长时间,并且该ID可能已被众多ID使用其他用户已经和你的数据库完整性完全妥协了。



在插入行之前你永远不需要ID值,任何依赖于预测的系统ID值将以一些非常非常恶劣的方式在生产中失败。



如果你想使用自动化tic ID值提供外键,然后我强烈建议您使用GUID ID值,这样您就可以控制正在发生的事情。行ID不应该用作userID或customerID ...


试试这个:



  SELECT  MAX(ID) AS  LastInsertedID; 


MySqlConnection connection = new MySqlConnection("datasource=123.456.789 ;port=3306;username=blabla ;password=blabla");
MySqlCommand command;
MySqlDataReader mdr;


connection.Open();
string selectQuery = "SELECT_LAST_INSERT_ID() * FROM mysqlcshap.Alamierung";
command = new MySqlCommand(selectQuery, connection);
mdr = command.ExecuteReader();
if (mdr.Read())

What I have tried:

How can i fix this?



SELECT_LAST_INSERT_ID() * FROM 

解决方案

Simple: don't.
It's not relevant at all, and particularly the way you are using it, or planning to use it.

The problem is that MySql is by its very nature a Multiuser system - which gives you problems if you try to use the last ID value, regardless of what you want to use it for.

If you want to use it to access the data you just created, you can't do it reliably like that as the value you get may not be relevant to your user - it could well have been created by a totally different user. The only way to get the ID that you wanted is to SELECT it as part of the INSERT operation using the LAST_INSERTED_ID[^] function as part of teh same command, or at the very, very least same connection.

If you want to use it to provide a "next ID" value by adding one to it before you actually do an INSERT, that's even more dangerous, because you have no way of knowing how long it will be before you use it, and that ID can have been used by numerous other users already and your database integrity completely compromised as a result.

You never need an ID value until a row is inserted, and any system which relies on "predicting" ID values is going to fail in production in some really, really nasty ways.

If you are trying to use automatic ID values to provide foreign keys, then I'd strongly suggest you use GUID ID values instead, so you have control over what is going on. The row ID should not be used as a userID or customerID at all...


Try this:

SELECT MAX(ID) AS LastInsertedID;


这篇关于如何在mysql / C#中选择最后一个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆