Solved

How do I grant permission to a folder when creating a database in a new SQL Express instance?

Posted on 2014-07-27
5
842 Views
Last Modified: 2014-08-07
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\MSSQL\DATA\AddBook.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )" & _
                  "LOG ON " & _
                  "( NAME = N'AddBook_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSSQL\DATA\AddBook_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.ToString())
            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!!
0
Comment
Question by:Karen Wilson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 40
ID: 40223413
Have you tried running your code as an administrator?

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.
0
 

Assisted Solution

by:Karen Wilson
Karen Wilson earned 0 total points
ID: 40223886
I think I found the code but I haven't really tested it yet.  I was hoping to receive a similar response to compare.

 'open the file up for permssion
Dim FolderPath As String = "C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSSQL\DATA" 'Specify the folder here
Dim UserAccount As String = machine & "\" & user 'Specify the user here

            Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
            Dim FolderAcl As New DirectorySecurity

FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
            FolderAcl.SetAccessRuleProtection(True, False) 'uncomment to remove existing permissions
            FolderInfo.SetAccessControl(FolderAcl)
0
 
LVL 81

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40234731
C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSSQL\DATA\AddBook.mdf'

Create the database elsewhere but not under C:\Program Files.. You can attach db files from just about anywhere. as a workaround if you insist on not using current microsoft best practices is to shell out to takeown i.e.
takeown /f "C:\Program Files\Microsoft SQL Server\MSSQL11.DTH2012\MSSQL\DATA\ /r /d n"

Open in new window

0
 

Author Closing Comment

by:Karen Wilson
ID: 40245659
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\MSSQL\DATA" 'Specify the folder here
Dim UserAccount As String = machine & "\" & user 'Specify the user here

Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
Dim FolderAcl As New DirectorySecurity

FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
FolderAcl.SetAccessRuleProtection(True, 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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question