Results 1 to 4 of 4

Thread: Unable to drop user after restore...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Unable to drop user after restore...

    I am writing a utility to my application that will allow the user to create a backup of an sql database and restore that backup on a different server. The restore option will also create the SQL login and user to that database that my software is using. This all works fine, apart from the fact that when the database is backed up is save the sql user in the backup, so when I restore the backup a user exists in the database, but no corresponding login. So what I planned on doing was dropping the user from the restored database and recreating both the login and user again.

    However when I try to drop the user from the restored database I get the following error:

    Cannot drop the login 'RISDEMO', because it does not exist or you do not have permission.

    The code I am using to drop the user the following:

    Code:
    SQLConnString = "Server='myserver';Trusted_Connection=True"
    Dim myConn As New SqlClient.SqlConnection(SQLConnString)
                Dim cmd As New SqlClient.SqlCommand("", myConn)
                Dim sb As Text.StringBuilder = Nothing
    
    Try
                    sb = New Text.StringBuilder
                    sb.Append("USE myDatabase")
                    sb.Append(ControlChars.CrLf & "GO")
                    sb.Append(ControlChars.CrLf & "DROP USER myUser")
                    sb.Append(ControlChars.CrLf & "GO")
                    myConn.Open()
                    cmd.CommandText = sb.ToString
                    cmd.ExecuteNonQuery()
                Catch ex As SqlClient.SqlException
                Finally
                    myConn.Close()
                End Try
    I understand the error, and I think it is a permissions thing. However I am using windows authentication and obviously have enough permissions to restore the database. The other thing that is strange is if I use SQL Server Management Studio still login using windows authentication I can delete the user????

    Any help would be apreshiated as I have spent hours on this.

    Thanks

    Simon

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Unable to drop user after restore...

    The alternative is to create a backup but with out users, if that is possible. I am using SMO, but I could not find anything on the internet on how to do this?

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Unable to drop user after restore...

    you say you can delete the user using management studio. i understand this as rightclick "delete" or similar. can you delete it via sql i.e. DROP USER myUser executed in myDatabase?
    ahh, and i am not sure if multiple 'go' work in one SqlCommand i vaguely remember having problems with this once <- edit: pretty sure this is the reason, try executing the use and drop sequentuially each with its own .ExecuteNonQuery
    Last edited by digitalShaman; Aug 5th, 2014 at 03:04 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Unable to drop user after restore...

    digitalShaman,

    Thanks so much, you where right I needed to execute in to separate queries.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width