Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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
在sql server中将多列从c#传递到用户定义的表类型_C#_Sql_Sql Server - Fatal编程技术网

在sql server中将多列从c#传递到用户定义的表类型

在sql server中将多列从c#传递到用户定义的表类型,c#,sql,sql-server,C#,Sql,Sql Server,我试图将两个值从c#传递到执行存储过程的sql server。查看如下所示的用户定义表类型: CREATE TYPE [dbo].[Ident] AS TABLE( [Id] [uniqueidentifier] NOT NULL, [Id2] [uniqueidentifier] NOT NULL ) GO 我的存储过程的开始如下所示 CREATE procedure [dbo].[indicator] @id [dbo].[Ident] READONLY a

我试图将两个值从c#传递到执行存储过程的sql server。查看如下所示的用户定义表类型:

CREATE TYPE [dbo].[Ident] AS TABLE(
    [Id] [uniqueidentifier] NOT NULL,
    [Id2] [uniqueidentifier] NOT NULL
)
GO
我的存储过程的开始如下所示

  CREATE    procedure [dbo].[indicator]

@id [dbo].[Ident] READONLY
  as
我试图在GetValue2方法中将ID从tvp&tvp2传递到sql server表类型。但是我不能让它工作。当我通过一个值时,我可以让它工作,但如何让它在两个值时工作?任何帮助都会很好

static void Main(string[] args)
        {
            String connectionString = "........";           

        List<Guid> tempguid = new List<Guid>();
        tempguid.Add(Guid.Parse("guid...."));
        DataTable tvp = new DataTable();
        tvp.Columns.Add(new DataColumn("Id", typeof(Guid)));
        //populate DataTable from your List here
        foreach (var id in tempguid)
            tvp.Rows.Add(id);

        List<Guid> tempguidid2 = new List<Guid>();
        tempguid.Add(Guid.Parse("guid....."));
        DataTable tvp2 = new DataTable();
        tvp2.Columns.Add(new DataColumn("Id", typeof(Guid)));
        //populate DataTable from your List here
        foreach (var id in tempguid)
            tvp2.Rows.Add(id);

        Console.WriteLine(GetValue2(ref tvp, connectionString));




    }

    public static List<Guid> GetValue2(ref DataTable tvp, ref DataTable tvp2, String connectionString)
    {

        List<Guid> items = new List<Guid>();

        using (var conn = new SqlConnection(connectionString))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("[dbo].[indicator]", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter tvpParameter = new SqlParameter();
            tvpParameter.ParameterName = "@id";
            tvpParameter.SqlDbType = System.Data.SqlDbType.Structured;
            tvpParameter.Value = tvp;
            tvpParameter.TypeName = "[dbo].[Ident]";
            cmd.Parameters.Add(tvpParameter);

            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    Console.WriteLine((int)rdr["type"]);
                }
                Console.ReadLine();
            }
        }
        return items;
    }
static void Main(字符串[]args)
{
字符串连接字符串=“……”;
List tempguid=新列表();
tempguid.Add(Guid.Parse(“Guid….”);
DataTable tvp=新DataTable();
添加(新的数据列(“Id”,typeof(Guid));
//从此处的列表中填充DataTable
foreach(tempguid中的变量id)
tvp.Rows.Add(id);
List tempguidid2=新列表();
tempguid.Add(Guid.Parse(“Guid….”);
DataTable tvp2=新DataTable();
添加(新的数据列(“Id”,typeof(Guid));
//从此处的列表中填充DataTable
foreach(tempguid中的变量id)
tvp2.Rows.Add(id);
控制台写入线(GetValue2(参考tvp,connectionString));
}
公共静态列表GetValue2(参考数据表tvp、参考数据表tvp2、字符串连接字符串)
{
列表项=新列表();
使用(var conn=newsqlconnection(connectionString))
{
conn.Open();
SqlCommand cmd=newsqlcommand(“[dbo].[indicator]”,conn);
cmd.CommandType=CommandType.storedProcess;
SqlParameter tvpParameter=新的SqlParameter();
tvpParameter.ParameterName=“@id”;
tvpParameter.SqlDbType=System.Data.SqlDbType.Structured;
tvpParameter.Value=tvp;
tvpParameter.TypeName=“[dbo].[Ident]”;
cmd.Parameters.Add(tvpParameter);
使用(SqlDataReader rdr=cmd.ExecuteReader())
{
while(rdr.Read())
{
Console.WriteLine((int)rdr[“type”]);
}
Console.ReadLine();
}
}
退货项目;
}

调用[dbo].[indicator]存储过程时,GetValue2方法已经可以正常工作。您必须更正main方法以正确测试它,如下所示,以绕过错误“尝试传递具有1列的表值参数,而相应的用户定义表类型需要2列。”


TVP被定义为有两列,但在C代码中只指定了一列。此外,“不工作”不是一个有用的问题陈述-请具体说明(结果是否不是预期的,是否出现异常等)。@IanKemp我收到此错误“试图传递一个具有1列的表值参数,而相应的用户定义表类型需要2列”。”。。。这正是我说的。
static void Main(string[] args) {
    String connectionString = "........"; //Replace with your specific connection string

    DataTable tvp = new DataTable(); //Creates the two columns Id,Id2 required to map to the database table type [dbo].[Ident]
    tvp.Columns.Add(new DataColumn("Id", typeof(Guid)));
    tvp.Columns.Add(new DataColumn("Id2", typeof(Guid)));

    //Just adding a row to datatable tvp
    var newRow = tvp.NewRow();
    newRow["Id"] = new Guid();  //or Guid.Parse("guid....") using a valid GUID string
    newRow["Id2"] = new Guid(); //or Guid.Parse("guid....") using a valid GUID string
    tvp.Rows.Add(newRow);

    DataTable tvp2 = new DataTable(); 
    // - tvp2 is declared just to call GetValue2 
    // - GetValue2 has a parameter tv2 that is not used
    Console.WriteLine(GetValue2(ref tvp, ref tvp2, csb.ConnectionString));

}