C# 如何将数组作为参数传递到存储过程中?

C# 如何将数组作为参数传递到存储过程中?,c#,arrays,sql-server,parameters,C#,Arrays,Sql Server,Parameters,我试图将一个字符串从C#发送到SQL server,但我不知道如何使用存储过程将该字符串插入到另一个表中 我希望插入数据的表只有两列,@IDUser(我也是从C#传递的一个参数,对于所有idproduct都是相同的)和类似于“1,2,3,4”的@idproduct字符串 所以我想要的是一个存储过程来分别插入@idUser和数组值,就像 IDUSER | IDPRODUCT 777 1 777 2 777 3 777 4 我正在使用sql

我试图将一个字符串从C#发送到SQL server,但我不知道如何使用存储过程将该字符串插入到另一个表中

我希望插入数据的表只有两列,@IDUser(我也是从C#传递的一个参数,对于所有idproduct都是相同的)和类似于“1,2,3,4”的@idproduct字符串

所以我想要的是一个存储过程来分别插入@idUser和数组值,就像

IDUSER | IDPRODUCT
777        1
777        2
777        3
777        4
我正在使用sql server 2016

我已经找到了这个问题的可能答案,但与我的SQL Server知识水平相比,它们看起来非常复杂


谢谢

我想你可以尝试以下两种方法:

  • 没有存储过程:您可以尝试使用类
  • C#代码:

    C#代码:

        private static void ExecuteProcedure( Dictionary<int, List<int>> dto)
        {
            string connectionString = GetConnectionString();
    
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "dbo.UserProductsInsert";
                    command.CommandType = CommandType.StoredProcedure;
    
                    SqlParameter parameter = command.Parameters.AddWithValue("@userProductsType", CreateDataTable(dto));
                    parameter.SqlDbType = SqlDbType.Structured;
                    parameter.TypeName = "dbo.UserProductsType";
    
                    command.ExecuteNonQuery();
                }
            }
        }
    
    private static void ExecuteProcedure(字典dto)
    {
    string connectionString=GetConnectionString();
    使用(SqlConnection连接=新的SqlConnection(connectionString))
    {
    connection.Open();
    使用(SqlCommand=connection.CreateCommand())
    {
    command.CommandText=“dbo.UserProductsInsert”;
    command.CommandType=CommandType.storedProcess;
    SqlParameter parameter=command.Parameters.AddWithValue(“@userProductsType”,CreateDataTable(dto));
    parameter.SqlDbType=SqlDbType.Structured;
    parameter.TypeName=“dbo.UserProductsType”;
    command.ExecuteNonQuery();
    }
    }
    }
    
    这应该会告诉您如何操作。如果您仍然无法获得它,请告诉我,我会给您一个答案。如果您希望通过sql中的参数执行此操作,请在使用2016时使用
    STRING\u SPLIT
    CREATE TABLE dbo.UserProducts
    (
        IdUser INT NOT NULL,
        IdProduct INT NOT NULL
    );
    GO
    
    CREATE TYPE dbo.UserProductsType AS TABLE
    (
        IdUser INT NOT NULL,
        IdUser INT NOT NULL
    );
    GO
    
    CREATE PROCEDURE dbo.UserProductsInsert
        @userProductsType dbo.UserProductsType READONLY
    AS
    BEGIN
    
        INSERT INTO UserProducts
        SELECT * FROM @userProductsType 
    END
    
        private static void ExecuteProcedure( Dictionary<int, List<int>> dto)
        {
            string connectionString = GetConnectionString();
    
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandText = "dbo.UserProductsInsert";
                    command.CommandType = CommandType.StoredProcedure;
    
                    SqlParameter parameter = command.Parameters.AddWithValue("@userProductsType", CreateDataTable(dto));
                    parameter.SqlDbType = SqlDbType.Structured;
                    parameter.TypeName = "dbo.UserProductsType";
    
                    command.ExecuteNonQuery();
                }
            }
        }