c# - 使用 dbClient 插入大量值

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

但我不想在我的代码中重复“INSERT INTO”超过 1000 次..

最佳答案

选项:

  • 循环,使用不同参数多次使用简单代码
  • 使用可以为您完成此操作的工具(dapper 有一些接近基本的技巧,大多数 ORM 将允许简单的基于列表的插入)
  • 对于非常大的插入,“批量复制”(如果您的提供商支持的话)- 可能使用 FastMember 将列表转换为 IDataReader

关于c# - 使用 dbClient 插入大量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22591379/

相关文章:

c# - 在 Json.Net 中生成换行符而不是 CRLF

python - 在 Python 中传递 "pointer to a virtual function"作为参数

c++ - g++错误:word64没有命名类型

C++ - 多个文件中的全局变量

.net - 验证签名的 Word 文档的正确方法是什么?

.net - 如何在 Visual Studio 2013/TFS 中管理类库演变?

c# - 来自 .Net 的异步电子邮件

c# - winsock 资源不足

c# - 如何使用 LINQ to SQL 和 C# 进行正确的连接?

c# - 条件表达式中的数据类型不匹配