如何从.NET上的varchar获取下一个相关的id?

时间:2017-06-09 13:48:07

标签: mysql .net

我在MySQL数据库上有这样的代码:

MD-54564
MD-54565
MD-54566

所以我需要获取下一个代码并尝试这样的事情:

string code_select = "SELECT MAX(CAST((SUBSTRING(code,4)) as UNSIGNED)) FROM table WHERE code like 'MD-%'";
DataTable   code = new DataTable();
code = conexion.mySelect(code_select);
num_code = Int32.Parse(code);
num_code = num_code + 1;
code= "MD-" + num_code.ToString();

这是在第4行给我CS1502 error

2 个答案:

答案 0 :(得分:1)

你可以在@Bean ObjectMapper dateObjectMapper() { // ... } @Bean @Primary ObjectMapper defaultObjectMapper() { // ... } 附近添加`来逃避这个词,因为它在MySQL中是keyword

以下代码正在运行:

code

查看演示:http://rextester.com/WBH92696

答案 1 :(得分:0)

string code_select = "SELECT MAX(CAST((SUBSTRING(code,4)) as UNSIGNED)) + 1 AS NextNumber FROM table WHERE code like 'MD-%'";
DataTable code = new DataTable();
code = conexion.mySelect(code_select);
DataRow row = code.Rows[0];
reg_num = row["NextNumber"].ToString();
cod_ev = "MD-" + reg_num;