如何快速将List<Object>写入数据库?

6
请告诉我如何使WriteToBase()方法更快,或者如何进行批量插入而不需要为每个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);
            cmdIns.Parameters.Add("@other", mc[i].c);
            cmdIns.ExecuteNonQuery();
        }
    }

有什么想法吗?

4
FYI:内联SQL不是一个好主意。 - Prisoner ZERO
2
@Prisoner ZERO,OP正在使用参数--我没有看到SQL语句有什么问题。你是在建议为所有事情使用存储过程吗?我已经阅读了很多意见,认为存储过程是一个坏主意。 - LarsTech
@PrisonerZERO FYI,Google刚推出了一种可扩展的数据库(称为Spanner),其中没有SP。 - Meir Schreiber
3个回答

10

您目前正在频繁访问数据库。应该只有一个访问来完成所有插入操作。

尝试使用以下代码:

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

3
如果有人想知道,您需要添加对 System.Transactions 的引用,并在顶部包含“using System.Transactions”代码行……至少我是这样做的。 - Bmo
在循环中添加参数会引发错误,因为您正在多次添加这些参数。只有值必须在循环内分配。 - Arda Basoglu

1
使用SqlBulkCopy。它可以高效地将另一个来源的数据批量加载到SQL Server表中。
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);
    }

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接