Connecting to SQL Server LocalDB in Delphi - sql-server

I have a .mdf database file, I want to connect to this file with ADOConnection and SQL Server LocalDB as provider
My connection string looks like this :
Data Source=(localdb)\v11.0;Integrated Security=SSPI;AttachDbFileName="MyMDFFileAddress";
But when I try to connect, this error is shown:
An attempt to attach an auto-named database for file "MDF File"
failed. A database with the same name exists, or specified file
I have tried many ways, but always the error above is shown!
I have Installed SQLLocalDB and SQL Server Native Client 11.0
On my machine I can connect to my own created instance on localDB and my database, but when I want to connect this file in another machine and use default instance and AttachDbFileName, that error is shown
I copied the .mdf file to default instance folder of LocalDB and tried to connect, but the same error is shown
I searched a lot but found no correct answer !
I'm using Delphi XE 6

Did you try this?
Data Source=(localdb)\v11.0;Integrated Security=True;AttachDbFileName=|DataDirectory|\"MyMDFFileAddress.mdf";Initial Catalog=YourDataBaseName;providerName="System.Data.SqlClient"
Is there DataBase instance (v11.0) in another machine?
Open command prompt and type to verify:
sqllocaldb info

Related

Cannot Connect To SQL-INSTANCE by Object Explorer: Invalid column name 'mastervalue'

I Want to connect to new sql server 2019 installation (Developer edition, SSMS.exe v18.10), but i am receiving error below:
Invalid column name 'mastervalue'.
Could not use view or function 'sys.configurations' because of binding errors.
Invalid column name 'mastervalue'.
Could not use view or function 'sys.configurations' because of binding errors. (Microsoft SQL Server, Error: 207)
when I open New Query, I successfully connect to Instance, but when i connecting to server by object explorer, receiving this error
What is the database setup? perhaps its a case sensitive issue, based on the error you're getting, (Microsoft SQL Server, Error: 207), maybe you server collation is not normal?
my issue SOLVED!
My OS is windows 10 Enterprise and my SQL is on Windows (Natively), but the problem was solved this way:
Rename ComputerName
Restart... but sql not run at all and in logs writen... File 'MSDBLog' does not exist.
run sql by -T902 flag and start service and modify msdb filename from MSDBLog-1 to MSDBLog
drop -T902 flag and restart service and problem solved.
Thanks

Cannot open database "DatabaseName" requested by the login. The Login failed. Login failed for user

Here is the scenario.I'm creating a desktop application with VB.NET (Windows Forms), and using SQL Server 2008 R2 for its database.
I have two computers (PC1 and PC2) and I want to connect PC2 to PC1.
PC 1 has the SQL Server database. What I do is I just copied .exe application in
the debug folder of my vb project and create a notepad for its connection and put them into one folder and transferred it to PC2.
When I run my application to other PC, I get an error
Unhandled exception has occurred in your application. If you click
Continue, the application will ignore this error and attempt to
continue, If you click quit, The application will close immediately.
Cannot open database "DatabaseName" requested by the login. The Login
failed. Login failed for user
My code inside the notepad
Data Source=.;Initial Catalog=DBSAS;Integrated Security=True
What I have done so far is to enable remote connection of SQL Server 2008 R2.
Can someone please give me an advice about this? I am really confused. Thanks
Hi please find sql server instance name at PC 1 using below command
OSQL -L
replace . to the instance name at you connection string.
Specify Data Source= "sql server instance name " in the connection string.
more detail are posted on below link.
How can I determine installed SQL Server instances and their versions?.
On the second pc create a txt file on desktop. Something like test.txt. Then rename it to test.udl Universal Data Link. Double click the test.udl. From there you can produce a valid connection string to the SQL Server. After you are done open the udl file with notepad to get the connection string. Hope this helps.
Specify Data Source=PC1 in the connection string. The . indicates a local SQL Server instance running on the same machine as the application. The error suggests SQL Server is installed on PC2 but doesn't contain the desired database.
EDIT:
The error
Cannot open database "DatabaseName" requested by the login
indicates authentication was successful to the SQL Server but the database context could not be set. If DatabaseName in the error message is the same one as the Initial Catalog in the connection string, then either the database does not exist or the user does not have permissions to use it. However, if the DatabaseName in the error message differs from the connection string, the connection string you changed is not the same one that is actually being used by the application.

SQL Server Express 2012 Error: Cannot connect to Local DB

I have installed Sql Express 2012 version. I installed Local Db separately. i can see its installed when I click on "Installed Sql Server discovery report". However, I cannot connect to it using Sql Server Management Studio or Microsoft Visual Studio 2012 Professional Edition. I get the error:
"The attempt to attach to the database failed with the following
information: A network-related or instance-specific error occurred
while establishing a connection to SQL Server. The server was not
found or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 50 - Local Database Runtime
error occurred. Cannot create an automatic instance. See the Windows
Application event log for error details."
In Windows Event viewer I see several of following errors with different regsistry keys each time:
"The DataDirectory registry value is missing in the LocalDb instance registry key:{-----}"
I checked in regsitry values for local db in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MicrosoftSQL Server Local DB and there's only one entry "Installed Version".
I have gone through several posts which state solution for the above error and have done almost everything they have suggested but still can't get localDb to work.
I'd appreciate any help that one can provide in order to solve this issue.
UPDATE:
Using Command Line Interface I verified that localDb v11.0 is installed. However, the command sqlLocalDb info v11.0 gives error:"The automatic instance "v11.0" is not created. If I try to create one it gives another error:"Unexpected error occured inside a localDb instance API method call."
You may try to use SQL Server Authentication. Also make sure that your SQL service is running.
Also you may try to delete the following registry:
HKEY_USERS\.DEFAULT\Software\Microsoft\Microsoft SQL Server\UserInstances\[GUID_FROM_EVENT_LOG]
You may also check Getting Started with SQL Server 2012 Express LocalDB
You need to start the SQL service for the local system.
Start Button
Write "SQL Server Configuration Manager" and click on it
After opening "SQL Server Configuration Manager" you need to start the local system services which are may be stopped in it that why your local Database not connected.
After starting local services you just connect the local system database. It should connected successfully.

Importing a .cvs file into a detached .mdf?

I was asked to repost this question, due to the answers were not going the right directions.
I have a detached database similar to to Northwind.MDF I want to import a .CVS file into this MDF file. I have the mdf file as part of my Solution Explorer in vs2010 and I can look work with the data, just no way to import. In vs2010 I can use the Server Explorer and connect with no problems. the connection string looks like this:
Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ASPX\sdb1\customer.mdf;Integrated Security=True;User Instance=True
When I try to use any tool outside of vs2010 I cannot connect. Non of the sqlserver tools connect and give me an access denied, even when I try to attach the file from within Management Studio.
TITLE: Microsoft SQL Server Management Studio
Attach database failed for Server 'STEPHANS-PC\SQLEXPRESS'. (Microsoft.SqlServer.Smo)
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
Unable to open the physical file "D:\ASPX\sdb1\customer.mdf". Operating system error 5: "5(Access is denied.)". (Microsoft SQL Server, Error: 5120)
This should not be this tuff to import data from a CSV.
You have to connect to your user instance database using an special connection string.
An user instance database is a sql express database based upon an mdf file (just what you have).
This link explains how to connect from SSMS to your User Instance Database:
How to: Connect to a User Instance

Can't connect to my own MDF file. Cannot open user default database. Login failed.Login failed for user... and other errors

SQLSEXPRESS service IS running!!
I have I program I wrote some time ago and it works. It was written in MS Visual Studio with local MDF file.
How is it possible to edit this file from Microsoft SQL Server Management Studio? I don't see this file in the list of globally connected databases.
If I try to attach this file with Visual Studio Wizard, I get message that such database already exists, but when I set arbitrary logical name, I get sharing violation.
How to reach MDF file?
EDIT 1
Now I found that my program also stopped to work.
The summary of results of different attempts to reach MDF file:
Program itself System.Data.SqlClient.SqlException (0x80131904): Cannot open user default database. Login failed. Login failed for user <username>. (windows user)
Visual Studio, Data source = Microsoft SQL Server, Logical Name = "" Error message: An attempt to attach an auto-named database for file
<filename> failed. A database with the same name exists, or
specified file cannot be opened, or it is located on UNC share.
Visual Studio, Data source = Microsoft SQL Server, Logical Name = SOMENAME Error message Unable to open the physical file <filename>. Operating system error 32: "32(The process cannot access the file because it is being used by another process )".
Cannot attach the file <filename> as database ‘SOMENAME’.
Visual Studio, Data source = Microsoft SQL Server Database File, Windows Authentication Error message: Cannot open user default database. Login failed. Login failed for user <username>.
Visual Studio, Data source = Microsoft SQL Server Database File, SQL Server Authentication Error message: Failed to generate a user instance of SQL Server. Only an integrated connection can generate a user instance. The connection will be closed.
Microsoft SQL Server Management Studio, Attach Database Error message: Unable to open the physical file <filename>. Operating system error 32: "32(The process cannot access the file because it is being used by another process)". (Microsoft SQL Server, Error: 5120)
MDF files are not intended to be used directly.
You may use them only through and with the help of SQL Server Engine.
Try this:
1- Create a database with the same name to mdf file.
2- Stop sql service
3- Go to the Data Carpet where is all mdf files and ldf , copy your original mdf file y replace.
4- Start the service
5- See in the management studio if the database still online there you go.
A .MDF database cannot be "opened".
It must be attached to an existing (and running) SQL Server instance.
If you have SQL Server Management Studio running, open a connection, and right click on databases. Choose "Attach" and point to the .MDF file. This will (attempt to) attach the database to the master database. If it works, the database will be visible in SSMS.

Resources