简体   繁体   English

从VB.NET执行索引重建

[英]Performing an Index Rebuild from VB.NET

I am trying to call the Index Rebuild stored procedure from a VB.NET console application, but I am getting the "User does not haver permission to perform this action" error. 我试图从VB.NET控制台应用程序调用“索引重建”存储过程,但出现“用户没有执行此操作的权限”错误。

I have logged onto the SQL server using the same userid as the console apps connection string, and managed to run the same stored procedure via SQL Management Studio. 我已经使用与控制台应用程序连接字符串相同的用户名登录到SQL Server,并设法通过SQL Management Studio运行相同的存储过程。

While trying to get this working, I granted the userid full server admin access, but still the error occurs, but only when calling from the console app. 在尝试使此工作正常进行时,我授予了userid完全服务器管理员访问权限,但仍然会发生错误,但仅在从控制台应用程序调用时才发生。

Code is 代码是

Private conPR As New SqlConnection(GetConnectionString.getconnectionstring("PR"))
Dim rebuildindexstr As String = "Exec sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD';"
Dim rebuildindexpro As New SqlCommand(rebuildindexstr, conPR)
conPR.Open()
rebuildindexpro.CommandTimeout = 0
rebuildindexpro.ExecuteNonQuery()
conPR.Close()

GetConnectionString just reads the required connection string from a text file Server=;Database=;uid=;Password= GetConnectionString只是从文本文件Server =; Database =; uid =; Password =中读取所需的连接字符串。

I am using the same connection string throughout the program successfully. 我在整个程序中成功使用了相同的连接字符串。

Has anyone managed to perform an index rebuild from a VB.NET console app? 是否有人设法从VB.NET控制台应用程序执行索引重建?

Try to vary this row: 尝试更改此行:

Dim builder As SqlConnectionStringBuilder = New 
    SqlConnectionStringBuilder("Integrated Security=SSPI;") 

OR 要么

Dim builder As SqlConnectionStringBuilder = New 
    SqlConnectionStringBuilder("Integrated Security=true;")

What this means: 这意味着什么:

Integrated Security = False : User ID and Password are specified in the connection. Integrated Security = False :在连接中指定了用户ID和密码。

Integrated Security = true : the current Windows account credentials are used for authentication. Integrated Security = true :当前Windows帐户凭据用于身份验证。 However, be carefully it does not work in all SQL providers. 但是,请注意,它并非在所有SQL提供程序中都起作用。 It will throw an exception with OleDb provider. OleDb提供程序将引发异常。

Integrated Security = SSPI : this is equivalent to true, but it works with SQL Client and OleDb providers. Integrated Security = SSPI :这等效于true,但是它可与SQL ClientOleDb提供程序一起使用。

Please, see connection strings at MSDN. 请在MSDN上查看连接字符串。

UPDATE: In addition, try to set to your user the following roles such as public and sysadmin : 更新:此外,尝试为您的用户设置以下角色,例如publicsysadmin

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM