Question SQL CONNECTION.. login failed for user

Haseo

Member
Joined
Jul 16, 2012
Messages
16
Programming Experience
Beginner
any one can help me for this..

my SQL connection code...
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=database; Integrated Security=SSPI")

the problem....

cannnot open database"database" requested by login failed.
login failed for user 'Mis_3\Mis_3'

name of my database = database.mdf
SQL server name = SQLEXPRESS



and i try this kind of connection..


Dim con As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=database; User Id=Mis_3"

the error is = login faild for user 'Mis_3' the user is not associated with trusted sql server connection..


please correct my connection or give me some sample code...
 
I used the Database explorer to add a connection to my database, then stole (i.e. copied) the connection string from there.

I have removed some of the fields and replaced the 'Attachdbfilename="database-filepath"' parm with 'database=database' and found I had to leave an additional parm of 'User Instance=True"

don't know why yet as I have only started using VB Express yesterday and SQL Server express this morning so I'm still playing...

I have not detached the database yet and that may yet break the connection I seem to have successfully made to my data...
 
You can try my own way of building sql connection


dim con as sqlconnection = new sqlconnection(Data Source=EXAMPLE-PC;Initial Catalog=dbdatabase;Integrated Security=True;)

And please include to add this line above - Imports System.Data.SqlClient
 
i do it like this if my app is a network app
VB.NET:
Data Source=[B]COMPUTER NAME[/B]\SQLExpress;Initial Catalog=[B]DBNAME[/B];Integrated Security=True
Change the bold texts with appropriate values.
 
To elaborate on and extend some of the answers already provided, you first need to specify the appropriate Data Source. If you want to connect to an instance on the local machine then you can use "." or "(local)" as the machine name, so that you don't have to change it if you move the app from machine to machine. If you want to connect to an instance on another machine then you have to specify the name of the machine or its network address.
Data Source =. 'The default instance on the local machine
Data Source =(local) 'The default instance on the local machine
Data Source =SomeMachine 'The default instance on the machine named SomeMachine
Data Source =.\SQLExpress 'The instance named SQLExpress on the local machine
Data Source =SomeMachine\SQLExpress 'The instance named SQLExpress on the machine named SomeMachine
You use Initial Catalog to specify the name of a database that is permanently attached to the instance you're connecting to. You use AttachDbFilename when you deploy an MDF file with your app and you want to attach it to the local SQL Server Express instance on demand and detach afterwards. The User Instance property is only applicable when using AttachDbFilename to create a separate temporary instance specifically for your app, to isolate your database.

If you want to connect using your Windows credentials then you use Integrated Security, but your current Windows user has to be recognised by the instance you're connecting to. If you're connecting to an instance on another machine and your account isn't recognised then it will fail. To connect using a SQL Server login rather than your Windows user then you must provide both a User Id and a Password that match a login known by that instance.

The instance that you're connecting to must also have been configured to accept remote connections, which SQL Server Express is not by default. That can be changed by ticking a check box in the instance properties in Management Studio.
 
Back
Top