如何使用c#使用SQL数据库进行批量插入或批量插入? [英] how to use batch insert or bulk insert using SQL database using c#?

查看:516
本文介绍了如何使用c#使用SQL数据库进行批量插入或批量插入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在努力  c#
application
 将创建新用户并将其存储到数据库中。



我想使用批处理一次创建多个记录,以便创建许多用户。



On Create按钮我需要通过添加nu来增加用户名mber或随机字符串


例如:如果用户输入的电子邮件为test@local.com,则考虑testone@local.com .. testfive@local.com,用于1到5个用户的批次。


我想我可以在这里使用简单的for循环,但是循环会每次都到达数据库我不想要我怎样才能使用批量插入或使用c#批量插入SQL数据库?



    静态   无效   主要   string   []   args    
{
字符串 用户名 = " testname" ;
字符串 电子邮件 = " test@gmail.com" ;
//将代码置于for循环中
程序 创建 username 电子邮件 );
}

公开 静态 无效 创建 string username string 电子邮件
{
字典 < 字符串 字符串 > InsertData = 字典 < 字符串 字符串 >();
InsertData 添加 email 用户名 );
控制台 WriteLine InsertData );
}




SE

解决方案

取决于您正在使用的数据访问技术。如果您正在使用EF或其他ORM,那么您可以添加/更新/删除一堆对象并将其发送批量或者最好是ORM到数据库。对于EF,有一个NuGet包,
使用批量插入。如果您使用
DataSet
然后它也会批量请求(基于配置)进行多项更改。纯ADO.NET和SQL的另一种方法是使用

SqlBulkCopy
与您的内存数据结合使用。


您是否有任何特殊原因要批量更新?您是否创建了100个他们?如果是这样的话,那么批量做是有道理的。但是,如果你谈论的是少数几个,那么批量实际上不会为你节省很多时间,而且可能不值得花费b $ b b。分析将有助于表明它是否是性能问题。


Michael Taylor

http://www.michaeltaylorp3.net


I'm working on c# application which will create new user and store it into database.

I want to use batch processing to create multiple records at once so that many users gets created.

On Create button I need to increment the username by adding a number or random strings

For example: if the user entered email as test@local.com then consider testone@local.com .. testfive@local.com for the batch of 1 to 5 users.

I think I can use simple for loop here but for loop will hit each and every time to database i do not want that how can I do it using batch insert or bulk insert into SQL database using c#?

 static void Main(string[] args)
    {
        string username = "testname";
        string email = "test@gmail.com";
       //put below code in for loop
        Program.Create(username, email);
    }

    public static void Create(string username, string email)
    {
        Dictionary<string, string> InsertData = new Dictionary<string, string>();
        InsertData.Add(email, username);
        Console.WriteLine(InsertData);
    }


SE

解决方案

Depends upon what data access technology you're using. If you're using EF or another ORM then you can add/update/delete a bunch of objects and they get sent in batches or however it is best for the ORM to the database. For EF there is a NuGet package that implements this using bulk insert. If you're using a DataSet then it batches requests (based upon the configuration) for multiple changes as well. Yet another approach for pure ADO.NET and SQL is to use the SqlBulkCopy in combination with your in memory data.

Is there any particular reason why you want to batch the updates? Are you creating 100s of them? If so then doing it in batch makes sense. But if you're talking about a handful then batching isn't really going to save you much time and probably isn't worth the effort. Profiling will help indicate if it is a performance issue.

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于如何使用c#使用SQL数据库进行批量插入或批量插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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