TTT
TTT

Reputation: 23

Connecting installed C# application to local SQL Server 2005 Express - User not associated with trusted sql connection

(I know there's a ton of questions like this on the web and I've spent several days now trying to solve this, but I'm not getting anywhere. I'm new to this so bear with me please)

1. The problem

After installing my application, SQL Server 2005 Express on my friends computer (Vista Home Premium) and attaching the database (mdf file) to the new SQL server whenever I launch my application or open a new form (C# windows form) inside the application I get the following message:

System.Data.SqlClient.SqlException: Login failed for user ''. The user is not associated with a trusted SQL Server connection.

If I click "continue" I can read/write to the database and the application works as expected (untill I open a new form and i get the same message again).

2. So this is what I've done so far:

I've turned "SQL Server and Windows Authentication mode" on since I've seen that being suggested in many threads, but it doesn't help.

My current connection string:

"Data Source=.\SQLEXPRESS;Initial Catalog=pj;Integrated Security=SSPI;"

Since my connection string contains ".\SQLEXPRESS" it will try to connect with "computer-name\username", in my case "TONY-COMPUTER\TONY", so I added a SQL user with that name together with role memberships db_datareader and db_datawriter to be able to read/write to DB.

I also tried with a connection string like:

"Data Source=.\SQLEXPRESS;Initial Catalog=pj;User Id=Tony;Password=123;"

Then added the SQL user "Tony".

But I keep getting the same error. What puzzles me is that there's no user name in the error message, I'd expect to see "TONY-COMPUTER\TONY" or "Tony" but it's blank.

Any help much appreciated!

/Tony

Upvotes: 2

Views: 629

Answers (2)

sgmoore
sgmoore

Reputation: 16067

If you are getting a Sql authentication error and yet your program is still able to access your database, then that would suggest you are attempting to access the database two different ways.

Are you sure you don't have some unnecessary code that attempts to access the database possibly using some hard-coded connection string?

This can sometimes happen if you have defined a connection using the Visual Studio designer which the settings may be stored in the designer.cs file but could be then overwritten at runtime using the proper connection string. On your development machine the hard-coded connection string may work and hence this not be noticed.

This would also explain why the error does not bear any relation to the actual connection string.

Upvotes: 1

Jorne De Blaere
Jorne De Blaere

Reputation: 39

Do you program in visual studio? if yes, have you tried to add a dataconnection in server explorer? if you have than you can right click on your connection, select properties and there you can select the connection string. just change the username en password.

what also can help is this

Upvotes: 0

Related Questions