문제

I'm trying to pass a parameter to a sql query (in c#) but the parameter is inside a text block:

SqlCommand cmd = new SqlCommand(@"EXEC sp_helptext N'dbo.@spname';");
cmd.Parameters.AddWithValue("@spname", "StoredProcedureName");

If I use the stored procedure name directly on query it works fine, but if I pass the parameter it does not work.

Does anyone know how to do this correcty ?

도움이 되었습니까?

해결책

You could do

SqlCommand cmd = new SqlCommand("EXEC sp_helptext @spname");
cmd.Parameters.AddWithValue("@spname", "dbo.StoredProcedureName");

But using

SqlCommand cmd = new SqlCommand("sp_helptext");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "dbo.StoredProcedureName");

is a better choice

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top