Attempting to grant my own permissions of MDF database file

John 306 Reputation points

I am having too many issues with granting my own permissions while accepting MDF files. The MDF file contains the SQL file.

Should I use the SQL Assembly file references in my coding instead of the MDF file access?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
10,932 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,969 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 10,381 Reputation points Microsoft Vendor

    Hi @John , Welcome to Microsoft Q&A,

    Have you tried using Windows Authenticate directly. This article has a good demonstration on how to use VS to connect MDF files: MDF files.

    I don't understand "use the SQL Assembly file references". If you are programming locally, you can easily connect to the local sql server Connect to a database.

    You can use connection string to easily connect to sql server. SqlClient

    using System;
    using System.Data;
    using System.Data.SqlClient;
    class Program
        static void Main()
            string connectionString =
                "Data Source=(local);Initial Catalog=Northwind;"
                + "Integrated Security=true";
            // Provide the query string with a parameter placeholder.
            string queryString =
                "SELECT ProductID, UnitPrice, ProductName from dbo.products "
                    + "WHERE UnitPrice > @pricePoint "
                    + "ORDER BY UnitPrice DESC;";
            // Specify the parameter value.
            int paramValue = 5;
            // Create and open the connection in a using block. This
            // ensures that all resources will be closed and disposed
            // when the code exits.
            using (SqlConnection connection =
                new SqlConnection(connectionString))
                // Create the Command and Parameter objects.
                SqlCommand command = new SqlCommand(queryString, connection);
                command.Parameters.AddWithValue("@pricePoint", paramValue);
                // Open the connection in a try/catch block.
                // Create and execute the DataReader, writing the result
                // set to the console window.
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                            reader[0], reader[1], reader[2]);
                catch (Exception ex)

    We usually do not connect the mdf file directly with code, but connect to a SQL Server instance running the SQL Server database engine.

    Best Regards,


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce ( 44,076 Reputation points

    you appear to be trying attach a database file already owned by sqlserver. this database may have been detached. if so, you should use transact sql or the ssms tool to attach.

    the method you are using requires the user to own the database file.

    0 comments No comments