Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表参数传递给SQL Server存储过程_C#_Sql Server 2012 - Fatal编程技术网

C# 将列表参数传递给SQL Server存储过程

C# 将列表参数传递给SQL Server存储过程,c#,sql-server-2012,C#,Sql Server 2012,我刚接触c#和.Net,我需要你的帮助 我已经在DB中创建了一个接受表值参数(id列表)的过程,我正在寻找一个Oracle关联数组的合适等价物 --数据库中的新数据类型: CREATE TYPE IDType AS TABLE ( ID bigint ) GO -- DB Procedure Create procedure TVP_PROC (@ID IDType readonly) Declare @TVP IDType Insert into @T

我刚接触c#和.Net,我需要你的帮助

我已经在DB中创建了一个接受表值参数(id列表)的过程,我正在寻找一个Oracle关联数组的合适等价物

--数据库中的新数据类型:

CREATE TYPE IDType AS TABLE
(
        ID bigint  
)
GO



-- DB Procedure
Create procedure TVP_PROC (@ID IDType readonly)
    Declare @TVP IDType
    Insert into @TVP select ID from @ID
As
  Update  my_tbl set id=(select ID from @TVP)
它应该这样运行:exec TVP_PROC@ID/@ID是ID的列表

.Net代码:

Public void UpdateID (List long<long> IDS )
{
    Datatable datatable = new datatable ();
    Datatable.columns.add (“IDS”,typeof(int64));
    Foreach (int64 id in IDS)
    {
        Datatable.Rows.Add(id);
    }
}


Var Query =hibernate.createsqlquery(“Exec TVP_PROC @ID:IDS”);
Public void UpdateID(列出长ID)
{
Datatable=新的Datatable();
Datatable.columns.add(“id”,typeof(int64));
Foreach(id中的int64 id)
{
Datatable.Rows.Add(id);
}
}
Var Query=hibernate.createsqlquery(“Exec TVP_PROC@ID:IDS”);
查询

问题:

  • 除了使用datatable变量并在每次迭代中分配ID之外,还有其他更好的编写方法吗

  • 更重要的是,分配table\list变量的适当方式是什么?是否存在可定义为此类数据类型的表值\数组变量


  • 现在,您已经在数据库中创建了表类型和存储过程,在C#中,您需要这样的代码:

    // define connection and command object
    using(SqlConnection conn = new SqlConnection("your-connection-string-here"))
    using(SqlCommand cmd = new SqlCommand("dbo.TVP_PROC", conn))
    {
       // define the command to be a stored procedure
       cmd.CommandType = CommandType.StoredProcedure;
    
       // set up parameters
       cmd.Parameters.Add("@ID", SqlDbType.Structured).Value = datatable;
    
       // open connection, execute stored procedure, close connection
       conn.Open();
       cmd.ExecuteNonQuery();
       conn.Close();
    }
    

    现在是2012年,有了表值参数。Thx很多,工作很好!!!
        Datatable datatable = new datatable ();
        Datatable.columns.add (“IDS”,typeof(int64));
        Foreach (int64 id in IDS)
        {
            Datatable.Rows.Add(id);
        }
    
     // Configure the SqlCommand and table-valued parameter.
     SqlCommand insertCommand = new SqlCommand("TVP_PROC", connection);
    
     insertCommand.CommandType = CommandType.StoredProcedure;
    
     //pass here your datatable
     SqlParameter tvpParam = insertCommand.Parameters.AddWithValue("@ID", datatable);
    
     //specify type of your parameter
     tvpParam.SqlDbType = SqlDbType.Structured;
    
      // Execute the command.
      insertCommand.ExecuteNonQuery();