0

I have something like that on DB side:

CREATE TYPE dbo.idstable AS TABLE
(
    Idx bigint NOT NULL PRIMARY KEY
);
GO

CREATE PROCEDURE [usp_GetIds]
    @input idstable READONLY
AS
    SELECT * from @input
go

Id like to call usp_GetIds from C# code using odbc.

using (var command = new OdbcCommand("usp_GetIds", conn))
{
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddRange(parameters);

    using (var reader = command.ExecuteReader())
    {
     ....
    }
}

Right now I stuck on something like that:

var parameters = new[] { new OdbcParameter("@input", CreateDataTable(ids)) };

private static DataTable CreateDataTable(IEnumerable<int> Ids)
{
    DataTable table = new DataTable();
    table.Columns.Add("Idx", typeof(Int23));
    foreach (int id in Ids)
    {
        table.Rows.Add(id);
    }
    return table;
}

and message: "No mapping exists from object type System.Data.DataTable to a known managed provider native type."

Is there any way to pass table as input param to stored procedure using odbc? or I have to use SqlConnection/SqlCommand/SqlParameter?

Neska
  • 110
  • 4
  • why are you using ODBC if you are hitting MS SQL Server? – Crowcoder Feb 09 '18 at 13:55
  • Possible duplicate of [Pass table valued parameter using ADO.Net](https://stackoverflow.com/questions/10409576/pass-table-valued-parameter-using-ado-net) – Crowcoder Feb 09 '18 at 13:55
  • @Crowcoder this is a part of bigger app, I just try to use what I have. Accordig to duplicate possibility, there is example of using SqlConnection and i need odbc – Neska Feb 09 '18 at 14:01
  • Unless you are using ODBC because you think you might change the database in the future you can mix ODBC and SQL Client in your code base. But, it might just work if you use the duplicate link code and simply change all the SQL prefixes to ODBC. – Crowcoder Feb 09 '18 at 14:04
  • Thanks. So as I understand, using ODBC it is not possible? – Neska Feb 09 '18 at 14:20
  • I think it is supported [based on this](https://learn.microsoft.com/en-us/sql/relational-databases/native-client-odbc-table-valued-parameters/table-valued-parameters-odbc) but I have never done it with ODBC. Just change the "SQL" prefixes to "ODBC" in the code sample and try it. – Crowcoder Feb 09 '18 at 14:21
  • Unfortunately is not so simple. "ODBC" version don't have Structured type. – Neska Feb 09 '18 at 14:53
  • Bummer. Can you really not use the SqlClient provider? The other work arounds are messy. – Crowcoder Feb 09 '18 at 14:57

0 Answers0