none
Relative path for sql database file connection RRS feed

  • Question

  • I use the following sql connection string to connect to my database file.

            SqlConnection bağlantı = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename='C:\\Program Files\\Microsoft SQL Server\\MSSQL13.MSSQLSERVER\\MSSQL\\DATA\\Okul.mdf';Integrated Security = True; Connect Timeout = 30");

    I want to move my database file to the folder of my exe file and want to use relative path.

    How can I change it?

    Wednesday, December 6, 2017 10:13 PM

Answers

  • Hello sqlpro9999,

    >>I want to move my database file to the folder of my exe file and want to use relative path

    You could use Fileinfo.Copyto method to copy the mdf file under debug folder. As for relative path, try to use "AttachDbFilename='|DataDirectory|\\xxx.mdf" statement. I create a simple example as below.

    static void Main(string[] args)
            {
                string sourceFile = @"C:xx\xx\xx\TT.mdf";
                FileInfo file = new FileInfo(sourceFile);
    
                if (!File.Exists(file.Name)) 
                    file.CopyTo(file.Name);    
                     
                string connectionstring = string.Format("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\\{0}';Integrated Security = True; Connect Timeout = 30",file.Name);
                SqlConnection conn = new SqlConnection(connectionstring);
     
                string sql = "select count(*) from Test";
                SqlCommand com = new SqlCommand(sql, conn);
                    conn.Open();
                    int x = (int)com.ExecuteScalar();       
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 7, 2017 4:43 AM
    • Marked as answer by sqlpro9999 Thursday, December 7, 2017 11:49 AM
    Thursday, December 7, 2017 4:40 AM
    Moderator

All replies

  • Try this

    var database = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Okul.mdf");
    var connString = $"Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename={database};Integrated Security = True; Connect Timeout = 30";


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, December 6, 2017 10:19 PM
    Moderator
  • From Visa and up, those O/S(s) are not going to allow any file that can be written to in the Program Files directory -- a protected directory.

    You make a folder away from Program Files and put you program there with MDF, or you put your program in Program Files and put your MDF file in ProgramData -- the all users folder.

    http://technet.microsoft.com/en-us/library/ff716245.aspx

      
    Wednesday, December 6, 2017 11:44 PM
  • Hello sqlpro9999,

    >>I want to move my database file to the folder of my exe file and want to use relative path

    You could use Fileinfo.Copyto method to copy the mdf file under debug folder. As for relative path, try to use "AttachDbFilename='|DataDirectory|\\xxx.mdf" statement. I create a simple example as below.

    static void Main(string[] args)
            {
                string sourceFile = @"C:xx\xx\xx\TT.mdf";
                FileInfo file = new FileInfo(sourceFile);
    
                if (!File.Exists(file.Name)) 
                    file.CopyTo(file.Name);    
                     
                string connectionstring = string.Format("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\\{0}';Integrated Security = True; Connect Timeout = 30",file.Name);
                SqlConnection conn = new SqlConnection(connectionstring);
     
                string sql = "select count(*) from Test";
                SqlCommand com = new SqlCommand(sql, conn);
                    conn.Open();
                    int x = (int)com.ExecuteScalar();       
            }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Thursday, December 7, 2017 4:43 AM
    • Marked as answer by sqlpro9999 Thursday, December 7, 2017 11:49 AM
    Thursday, December 7, 2017 4:40 AM
    Moderator