ADO NET C# 如何将 collection 添加到数据库?

ADO NET C# how can I add collection to database?

我只添加一条记录没有任何问题。

public int Add(Company Company)
        {
            var cmd = new SqlCommand("MD.CompanyAdd", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandTimeout = 0;

            try
            {
                conn.Open();

                cmd.Parameters.AddWithValue("@CompanyName", Company.CompanyName);
                cmd.Parameters.AddWithValue("@CompanyCode", Company.CompanyCode);
                cmd.Parameters.AddWithValue("@RateType", Company.RateType);

                var result = cmd.ExecuteScalar();

                //var result = cmd.ExecuteNonQuery();
                //var qwe = result.ToString();

                return int.Parse(result.ToString());
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }

但我不明白如何添加 collection。 我想我应该用 DataTable 做点什么,但我不明白到底是什么。

class CompanyRepository
    {
        private string _connectionString;
        private SqlConnection conn;

        public CompanyRepository(string ConnectionString)
        {
            _connectionString = ConnectionString;
            conn = new SqlConnection(_connectionString);
        }

        public void AddList(IList<Company> Companies)
        {
            try
            {
                conn.Open();

                var cmd = new SqlCommand("MD.CompanyAddList", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandTimeout = 0;

                DataTable dataTable = new DataTable();

                dataTable.Columns.Add("[CompanyCode]", typeof(string));
                dataTable.Columns.Add("[CompanyName]", typeof(string));
                dataTable.Columns.Add("[RateType]", typeof(string));


                foreach (var Company in Companies)
                {
                    dataTable.Rows.Add(Company);
                }

                cmd.Parameters.AddWithValue("@ImportTable", dataTable);

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
    }

    public class Company 
    {
        public int CompanyId { get; set; } // CompanyID (Primary key)
        public string CompanyCode { get; set; } // CompanyCode (length: 10)
        public string CompanyName { get; set; } // CompanyName (length: 256)
        public int? RateType { get; set; } // RateType

    }

请帮助我!

P.S。 请原谅我的垃圾邮件,但网站需要更多文本才能创建 post。我希望这就够了。

我认为这可能有效。

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
foreach (Entry entry in entries)
   dt.Rows.Add(new string[] { entry.Id, entry.Name });

using (SqlBulkCopy bc = new SqlBulkCopy(connection))
{   // the following 3 lines might not be neccessary
    bc.DestinationTableName = "Entries";
    bc.ColumnMappings.Add("Id", "Id");
    bc.ColumnMappings.Add("Name", "Name");

    bc.WriteToServer(dt);
}

复制自here