简体   繁体   English

使用LIST插入存储过程

[英]INSERT using LIST into Stored Procedure

I have this stored procedure: 我有这个存储过程:

CREATE PROCEDURE [RSLinxMonitoring].[InsertFeatures] 
   @Features nvarchar(50), 
   @TotalLicenses int, 
   @LicensesUsed int, 
   @ServerName nvarchar(50) 
AS
   SET NOCOUNT ON

   INSERT INTO [RSLinxMonitoring].[FeatureServer]
        ([Features]
           ,[TotalLicenses]
           ,[LicensesUsed]
        ,[Server])
   VALUES(@Features
          ,@TotalLicenses
          ,@LicensesUsed
          ,@ServerName)

It works as expected, but since I need to insert quit a bit from my C# Linq-to-SQL class, I would like to insert a list from my application instead, is this possible? 它可以按预期工作,但是由于我需要从C#Linq-to-SQL类中插入退出,因此我想从应用程序中插入列表,这可能吗?

I have seen it been done then using SELECT statement, but not when using INSERT . 我已经看到,然后使用SELECT语句完成了操作,但是使用INSERT时却没有完成。
UPDATE: Since LINQ to SQL Doesn't support User-Defined Table Types i can't Tables. 更新:由于LINQ to SQL不支持用户定义的表类型,因此我不能使用表。 :( :(

If you are using SQL server 2008 & above, you can use below solution. 如果您使用的是SQL Server 2008及更高版本,则可以使用以下解决方案。 Declare Table type like : 声明表类型,例如:

CREATE TYPE FeatureServerType AS TABLE 
(
   [Features] nvarchar(50)
   ,[TotalLicenses] int
   ,[LicensesUsed] int
   ,[Server] nvarchar(50) 
);

Use it like : 像这样使用它:

CREATE PROCEDURE [RSLinxMonitoring].[InsertFeatures] 
   @TabletypeFeatures FeatureServerType READONLY
AS
   SET NOCOUNT ON;

   INSERT INTO [RSLinxMonitoring].[FeatureServer]
        ([Features]
           ,[TotalLicenses]
           ,[LicensesUsed]
        ,[Server])
   SELECT * FROM @TabletypeFeatures 

You should use Table type parameters. 您应该使用表类型参数。

create a class and Table type in sql server. 在sql server中创建一个类和Table类型。 Names and order should match. 名称和顺序应匹配。 Now just convert your list to Table using the following code and pass it as a paremeter to the procedure. 现在,使用以下代码将列表转换为Table并将其作为参数传递给该过程。

stored procedure help can be seen here 存储过程的帮助可以在这里看到

http://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/ http://blog.sqlauthority.com/2008/08/31/sql-server-table-valued-parameters-in-sql-server-2008/

    public static DataTable ToDataTable<T>(this List<T> iList)
    {
        DataTable dataTable = new DataTable();
        PropertyDescriptorCollection propertyDescriptorCollection =
            TypeDescriptor.GetProperties(typeof(T));
        for (int i = 0; i < propertyDescriptorCollection.Count; i++)
        {
            PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
            Type type = propertyDescriptor.PropertyType;

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                type = Nullable.GetUnderlyingType(type);


            dataTable.Columns.Add(propertyDescriptor.Name, type);
        }
        object[] values = new object[propertyDescriptorCollection.Count];
        foreach (T iListItem in iList)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
            }
            dataTable.Rows.Add(values);
        }
        return dataTable;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM