Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper - Table-Valued Parameter with string query

Tags:

dapper

I need to use Table Valued Parameter with string query

I have the following code

string query = "SELECT * FROM Tabla1 T1 INNER JOIN @listItems T2 ON T2.Id = T1.Id";
var results = sqlConnection.Query(query , new TableValuedParameter<string>("@listItems", "string_list_Type", valuesList));

the variable valueList is a List

The execution give me a error: "Could not find stored procedure"

Is posible usa table valued parameters without use procedure ?

thanks regards

like image 512
Leandro Tuttini Avatar asked Aug 10 '15 14:08

Leandro Tuttini


People also ask

What is the use of TVP in SQL Server?

Table-valued parameters are declared by using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.

What is the use of dapper in C#?

Dapper is a micro ORM or it is a simple object mapper framework which helps to map the native query output to a domain class or a C# class. It is a high performance data access system built by StackOverflow team and released as open source.


1 Answers

Without seeing full code sample it is hard to tell what the problem could be. Consider this, which executes perfectly fine:

First, TVP definition

CREATE TYPE [dbo].[TVPSTRING] AS TABLE(
    [VALUE] NVARCHAR(MAX) NULL
)

Then sample Dapper code:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;

namespace CPTVP
{
    class Program
    {
        static void Main(string[] args)
        {
            var dt = new DataTable();
            dt.Columns.Add("StringsStringsEverywhere", typeof(string));
            foreach (int i in Enumerable.Range(0,10))
            {
                dt.Rows.Add(string.Format("{0:0000}", i));
            }

            using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Scratch;Integrated Security=true;"))
            {
                Console.WriteLine(String.Join("\n", conn.Query<string>("SELECT * FROM @tvpstr", new {tvpstr=dt.AsTableValuedParameter("dbo.TVPSTRING")})));
            }
        }
    }
}
like image 174
Darek Avatar answered Sep 19 '22 13:09

Darek