Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to be able to attach the db using a relative path. Meaning that instead of using 'C:\Release\App_Data\TRTF_TagLogging.mdf', I should use '.\App_Data\TRTF_TagLogging.mdf'. Using '.\App_Data\TRTF_TagLogging.mdf', I get 'System.Data.SqlClient.SqlException (0x80131904): A file activation error occurred. The physical file name '.\App_Data\TRTF_TagLogging.mdf' may be incorrect.'
How could I do this?

What I have tried:

I am able to attach a mssql database on button click using this code:
using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=;Integrated Security=True; MultipleActiveResultSets = true")) 
            {
                using (SqlCommand cmd = new SqlCommand("", connection))
                {
                    using (SqlCommand cmd1 = new SqlCommand("", connection))
                    {
                        try
                        {
                            cmd.CommandText = "CREATE DATABASE [TRTF_TagLogging] ON (FILENAME ='C:\\Release\\App_Data\\TRTF_TagLogging.mdf'),(FILENAME ='C:\\Release\\App_Data\\App_Data\\TRTF_TagLogging_log.ldf') FOR ATTACH";

                            cmd1.CommandText = "if(exists(select * from sys.databases where name = 'TRTF_TagLogging')) PRINT 'DB attached'";

                            connection.Open();
                            cmd.ExecuteNonQuery();
                            cmd1.ExecuteNonQuery();
                            //the rest of the code
Posted
Updated 2-Apr-20 0:09am

The problem is that a relative path is just that: relative.
It's relative to the process that is trying to access the path - which in the case of a server based database engine such as SQL Server or MySql is not the process running your code.
So the path may be correct from the point of view of your software, but is unlikely to be correct from the POV of the server.
For this reason - among others - don't try to use a relative path. And definitely don't try to store databases in the same folder as your app, or a subfolder of that folder. It may work in dev, but in production it will fail mightily. There is also the fun and games that in production the server and client software are most likely to be on different machines, so the DB system almost certainly won't have any access to a local relative path at all!

You also shouldn't be use fixed connection strings, or trying to attach a DB in production code: the DB should be under the full control of the DB Server at all times or you will end up with multiple copies used by multiple users and teh whoel thing falls apart.
 
Share this answer
 
I managed to solve it like this:

string path = Directory.GetCurrentDirectory();
cmd.CommandText = @"CREATE DATABASE [TRTF_TagLogging] ON (FILENAME ='" + path + @"\App_Data\TRTF_TagLogging.mdf'),(FILENAME ='" + path + @"\App_Data\TRTF_TagLogging_log.ldf') FOR ATTACH";


Directory.GetCurrentDirectory() returns C:\Release.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900