Karen Wilson
asked on
How do I grant permission to a folder when creating a database in a new SQL Express instance?
I have performed a quiet load of SQL Express and named the instance something specific. This code works. I then try to set up a new database with the code below, and it throws an error. When I access the file through file explorer, I get the error, "You don't currently have permission to access this folder. Click continue to permanently get access to this folder." I click Continue and the folder opens. I then go back to my code below and it works.
How can I grant permission to the folder from behind the scenes in my code?
Private Sub myNewDatabase()
Try
Dim str As String
Dim srv As Server
srv = New Server(machine & "\" & Inst)
Dim myConn As SqlConnection = New SqlConnection("Server=" & machine & "\" & Inst & ";Trusted_Connection=True; ")
'============new string ========================== =======
str = "CREATE DATABASE [AddBook] " & _
"CONTAINMENT = NONE ON PRIMARY " & _
"( NAME = N'AddBook', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS QL\DATA\Ad dBook.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )" & _
"LOG ON " & _
"( NAME = N'AddBook_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS QL\DATA\Ad dBook_log. ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)"
'========================= ========== ========== ========== =
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
Try
myConn.Open()
myCommand.ExecuteNonQuery( )
Catch ex As Exception
MessageBox.Show(ex.ToStrin g())
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try
Catch ex As Exception
MessageBox.Show(ex.Message , My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Many thanks!!
How can I grant permission to the folder from behind the scenes in my code?
Private Sub myNewDatabase()
Try
Dim str As String
Dim srv As Server
srv = New Server(machine & "\" & Inst)
Dim myConn As SqlConnection = New SqlConnection("Server=" & machine & "\" & Inst & ";Trusted_Connection=True;
'============new string ==========================
str = "CREATE DATABASE [AddBook] " & _
"CONTAINMENT = NONE ON PRIMARY " & _
"( NAME = N'AddBook', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS
"LOG ON " & _
"( NAME = N'AddBook_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS
'=========================
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
Try
myConn.Open()
myCommand.ExecuteNonQuery(
Catch ex As Exception
MessageBox.Show(ex.ToStrin
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try
Catch ex As Exception
MessageBox.Show(ex.Message
End Try
End Sub
Many thanks!!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used this code and it worked:
'open the file up for permssion
Dim FolderPath As String = "C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS QL\DATA" 'Specify the folder here
Dim UserAccount As String = machine & "\" & user 'Specify the user here
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPat h)
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(Ne w FileSystemAccessRule(UserA ccount, FileSystemRights.Modify, InheritanceFlags.Container Inherit Or InheritanceFlags.ObjectInh erit, PropagationFlags.None, AccessControlType.Allow))
FolderAcl.SetAccessRulePro tection(Tr ue, False) 'uncomment to remove existing permissions
I also made the choice to use a directory outside of the program files directory and that is working, too.
'open the file up for permssion
Dim FolderPath As String = "C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSS
Dim UserAccount As String = machine & "\" & user 'Specify the user here
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPat
Dim FolderAcl As New DirectorySecurity
FolderAcl.AddAccessRule(Ne
FolderAcl.SetAccessRulePro
I also made the choice to use a directory outside of the program files directory and that is working, too.
In newer versions of Windows, even if you are logged as an administrator, applications are working by default as if you were a regular user.
You can force an application to work under your administrator account either by launching with a right click and selecting the Run As Administrator option, or by modifying the Windows Settings in the Application tab of the project's Properties window.