I have created a table-valued function because I want to return table of values to my .NET application. At the moment the way the code runs is that it returns affected rows which is expected of ExecuteNonQuery
method. I want to expecting table as return type. So not sure what to do.
SQL:
CREATE FUNCTION [EMR].[fnGetMemberOrderDailySummary]
(@TVP typtblMember READONLY)
RETURNS TABLE
AS
RETURN (
SELECT *
FROM [EMR].[tblFactMemberOrderDailySummary]
WHERE Member IN (SELECT MemberMnemonic FROM @TVP)
)
.NET code:
using (var conn = new SqlConnection(_EDWConnString))
{
table = CreateDataTable(Member);
var procName = "[EMR].[fnGetMemberOrderDailySummary]";
var procParam1Name = "@typtblMember";
var cmd = new SqlCommand(procName, conn) { CommandType = CommandType.StoredProcedure };
var dtparam = cmd.Parameters.AddWithValue(procParam1Name, Member.AsDataTable());
dtparam.SqlDbType = SqlDbType.Structured;
_logger.Log(LogSeverity.Debug, $"Beneficial owner position records insert into EDW is started");
var affectedRows = cmd.ExecuteNonQueryAsync();
var isSuccessful = affectedRows != null;
_logger.Log(LogSeverity.Debug, $"GetSampleData meeting expectations: [{isSuccessful}]");
return table;
}
2条答案
按热度按时间oug3syen1#
You don't need a table-value function if you want to get a
DataTable
from the database. Instead, you can run the SQL directly andFill()
theDataTable
with aDataAdapter
orLoad()
it from aDataReader
:If you don't want to have SQL directly in your C# code, you would do this as a stored procedure instead of table-value function:
and then:
Either way, I'd tend to write the SQL to use a
JOIN
instead ofIN()
:unftdfkk2#
You are using cmd.ExecuteNonQuery, use reader instead