来自JSON的SQL Bulk WritetoServer

时间:2012-07-18 17:03:22

标签: c# sql-server sql-server-2008 bulkinsert sqlbulkcopy

您好我正在尝试从JSON to SQL Server输入大块数据,所以根据我研究的内容,我可以使用WriteToSever(dataTable)将JSON to DataTable and then use SQLBulk转换为将数据写入服务器。这是最好的方式吗?

我将如何通过这种方式为每一行提取Scope_identity()

1 个答案:

答案 0 :(得分:1)

批量插入命令实际上不可能提取范围标识。如果要准确地返回insert语句中的每个scope_identity()值,则必须单独插入每个记录。

但是,你也可以考虑使用表参数插入(我在这里写了一篇关于它的文章:http://www.altdevblogaday.com/2012/05/16/sql-server-high-performance-inserts/)。

首先,创建一个表插入类型:

CREATE TYPE item_drop_bulk_table_rev4 AS TABLE (
    item_id BIGINT,
    monster_class_id INT,
    zone_id INT,
    xpos REAL,
    ypos REAL,
    kill_time datetime
)

其次,创建一个插入数据的过程:

CREATE PROCEDURE insert_item_drops_rev4
    @mytable item_drop_bulk_table_rev4 READONLY
AS

BEGIN TRANSACTION

-- Lookup the current ID
DECLARE @id_marker bigint
SELECT @id_marker = MAX(primary_key_column) FROM item_drops_rev4 

-- Insert all the data
INSERT INTO item_drops_rev4 
    (item_id, monster_class_id, zone_id, xpos, ypos, kill_time)
SELECT 
    item_id, monster_class_id, zone_id, xpos, ypos, kill_time 
FROM 
    @mytable

-- Return back the identity values
SELECT * FROM item_drops_rev4 WHERE primary_key_column > @id_marker

COMMIT TRANSACTION

第三,编写C#代码以插入此数据:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("item_id", typeof(Int64)));
dt.Columns.Add(new DataColumn("monster_class_id", typeof(int)));
dt.Columns.Add(new DataColumn("zone_id", typeof(int)));
dt.Columns.Add(new DataColumn("xpos", typeof(float)));
dt.Columns.Add(new DataColumn("ypos", typeof(float)));
dt.Columns.Add(new DataColumn("timestamp", typeof(DateTime)));

for (int i = 0; i < MY_INSERT_SIZE; i++) {
    dt.Rows.Add(new object[] { item_id, monster_class_id, zone_id, xpos, ypos, DateTime.Now });
}

// Now we&#039;re going to do all the work with one connection!
using (SqlConnection conn = new SqlConnection(my_connection_string)) {
    conn.Open();
    using (SqlCommand cmd = new SqlCommand("insert_item_drops_rev4", conn)) {
        cmd.CommandType = CommandType.StoredProcedure;

        // Adding a "structured" parameter allows you to insert tons of data with low overhead
        SqlParameter param = new SqlParameter("@mytable", SqlDbType.Structured);
        param.Value = dt;
        cmd.Parameters.Add(param);
        SqlDataReader dr = cmd.ExecuteReader()
        // TODO: Read back in the objects, now attached to their primary keys
    }
}

现在,理论上,您只能检索单个“id标记”。但是,我不相信每个版本的SQL Server都会以线性顺序从数据表插入中插入记录,因此我认为检索整个数据批量更安全。