Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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# Can';t在存储过程内连接到tvp_C#_Sql_Sql Server_Stored Procedures_Table Valued Parameters - Fatal编程技术网

C# Can';t在存储过程内连接到tvp

C# Can';t在存储过程内连接到tvp,c#,sql,sql-server,stored-procedures,table-valued-parameters,C#,Sql,Sql Server,Stored Procedures,Table Valued Parameters,当我从c调用SP(使用JOIN任何表[dbo].[pru 2])时,我什么也得不到(我的DataReader包含0行)。但如果我在MSSMS中执行相同的SP,我就会得到我想要的 我无法将表连接到tvp参数 我的电视节目 CREATE TYPE [dbo].[GuidList] AS TABLE( [Id] [uniqueidentifier] NOT NULL ) 存储过程[dbo].[pr_1] 这很好。我在MSSMS和C#中得到结果 但如果试图连接任何数据(或表),我在C#中什么也

当我从
c
调用SP(使用
JOIN
任何表
[dbo].[pru 2]
)时,我什么也得不到(我的
DataReader
包含
0行
)。但如果我在
MSSMS
中执行相同的SP,我就会得到我想要的

我无法将表连接到tvp参数

我的电视节目

CREATE TYPE [dbo].[GuidList] AS TABLE(
    [Id] [uniqueidentifier] NOT NULL
)
存储过程
[dbo].[pr_1]
这很好。我在
MSSMS
C#
中得到结果

但如果试图连接任何数据(或表),我在C#中什么也得不到,但在MSSM中我得到了我想要的

存储过程
[dbo].[pr_2]

CREATE PROCEDURE [dbo].[pr_2]
  @tvp [dbo].[GuidList] READONLY
AS

BEGIN
    SELECT 
        [id]
        ,[Name]
        ,[Email]
    FROM [dbo].[User] users
        inner join @tvp tvp on tvp.Id = users.UserId
    WHERE users.isActive = 1
END
还有我的c代码

我想要什么就回来什么

这是我的连接字符串

Data source=srv;Initial catalog=catcalog;Persist security info=True;User id=test;Password=pass;Connect Timeout=30;Max Pool Size=100;Connect Timeout=60;Failover Partner=SRV;

我不确定,我用了和你一样的方法,而且效果很好。尝试更改此行-tvp.TableName=“[dbo].[GuidList]”;至tvp.TableName=“GuidList”@但是我的[dbo].[pr_1]正在使用tvp.TableName=“[dbo].[GuidList]”;我想,这可能是sql server设置的原因。您尝试过这个吗?因为这是我在工作代码中注意到的唯一区别。其余的都是一样的。您不需要在参数上指定类型名称,也不需要在数据表上指定表名。我猜问题在于,当从代码运行过程时,传递的数据与从SSM运行过程时传递的数据不同。在sql server的联接中使用TVP没有任何问题。
DataTable tvp = new DataTable();
tvp.TableName = "[dbo].[GuidList]";
tvp.Columns.Add("Id", typeof(Guid));

for (int i = 0; i < 10; i++)
{
    DataRow row = tvp.NewRow();
    //just for example
    row["Id"] = Guid.NewGuid();
    tvp.Rows.Add(row);
}

DataTable result = new DataTable();

using (SqlConnection connection = new SqlConnection("MyConStr"))
{
    using (SqlCommand command = new SqlCommand())
    {
        try
        {
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "[dbo].[pr_2]";

            SqlParameter tvpSqlParam = new SqlParameter("@tvp", tvp);
            tvpSqlParam.SqlDbType = SqlDbType.Structured;
            tvpSqlParam.TypeName = "[dbo].[GuidList]";

            command.Parameters.Add(tvpSqlParam);

            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                result.Load(reader);
            }

        }
        catch (SqlException sqlException)
        {
        }
        finally
        {
            command.Parameters.Clear();
        }
    }
}
declare @TVP [dbo].[GuidList]
insert into @TVP
SELECT [ContractTemplateId]
FROM [dbo].[MyTable]

exec [dbo].[pr_2] @tvp = @TVP
Data source=srv;Initial catalog=catcalog;Persist security info=True;User id=test;Password=pass;Connect Timeout=30;Max Pool Size=100;Connect Timeout=60;Failover Partner=SRV;