在使用dbClient时插入大量值

时间:2014-03-23 13:14:53

标签: c# c++ .net dbclient

目前我正在尝试在我的新项目中插入很多值,但我无法弄清楚如何做到这一点。我的“最简单”的SQL查询如下所示,此函数的工作方式如下:

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();     

但是现在我想插入多个值,并使用以下查询:

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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1'),
('private', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 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)以使其正常工作......一个逻辑但混乱的解决方案可能是:

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.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');");

但我不想在我的代码中重复'插入'1000次......

2 个答案:

答案 0 :(得分:1)

选项:

  • 循环,使用简单的代码多次使用不同的参数
  • 使用可以为您做到这一点的工具(dapper在那里有一些接近基本的技巧,大多数ORM都允许基于列表的简单插入)
  • 适用于非常大的插页,"批量复制"如果您的提供商支持它 - 可能使用FastMember将列表转换为IDataReader

答案 1 :(得分:1)

您只需要多个INSERT语句:

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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');

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', 'Penthouse', @username, 'Ontvang deze kamer GRATIS bij het lid worden van VIP (http://wonderhotel.nl/vip).', 11, 'open', 7, 25, 'model_i', '', 1, '', 1, 0, '', '', '0.0', '0.0', '0.0', '1', '0', '0', '0', -2, -2, 0, 0, 0, '0', '1', '1');