mdf 文件在 App_data 文件夹下正常工作,但附加到 sql server 后出现以下错误

发布于 2024-08-12 00:38:52 字数 872 浏览 3 评论 0原文

mdf 文件在 App_data 文件夹下正常工作,但将其附加到 sql server 后,在运行 asp.net 页面时出现以下错误。

无法打开用户默认数据库。登录失败。 用户“Domain\myUserName”登录失败。

[编辑] 更多信息; SQL 数据源和连接字符串。

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感悟人生的甜 2024-08-19 00:38:52

您可以确保指定了正确的 sql 用户名和密码,或者可以授予“Domain\myUserName”对数据库的访问权限。
查看下面的连接字符串。

连接字符串示例

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

温柔女人霸气范 2024-08-19 00:38:52

我不确定 MDF 文件如何“正常工作”,如果它只是位于文件夹中且未附加到 SQL Server。 “正确工作”是什么意思?

无论如何,什么版本的 SQL Server?您可以使用 sp_defaultdb 将该登录的默认数据库设置为您知道的另一个数据库,例如:

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

TechNet: sp_defaultdb

如果您使用的是 SQL Server 2005 或 2008,则应使用 ALTER LOGIN,例如

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

TechNet:更改登录

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

铜锣湾横着走 2024-08-19 00:38:52

如果您使用 Visual Studio 并且已通过这种方式创建了本地数据库,并且想知道“正确”连接字符串是什么:

1) 打开服务器资源管理器

2) 在“数据连接”下选择您的数据库并选择“属性”

3)查看“连接字符串”属性。

这就是你必须使用的。

在您的情况下,它可能不正确,因为您没有指定“数据源”参数。以下是有效连接字符串的示例:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文