在没有用户包装器proc的情况下从客户端应用程序执行系统存储过程(sys.sp_helptext)

时间:2012-08-18 07:32:47

标签: c# sql-server-2008

我想创建一个接受一个参数的C#方法,即存储过程的名称。然后,该方法将直接执行以下系统存储过程,而无需使用用户定义的子例程来包装此调用。

sys.sp_helptext 'proc name'

我收到错误:

  

无法找到存储过程'sys.sp_help_text

这是权限问题(我是我的测试数据库的管理员)还是资格问题?

public static string GetStoredProcedure(string objectName, string connectionString)
{

    using (SqlConnection sqlConnection = new SqlConnection())
    {
        sqlConnection.ConnectionString = connectionString;
        sqlConnection.Open();
        SqlCommand sqlCommand = new SqlCommand("sys.sp_help_text", sqlConnection);
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.Parameters.AddWithValue("@objname", objectName);
        DataSet ds = new DataSet();
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
        sqlDataAdapter.SelectCommand = sqlCommand;
        sqlDataAdapter.Fill(ds);
        return DataTableToString(ds.Tables[0]);;   
    }
}     

2 个答案:

答案 0 :(得分:2)

没问题,只是命名错误

SqlCommand sqlCommand = new SqlCommand("sys.sp_helptext", sqlConnection);

而不是

SqlCommand sqlCommand = new SqlCommand("sys.sp_help_text", sqlConnection);

答案 1 :(得分:0)

您可能必须使用具有更多权限的登录名连接到数据库。尝试使用您的应用程序使用的同一帐户登录后,从managment studio执行此sp。