mhmad
mhmad

Reputation: 43

AttachDbFilename in VB

I am made project in VB(Visual Basic 2010) so i am connection my project with data base with the following connection string in App.config :

  <connectionStrings>
  connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MY_DATABASE.mdf;Integrated Security=True; Connect Timeout=30"
  </connectionStrings>

but when i published my project by visual stdio and run the project after published to .exe the project did not run because AttachDbFilename is Error.

I am try to change the **

AttachDbFilename to c:\Folder\MY_DATABASE.mdf

**

but i received error : An attempt to attach an auto-named database for file C:\Folder\MY_DATABASE.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC shar

What i can do .

Upvotes: 0

Views: 1341

Answers (2)

Kartik Shah
Kartik Shah

Reputation: 153

Connect VB Project with Service-Base Database via this code

firstly add Service Base Database

import system.Data.SqlClient

than declare valriable for

Dim con as New SqlDataConnection
Dim cmd as new SqlCommand
Dim dr as SqlDataReader

in load event of form write code

con=New SqlConnection('Copy Path of the database');

find path of the database :

click on service base database

than in property you find the path copy that path and paste between ('')

Upvotes: 0

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

In you connection string just add the

'database=<database name> '

This prevents SQL server to create the auto-named database.

Sources

Example further detailed:

Dim myConnection As Sqlconnection = New SqlConnection()
 myConnection.ConnectionString="Server=server\serverinstance;Database=MYDbase;User Id=User1;Password=Pass1;"

Please change Server\Serverinstance to your Servers Data / mdf file

Please Change MyDbase to your database name / mdf file name

Change user and password to your username and password.

Edit 3: Try

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

And if that doesnt help here is everything on Conections

http://www.connectionstrings.com/

Upvotes: 1

Related Questions