开发者

How quickly to write List<Object> to database?

开发者 https://www.devze.com 2023-03-13 06:11 出处:网络
Please explain me how to make a WriteToBase() method faster or how I can bulk insert without making calls for each insert.

Please explain me how to make a WriteToBase() method faster or how I can bulk insert without making calls for each insert.

    class MyClass
    {
        public int a;
        public int b;
        public int c;
    }
    void main()
    {
        List<MyClass> mc = new List<MyClass>();
        mc.Add(new MyClass()); //example
        mc.Add(new MyClass());

        WriteToBase(mc);
    }
    void WriteToBase(List<MyClass> mc)
    {
        //Create Connection

        string sqlIns = "INSERT INTO table (name, information, other) VALUES (@name,           @information, @other)";

        SqlCommand cmdIns = new SqlCommand(sqlIns, Connection);
        for (int i = 0; i < mc.Count; i++)
        {
            cmdIns.Parameters.Add("@name", mc[i].a);
            cmdIns.Parameters.Add("@information", mc[i].b);
     开发者_如何学C       cmdIns.Parameters.Add("@other", mc[i].c);
            cmdIns.ExecuteNonQuery();
        }
    }

Any ideas?


You are currently hitting the database many times. There should be only 1 hit for all the inserts.

Try this code:

void WriteToBase(List<MyClass> mc)
{
  //Create Connection
  using (TransactionScope scope = new TransactionScope())
  {
    string sqlIns = "INSERT INTO table (name, information, other) 
                     VALUES (@name, @information, @other)";

    SqlCommand cmdIns = new SqlCommand(sqlIns, Connection);

    for(int i=0;i<mc.Count;i++)
    {
      cmdIns.Parameters.Add("@name", mc[i].a);
      cmdIns.Parameters.Add("@information", mc[i].b);
      cmdIns.Parameters.Add("@other", mc[i].c);
      cmdIns.ExecuteNonQuery();
    }
    scope.Complete();    
  }
}


Use SqlBulkCopy. It lets you efficiently bulk load an SQL Server table with data from another source.

private static void WriteToBase(IEnumerable<MyClass> myClasses)
    {
        var dataTable = new DataTable();
        dataTable.Columns.Add("name", typeof(string));
        dataTable.Columns.Add("information", typeof(string));
        dataTable.Columns.Add("other", typeof(string));

        foreach (var myClass in myClasses)
        {
            var row = dataTable.NewRow();
            row["name"] = myClass.name;
            row["information"] = myClass.information;
            row["other"] = myClass.other;
            dataTable.Rows.Add(row);
        }

        using var connection = new SqlConnection(Constants.YourConnectionString);
        connection.Open();
        using var bulk = new SqlBulkCopy(connection) {DestinationTableName = "table" };
        bulk.WriteToServer(dataTable);
    }


void WriteToBase(List<MyClass> mc)
{
  //Create Connection
  using (TransactionScope scope = new TransactionScope())
  {
    string sqlIns = "INSERT INTO table (name, information, other) 
                     VALUES (@name, @information, @other)";

    SqlCommand cmdIns = new SqlCommand(sqlIns, Connection);

    for(int i=0;i<mc.Count;i++)
    {
      cmdIns.Parameters.AddWithValue("@name", mc[i].a);
      cmdIns.Parameters.AddWithValue("@information", mc[i].b);
      cmdIns.Parameters.AddWithValue("@other", mc[i].c);
      cmdIns.ExecuteNonQuery();
      cmdIns.Parameters.Clear();
    }
    scope.Complete();    
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消