Cql批量复制/插入C#

我是JSON和SQLBulkCopy的新手。 我有一个JSON格式的POST数据,我希望使用C#在Microsoft SQL中批量复制/插入。

JSON格式:

{ "URLs": [{ "url_name": "Google", "url_address": "http://www.google.com/" }, { "url_name": "Yahoo", "url_address": "http://www.yahoo.com/" }, { "url_name": "FB", "url_address": "http://www.fb.com/" }, { "url_name": "MegaSearches", "url_address": "http://www.megasearches.com/" }] } 

类别:

 public class UrlData { public List URLs {get;set;} } public class Url { public string url_address {get;set;} public string url_name {get;set;} } 

我怎样才能有效地做到这一点?

因为你只需要加载

10到50个url

显然没有必要使用SqlBulkCopy – 它适用于数千个插入。 除非您需要多次重复此操作。

因此,如果你有一个url列表,即List,那么只需从列表中循环遍历所有URL并将它们插入到数据库中,例如

 string insertQuery = "insert into TUrls(address, name) values(@address, @name)"; foreach (URL url in listOfUrls) { SqlCommand cmd = new SqlCommand(insertQuery); cmd.Parameters.AddWithValue("@name", url.url_name); cmd.Parameters.AddWithValue("@address", url.urld_address); // don't forget to take care of connection - I omit this part for clearness cmd.ExecuteNonQuery(); } 

但是如果你真的需要使用SqlBulkCopy你需要将类URL的对象转换为DataTable 。 要做到这一点,请看Marc Gravell的答案 :

这是使用NuGet的FastMember进行的 2013年更新:

 IEnumerable data = ... DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } 

是的,这几乎与这个完全相反; reflection就足够了 – 或者如果你需要更快,2.0中的HyperDescriptor ,或3.5中的Expression 。 实际上, HyperDescriptor应该足够了。

例如:

 // remove "this" if not on C# 3.0 / .NET 3.5 public static DataTable ToDataTable(this IList data) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); for(int i = 0 ; i < props.Count ; i++) { PropertyDescriptor prop = props[i]; table.Columns.Add(prop.Name, prop.PropertyType); } object[] values = new object[props.Count]; foreach (T item in data) { for (int i = 0; i < values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } return table; } 

因此,您可以使用Marc的解决方案之一从List创建DataTable 。 然后你只需要在服务器上写表到目标表:

 string csDestination = "put here connection string to database"; using (SqlConnection destinationConnection = new SqlConnection(csDestination)) using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection)) { bulkCopy.DestinationTableName = "TUrls"; bulkCopy.WriteToServer(dataTableOfUrls); } 

希望能帮助到你。

UPD

  1. 回答@ pseudonym27问题:“我可以使用BulkCopy类将数据附加到SQL数据库中的现有表吗?”

当然你可以,因为BulkCopy只是插入命令,但它有点不同,就是这样。 我建议你使用中间表,以防万一操作很有可能出错(你想尽可能少地占用你的目标表)或者你需要做一些数据转换,但只有你觉得需要它。

您应该使用表值参数。 如果你正在使用> sql server 2005.你可以在这里有一个例子

如果它只是10-50个url,不经常插入,你可以触发插入语句。 简单而轻松,您可以使用简单快捷的东西,如小巧玲珑。

否则,如果您想要批量复制,则需要首先从JSON创建并填充ADO.NET数据表 – 最好匹配目标sql表的模式。 这是你的选择。

我不知道你在使用什么数据库。 但我有postgres的经验,我认为这在其他关系数据库中是类似的:

在数据库中,您也可以从csv分隔值复制(例如Postgres )。 如果根据精确的输入格式格式化字符串,这应该是最快的方法。