Attempting to grant my own permissions of MDF database file

John 466 Reputation points
2023-09-19T01:08:43.94+00:00

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.
13,656 questions
C#
C#
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.
10,832 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,321 Reputation points Microsoft Vendor
    2023-09-19T03:35:39.0166667+00:00

    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 ado.net 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.
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2}",
                            reader[0], reader[1], reader[2]);
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadLine();
            }
        }
    

    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,

    Jiale


    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 (SqlWork.com) 64,001 Reputation points
    2023-09-19T15:43:54.3033333+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.