将Dictionary <string,int>传递给存储过程T-SQL

IFr*_*izy 7 t-sql sql-server import asp.net-mvc

我有mvc应用程序.在行动中我有Dictionary<string,int>.的Key是ID和Value是sortOrderNumber.我想创建存储过程,将获取密钥(id)在数据库中查找此记录并从Dictionary中保存orderNumbervalue.我想一次调用存储过程并将数据传递给它,而不是多次调用更新数据.

你有什么想法吗?谢谢!

Sol*_*zky 16

接受使用TVP的答案通常是正确的,但需要根据传入的数据量进行一些澄清.对于较小的数据集,使用DataTable很好(更不用说快速和简单),但对于较大的数据集,它可以因为它只是为了将数据集传递给SQL Server而将数据集放在DataTable中,因此它不会重复数据集.因此,对于更大的数据集,可以选择流式传输任何自定义集合的内容.唯一真正的要求是你需要根据SqlDb类型定义结构并遍历集合,这两者都是相当简单的步骤.

下面显示了最小结构的简单概述,这是我发布的答案的改编如何在最短的时间内插入1000万条记录?,它处理从文件导入数据,因此数据当前不在内存中略有不同.从下面的代码中可以看出,这种设置并不过于复杂,而且非常灵活,高效且可扩展.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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;
      }
}
Run Code Online (Sandbox Code Playgroud)

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();
   }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*rne 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
Run Code Online (Sandbox Code Playgroud)

这将显示它是多么相对容易,它只是选择你发送的值用于演示目的.我相信你可以很容易地在你的情况下抽象出来.

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

产生

0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
Run Code Online (Sandbox Code Playgroud)

  • 如果传入大量数据,那么通过IEnumerable接口传输数据会更有效,而不是以DataTable的形式制作原始数据的第二个副本.我写了一篇关于这个方法的文章,包括一个工作示例:http://www.sqlservercentral.com/articles/SQL+Server+2008/66554/ (2认同)