C# 如何在文本查询中插入sql参数

C# 如何在文本查询中插入sql参数,c#,sql,sql-server,sqlcommand,sqlparameter,C#,Sql,Sql Server,Sqlcommand,Sqlparameter,我试图将参数传递给sql查询(在c#中),但该参数位于文本块内: SqlCommand cmd = new SqlCommand(@"EXEC sp_helptext N'dbo.@spname';"); cmd.Parameters.AddWithValue("@spname", "StoredProcedureName"); 如果我在查询中直接使用存储过程名称,它可以正常工作,但如果我传递参数,它就不工作 有人知道怎么做吗?你可以 SqlCommand cmd = new SqlComma

我试图将参数传递给sql查询(在c#中),但该参数位于文本块内:

SqlCommand cmd = new SqlCommand(@"EXEC sp_helptext N'dbo.@spname';");
cmd.Parameters.AddWithValue("@spname", "StoredProcedureName");
如果我在查询中直接使用存储过程名称,它可以正常工作,但如果我传递参数,它就不工作

有人知道怎么做吗?

你可以

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

SqlCommand cmd = new SqlCommand("sp_helptext");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@objname", "dbo.StoredProcedureName");
是一个更好的选择

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

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

是一个更好的选择

我已经尝试过了,但没有效果。我需要将一个文本参数传递给sp_helptext解决方案会很好,但是,我有一个很长的查询,sp_helptext的执行处于查询的中间位置,我无法单独执行。好吧,我对我的查询做了一些更正,您的第一个答案也可以。谢谢你,我已经试过了,但不起作用。我需要将一个文本参数传递给sp_helptext解决方案会很好,但是,我有一个很长的查询,sp_helptext的执行处于查询的中间位置,我无法单独执行。好吧,我对我的查询做了一些更正,您的第一个答案也可以。谢谢你,卢斯卡