C# 从.NET调用表值SQL函数
标量值函数可以从.NET调用,如下所示:C# 从.NET调用表值SQL函数,c#,.net,sql,C#,.net,Sql,标量值函数可以从.NET调用,如下所示: SqlCommand cmd = new SqlCommand("testFunction", sqlConn); //testFunction is scalar cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("retVal", SqlDbType.Int); cmd.Parameters["retVal"].Direction = ParameterDirect
SqlCommand cmd = new SqlCommand("testFunction", sqlConn); //testFunction is scalar
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("retVal", SqlDbType.Int);
cmd.Parameters["retVal"].Direction = ParameterDirection.ReturnValue;
cmd.ExecuteScalar();
int aFunctionResult = (int)cmd.Parameters["retVal"].Value;
我还知道表值函数可以以类似的方式调用,例如:
String query = "select * from testFunction(param1,...)"; //testFunction is table-valued
SqlCommand cmd = new SqlCommand(query, sqlConn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(tbl);
我的问题是,表值函数能否像标量值函数一样作为存储过程调用?e、 例如,使用正在调用的表值函数复制我的第一个代码段,并通过ReturnValue参数获取返回的表。否,因为您需要选择它们。但是,您可以创建一个存储的过程包装器,这可能会破坏具有表函数的功能。@ChuckConway为什么要从标题中删除C?考虑到C标签,它是有效的。。。但是这个标签并没有帮助它,它出现在谷歌上。我不认为它真的会击败拥有表值函数的意义。拥有TVF的一点是,它们具有存储过程所没有的某些安全保证。另一点是,它们可以重用,而不简单地复制和粘贴就很难重用存储过程。