在SqlCommand中调用函数

F11*_*F11 5 c# sql t-sql sql-server asp.net

public void CreateMySqlCommand() 
 {
    SqlCommand myCommand = new SqlCommand();
    myCommand.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
    myCommand.CommandTimeout = 15;
    myCommand.CommandType = CommandType.Text;
 }
Run Code Online (Sandbox Code Playgroud)

我可以在myCommand.CommandText中使用Sql Server函数吗?为什么?

Mah*_*mal 14

如果您的意思是,SQL Server用户定义的函数.然后,是的 ; 你可以正常使用它:

myCommand.CommandText = "SELECT fn_Yourfunctionname(@parameternames)";
myCommand.CommandType = CommandType.Text;
myCommand.Parameters.Add(new SqlParameter("@parameternames", ...
Run Code Online (Sandbox Code Playgroud)

它工作的原因是因为这是在SQL Server中直接调用函数的方式.