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



我想创建一个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]);;   
    }
}     

没有问题,只是命名错误的

尝试

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

而不是

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

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

最新更新