Question Login failed for user - Windows Authentication

gs99

Active member
Joined
May 8, 2010
Messages
42
Programming Experience
Beginner
I have SQL Server 2008 R2
When starting it, I have the "Connect to Server" dialog that shows Authentication: Windows Authentication
There is a database called testDB, that came with SQL.

In VB 2008 Express, I have a simple app including this code:

VB.NET:
    Dim objConnection As New SqlConnection("server=localhost;database=testDB;Trusted_Connection=True;")

    objConnection.Open()
But when I run it, this error message appears on the Open() line:
Cannot open database "testDB" requested by the login. The login failed. Login failed for user 'my User name'.

Any clues about the problem?
 
I think I've resolved this problem.
These are the steps I did, using database "george" with table "Names"

1. In SQL Studio: right click "george", Tasks, Detach, OK.
2. Cut from folder MSSQL\DATA: george.mdf and george_log.ldf
3. Paste to application folder (in my case: Visual Basic bin\debug)
(In this way the |DataDirectory| in ConnectionString will find these files.)
4. Modify the ConnectionString statement shown below.

When app is run, it displays a DataGridView with contents of table Names.

VB.NET:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Dim objConnection As New SqlConnection( _
    "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\george.mdf;Integrated Security=True;User Instance=true;")
    Dim objDataAdapter As New SqlDataAdapter()
    Dim objDataSet As New DataSet

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Set the SelectCommand properties...
        objDataAdapter.SelectCommand = New SqlCommand
        objDataAdapter.SelectCommand.Connection = objConnection
        objDataAdapter.SelectCommand.CommandText = "SELECT FirstName, LastName " & _
        "FROM Names " & _
        "ORDER BY FirstName"
        objDataAdapter.SelectCommand.CommandType = CommandType.Text

        'open the connection...
        objConnection.Open()

        'Fill the dataset object with data...
        objDataAdapter.Fill(objDataSet, "Names")

        'Close the connection...
        objConnection.Close()

        'Set the DataGridView properties to bind it to our data...
        grdCustomers.AutoGenerateColumns = True
        grdCustomers.DataSource = objDataSet
        grdCustomers.DataMember = "Names"

        'Clean up
        objDataAdapter = Nothing
        objConnection = Nothing

    End Sub
End Class
Thanks to book "Beginning Microsoft Visual Basic 2008" by Thearon Willis and Bryan Newsome,
and many forum posts about "user instance" etc..

Note: If database needs to be modifed,
1. SQL Studio: Right click Databases, Attach. Add, browse to Visual Basic bin\debug folder, select george.mdf, OK.
Database now appears in Studio list with its complete path.
2. Make changes.
3. Detach it again.

This seems to work.
 

Latest posts

Back
Top