Forum Discussion

stega's avatar
stega
Contributor
13 years ago

Database query open problem

Hi,

I want to use relative path for my database file, but i got Unspecified error when the script tries to open the query.

Here is the code:



var Qry = ADO.CreateADOQuery();

Qry.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=..\\..\\..\\..\\vault.accdb;Uid=Admin;Pwd=;";

Qry.SQL = "SELECT * FROM [parameters];";

Qry.Open();



Can anybody help me?

Thanks!

9 Replies

  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 2 rankChampion Level 2
    Hi Gabor,


     



    I think that the problem is that ADO provider is called as a COM object and its working folder is not necessarily the same as the test suite's one. So I would recommend to construct the full path based on the relative one:



    var sPath = "..." + aqFileSystem.ExpandUNCPath(aqFileSystem.IncludeTrailingBackSlash(Project.Path) + "..\\..\\..\\..\\vault.accdb") + "...";



    Qry.ConnectionString = sPath;


  • Thank you the reply.

    I have found the solution, i missed the first "folder":

    Qry.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=.\\..\\..\\..\\vault.accdb;Uid=Admin;Pwd=;";
  • karkadil's avatar
    karkadil
    Valued Contributor
    Hi Gabor,



    I would suggest using Alexei's solution (use Project.Path property to get project path) because your solution (with leading .\ folder) may fail in case current folder is changed for some reason during script run.
  • Hi Alexei,



    What should i write to the "..." places? How this method works?
  • karkadil's avatar
    karkadil
    Valued Contributor
    "..." means "whatever you need", any string you want.



    The main idea of my hint is using Project.Path variable (or ProjectSuite.Path) instead of ".\" (current dir).



    If your database file is placed one level up from your ProjectSuite file, then you can build the path like this



    var dbpath = ProjectSuite.Path + "..\\vault.accdb";

    Qry.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" + dbpath + ";Uid=Admin;Pwd=;";
  • If my suite is in 2.2.1, and i want to move into 1.2.1 than how does the code should look like?



    c:\+--1

        |    +--1.1

        |    +--1.2

        |           +--1.2.1

        +--2

             +--2.1

             +--2.2

                    +--2.2.1
  • karkadil's avatar
    karkadil
    Valued Contributor
    var dbpath = ProjectSuite.Path + "..\\..\\..\\1\\1.1\\1.2.1\\vault.accdb";



    However, if your database is placed somewhere (e.g. c:\\database\\vault.accdb) and this path never changes (and will not change even on another PC), then I suggest hardcode the path with absolute path, not relative.
  • I've tried this, but it concatenates the "..\\..\\..\\1\\1.1\\1.2.1\\vault.accdb" after the path.

  • karkadil's avatar
    karkadil
    Valued Contributor
    That's correct, this is how it should work.

    If you want to convert relative path to an absolute one, you can use aqFileSystem.ExpandFileName or aqFileSystem.ExpandUNCFileName methods.



    But it doesn't matter, because



    C:\\Users\\karkadil\\..\\..\\

    is the same as

    C:\\

    and you can work with both of them the same way