我有mvc应用程序。在行动中我有Dictionary<string,int>
。 Key
是ID,Value
是sortOrderNumber。我想创建存储过程,获取密钥(id)在数据库中查找此记录,并从Dictionary中orderNumber
列保存value
列。我想一次调用存储过程并将数据传递给它,而不是多次调用来更新数据。
你有什么想法吗? 谢谢!
答案 0 :(得分:16)
使用TVP的公认答案通常是正确的,但需要根据传入的数据量进行一些澄清。对于较小的数据集,使用DataTable很好(更不用说快速和简单),但对于较大的数据集设置它不会扩展,因为它通过将数据集放在DataTable中来复制数据集,只是为了将数据集传递给SQL Server。因此,对于更大的数据集,可以选择流式传输任何自定义集合的内容。唯一真正的要求是你需要根据SqlDb类型定义结构并遍历集合,这两个步骤都相当简单。
下面显示了最小结构的简单概述,它是我在How can I insert 10 million records in the shortest time possible?上发布的答案的改编,它处理从文件导入数据,因此数据目前不在,因此略有不同记忆。从下面的代码中可以看出,这种设置并不过于复杂,而且非常灵活,而且效率高,可扩展。
SQL对象#1:定义结构
-- First: You need a User-Defined Table Type
CREATE TYPE dbo.IDsAndOrderNumbers AS TABLE
(
ID NVARCHAR(4000) NOT NULL,
SortOrderNumber INT NOT NULL
);
GO
SQL对象#2:使用结构
-- Second: Use the UDTT as an input param to an import proc.
-- Hence "Tabled-Valued Parameter" (TVP)
CREATE PROCEDURE dbo.ImportData (
@ImportTable dbo.IDsAndOrderNumbers READONLY
)
AS
SET NOCOUNT ON;
-- maybe clear out the table first?
TRUNCATE TABLE SchemaName.TableName;
INSERT INTO SchemaName.TableName (ID, SortOrderNumber)
SELECT tmp.ID,
tmp.SortOrderNumber
FROM @ImportTable tmp;
-- OR --
some other T-SQL
-- optional return data
SELECT @NumUpdates AS [RowsUpdated],
@NumInserts AS [RowsInserted];
GO
C#代码,第1部分:定义迭代器/发件人
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Server;
private static IEnumerable<SqlDataRecord> SendRows(Dictionary<string,int> RowData)
{
SqlMetaData[] _TvpSchema = new SqlMetaData[] {
new SqlMetaData("ID", SqlDbType.NVarChar, 4000),
new SqlMetaData("SortOrderNumber", SqlDbType.Int)
};
SqlDataRecord _DataRecord = new SqlDataRecord(_TvpSchema);
StreamReader _FileReader = null;
// read a row, send a row
foreach (KeyValuePair<string,int> _CurrentRow in RowData)
{
// You shouldn't need to call "_DataRecord = new SqlDataRecord" as
// SQL Server already received the row when "yield return" was called.
// Unlike BCP and BULK INSERT, you have the option here to create an
// object, do manipulation(s) / validation(s) on the object, then pass
// the object to the DB or discard via "continue" if invalid.
_DataRecord.SetString(0, _CurrentRow.ID);
_DataRecord.SetInt32(1, _CurrentRow.sortOrderNumber);
yield return _DataRecord;
}
}
C#代码,第2部分:使用迭代器/发件人
public static void LoadData(Dictionary<string,int> MyCollection)
{
SqlConnection _Connection = new SqlConnection("{connection string}");
SqlCommand _Command = new SqlCommand("ImportData", _Connection);
SqlDataReader _Reader = null; // only needed if getting data back from proc call
SqlParameter _TVParam = new SqlParameter();
_TVParam.ParameterName = "@ImportTable";
// _TVParam.TypeName = "IDsAndOrderNumbers"; //optional for CommandType.StoredProcedure
_TVParam.SqlDbType = SqlDbType.Structured;
_TVParam.Value = SendRows(MyCollection); // method return value is streamed data
_Command.Parameters.Add(_TVParam);
_Command.CommandType = CommandType.StoredProcedure;
try
{
_Connection.Open();
// Either send the data and move on with life:
_Command.ExecuteNonQuery();
// OR, to get data back from a SELECT or OUTPUT clause:
SqlDataReader _Reader = _Command.ExecuteReader();
{
Do something with _Reader: If using INSERT or MERGE in the Stored Proc, use an
OUTPUT clause to return INSERTED.[RowNum], INSERTED.[ID] (where [RowNum] is an
IDENTITY), then fill a new Dictionary<string, int>(ID, RowNumber) from
_Reader.GetString(0) and _Reader.GetInt32(1). Return that instead of void.
}
}
finally
{
_Reader.Dispose(); // optional; needed if getting data back from proc call
_Command.Dispose();
_Connection.Dispose();
}
}
答案 1 :(得分:10)
使用Table Valued参数实际上并不复杂。
给出这个SQL:
CREATE TYPE MyTableType as TABLE (ID nvarchar(25),OrderNumber int)
CREATE PROCEDURE MyTableProc (@myTable MyTableType READONLY)
AS
BEGIN
SELECT * from @myTable
END
这将显示它是多么相对容易,它只是选择你发送的值用于演示目的。我相信你可以在你的案例中轻易地抽象出来。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace TVPSample
{
class Program
{
static void Main(string[] args)
{
//setup some data
var dict = new Dictionary<string, int>();
for (int x = 0; x < 10; x++)
{
dict.Add(x.ToString(),x+100);
}
//convert to DataTable
var dt = ConvertToDataTable(dict);
using (SqlConnection conn = new SqlConnection("[Your Connection String here]"))
{
conn.Open();
using (SqlCommand comm = new SqlCommand("MyTableProc",conn))
{
comm.CommandType=CommandType.StoredProcedure;
var param = comm.Parameters.AddWithValue("myTable", dt);
//this is the most important part:
param.SqlDbType = SqlDbType.Structured;
var reader = comm.ExecuteReader(); //or NonQuery, etc.
while (reader.Read())
{
Console.WriteLine("{0} {1}", reader["ID"], reader["OrderNumber"]);
}
}
}
}
//I am sure there is a more elegant way of doing this.
private static DataTable ConvertToDataTable(Dictionary<string, int> dict)
{
var dt = new DataTable();
dt.Columns.Add("ID",typeof(string));
dt.Columns.Add("OrderNumber", typeof(Int32));
foreach (var pair in dict)
{
var row = dt.NewRow();
row["ID"] = pair.Key;
row["OrderNumber"] = pair.Value;
dt.Rows.Add(row);
}
return dt;
}
}
}
可生产
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
答案 2 :(得分:3)
存储过程不支持数组作为输入。谷歌搜索使用XML或逗号分隔的字符串提供了几个黑客,但这些都是黑客。
更多SQLish方法是创建一个临时表(名为#Orders
)并将所有数据插入到该表中。然后你可以使用相同的开放式Sql连接来调用sp,并且让SP使用#Orders
表来读取值。
另一种解决方案是使用Table-Valued Parameters,但这需要更多的SQL来设置,所以我认为使用临时表方法可能更容易。