0

mdf file is working correctly under App_data folder but after attaching it to sql server give following error when running asp.net page.

Cannot open user default database. Login failed. Login failed for user 'Domain\myUserName'.

[edit] More information; SQL data source and connection string.

<asp:SqlDataSource
        id="srcFiles"
        ConnectionString="Server=.\SQLExpress;Integrated Security=True;
            AttachDbFileName=|DataDirectory|FilesDB.mdf;User Instance=True"
        SelectCommand="SELECT Id,FileName FROM Files"
        InsertCommand="INSERT Files (FileName,FileBytes) VALUES (@FileName,@FileBytes)"
        Runat="server">
        <InsertParameters>
            <asp:ControlParameter Name="FileName" ControlID="upFile" PropertyName="FileName" />
            <asp:ControlParameter Name="FileBytes" ControlID="upFile" PropertyName="FileBytes" />
        </InsertParameters>
    </asp:SqlDataSource>
Novice Developer
  • 4,489
  • 11
  • 39
  • 42

3 Answers3

0

You can make sure that you are specify the correct sql username and password or you can give 'Domain\myUserName' access to your database. check out the connection strings below.

Connection string samples

ps.
  • 4,230
  • 5
  • 31
  • 39
  • you are specify = you specify – ps. Nov 19 '09 at 16:03
  • EXEC sp_grantlogin 'Domain\myUserName' USE YOURDBNAME EXEC sp_grantdbaccess 'Domain\myUserName' you should check out the following link http://forums.asp.net/t/1296417.aspx – ps. Nov 19 '09 at 20:06
0

I'm not sure how an MDF file is "working correctly" if it is just sitting in a folder and not attached to SQL Server. What do you mean by "working correctly"?

Anyway, what version of SQL Server? You can use sp_defaultdb to set the default database for that login to another database that you know is there, e.g.:

EXEC sp_defaultdb @loginame = 'Domain\myUserName', @defdb = 'tempdb';

TechNet: sp_defaultdb

If you are using SQL Server 2005 or 2008, you should use ALTER LOGIN instead, e.g.

ALTER LOGIN [Domain\MyUserName] SET DEFAULT_DATABASE = tempdb;

TechNet : ALTER LOGIN

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
0

If you are using visual studio and you have created a local database that way, and want to know what the 'correct' connection string is:

1) Open the Server Explorer

2) Under 'data connections' select your database and choose 'properties'

3) Look at the 'connection string' property.

That is what you must use.

In your case, it is possibly incorrect because you have not specified a 'Data Source' param. Here is an example of a valid connection string:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True

Doug
  • 32,844
  • 38
  • 166
  • 222