4

I have a SQL Database MDF file , I want to connect to that file with ADOConnection and SQL Client 11.0 as provider

ADOConnection connects successfully but there is no Table !

ADOConnection.GetTableNames() returns ""

My code is like this :

 ADOConnection1.ConnectionString := 'Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Data Source=(localdb)\v11.0;Initial File Name="";Server SPN="";'
               +'AttachDbFileName="G:\Projects\Delphi\Pasargad Insurance\DB\Main.mdf";';
 ADOConnection1.Connected := True;

 ADOConnection1.GetTableNames(REdit.Lines);

When I connect to Database with SQL Server Managment Studio, tables are exists

Edit :

I removed "Initial File Name" and now the ADOConnection failed to connect :

An attempt to attach an auto-named database for file "G:\Projects\Delphi\Pasargad Insurance\DB\Main2.mdf" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share

I switched tracing of LocalDB to off, copied my database file and deleted main database with SQLServer Managment Studio , and tried again to connect but the same error shown.

J...
  • 30,968
  • 6
  • 66
  • 143
Mahmoud_Mehri
  • 1,643
  • 2
  • 20
  • 38
  • 1
    For start I would try to remove `Initial File Name` from the connection string as it seems to be mutually exlusive with `AttachDbFileName`. – TLama Jul 02 '15 at 06:45
  • 1
    I removed Initial file name and now ADOConnection fails ! , it seems the adoconnection not connected to my file , it was connected to initial file that when i removed this option , it fails to connect ! , I will edit my question with more details ... – Mahmoud_Mehri Jul 02 '15 at 07:00
  • Try to add `User Instance=True;` in the connection string. – Val Marinov Jul 02 '15 at 09:04
  • Can you connect the database add designtime? If yes then copy the connectionstring of the TadoConnection into the code. When I connect thesql server I don't use the attachdbfilename. – Ravaut123 Jul 02 '15 at 10:05
  • `...or it is located on UNC share.` So... is it? Is your `G:` drive a network share? If it is, use the full UNC path instead. – J... Jul 02 '15 at 10:25
  • 1
    I've converted your error message image to text - this is helpful for a number of reasons (searchability being among the most important). You can always copy the text of a message dialog by pressing `ctrl+c`. – J... Jul 02 '15 at 10:35

0 Answers0