将变量分配给新生成的ID

时间:2014-03-23 14:09:26

标签: c# dbclient

我在我的新项目中运行此查询:

dbClient.setQuery("INSERT INTO `rooms` (`roomtype`, `caption`, `owner`, `description`, `category`, `state`, `users_now`, `users_max`, `model_name`, `public_ccts`, `score`, `tags`, `icon_bg`, `icon_fg`, `icon_items`, `password`, `wallpaper`, `floor`, `landscape`, `allow_pets`, `allow_pets_eat`, `allow_walkthrough`, `allow_hidewall`, `wallthick`, `floorthick`, `achievement`, `group_id`, `game_id`, `mute_settings`, `ban_settings`, `kick_settings`) VALUES " +
                                    "('private', 'VIP CADEAU: Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 0, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');");
                                    dbClient.addParameter("username", Session.GetHabbo().Username);
                                    dbClient.runQuery();

但是现在,我希望从此查询中获取新生成的“id”(= NULL)并为其分配一个参数,以便在以下查询中将其用作@newid

dbClient.setQuery("INSERT INTO `items` (`user_id`, `room_id`, `base_item`, `extra_data`, `x`, `y`, `z`, `rot`, `wall_pos`, `rareid`) VALUES " +
"(@id, @newid, 99036, '0', 6, 6, 0.0002, 0, '', 0);");
                                    dbClient.addParameter("id", Session.GetHabbo().Id);
                                    dbClient.runQuery();

如何将第一个查询中生成的值分配给@newid?这个解决方案对我而言看似合乎逻辑,但它不起作用:

dbClient.addParameter("roomid", dbClient.setQuery("LAST_INSERT_ID();");

错误:http://puu.sh/7GmCI/f5b6794d02.png

感谢您的帮助!

@Tewr

dbClient.setQuery("INSERT INTO `rooms` (`id`, `roomtype`, `caption`, `owner`, `description`, `category`, `state`, `users_now`, `users_max`, `model_name`, `public_ccts`, `score`, `tags`, `icon_bg`, `icon_fg`, `icon_items`, `password`, `wallpaper`, `floor`, `landscape`, `allow_pets`, `allow_pets_eat`, `allow_walkthrough`, `allow_hidewall`, `wallthick`, `floorthick`, `achievement`, `group_id`, `game_id`, `mute_settings`, `ban_settings`, `kick_settings`) VALUES " +
                                    "(NULL, 'private', 'VIP CADEAU: Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 0, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');");
                                    dbClient.addParameter("username", Session.GetHabbo().Username);
                                    dbClient.setQuery("SELECT LAST_INSERT_ID();");
                        dbClient.runQuery();
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());

之后,下面的代码如下,并使用我们之前定义的@roomid。

  dbClient.setQuery("INSERT INTO `items` (`user_id`, `room_id`, `base_item`, `extra_data`, `x`, `y`, `z`, `rot`, `wall_pos`, `rareid`) VALUES " +
"(@id, @roomid, 99036, '0', 6, 6, 0.0002, 0, '', 0);");
                                    dbClient.addParameter("id", Session.GetHabbo().Id);
                                    dbClient.runQuery();

但是这会导致错误,说没有定义@roomid。

1 个答案:

答案 0 :(得分:1)

您的编译错误和现有代码提供了一些线索:

由于您在末尾缺少括号,因此该行无法编译,并且setQuery()不返回值:

dbClient.addParameter("roomid", dbClient.setQuery("LAST_INSERT_ID();");

所以用以下代码替换:

dbClient.setQuery("SELECT LAST_INSERT_ID();")
dbClient.runQuery();
dbClient.addParameter("roomid", dbClient.getRow()[0].ToString());

现在您的代码中的这一行正在编译,但它并不能保证它能够正常工作......看起来您的代码应该已经在第一时间运行了。确保使用AUTO_INCREMENT正确配置了您的表格。

相关问题