在 C# 中打开或使用 master.mdf 数据库时出现问题

发布于 2024-11-28 13:31:27 字数 1029 浏览 3 评论 0原文

我定义了 Sql_Cmd 和其他之前需要的 sql 变量 现在编写此代码:

string strConnection2 = "Data Source=.\\sqlexpress;AttachDbFilename=master.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlcon2 = new SqlConnection(strConnection2);
string sql = "select * ";
Sql_Cmd.CommandType = CommandType.Text;
Sql_Cmd.CommandText = sql;
Sql_Cmd.Connection = sqlcon2;
try
{
    sqlcon2.Open();
    Sql_Cmd.ExecuteNonQuery();
}
catch (SqlException Error_Exception)
{
    //FormError1 = new FormErrorInDataBase();
    //FormError1.Show();
}

当我想打开 sqlcon2 时,我看到此错误:

<块引用>

尝试为文件 master.mdf 附加自动命名数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。

请帮助我 - 如何打开 master 数据库,然后使用该数据库的选择查询,以及 master.mdf 的连接字符串是什么?

我可以写mater.mdf的目录,例如C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\master.mdf还是足够了我写的是AttachDbFilename=master.mdf

I define Sql_Cmd And Other sql variable that need before
and Now write this code:

string strConnection2 = "Data Source=.\\sqlexpress;AttachDbFilename=master.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlcon2 = new SqlConnection(strConnection2);
string sql = "select * ";
Sql_Cmd.CommandType = CommandType.Text;
Sql_Cmd.CommandText = sql;
Sql_Cmd.Connection = sqlcon2;
try
{
    sqlcon2.Open();
    Sql_Cmd.ExecuteNonQuery();
}
catch (SqlException Error_Exception)
{
    //FormError1 = new FormErrorInDataBase();
    //FormError1.Show();
}

When I want to open sqlcon2 I see this error:

An attempt to attach an auto-named database for file master.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Please help me - how can I open master database and then use select query for this database, and what is the connection string for master.mdf?

Can I write the directory of mater.mdf such as C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\master.mdf or is it enough that I write AttachDbFilename=master.mdf?

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

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

发布评论

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

评论(3

羁客 2024-12-05 13:31:27

您是否正在尝试打开“主”数据库?名为“master”的系统数据库由 SQL Server 内部使用,不应该/不需要以这种方式打开或附加。如果没有,您必须将数据库命名为“master”以外的名称 - 这是保留的数据库名称。

Are you trying to open 'the' master database? The system database called 'master' is used internally by SQL Server and should not / does not need to be opened or attached in this way. If not, you'll have to call your database something other than 'master' - that is a reserved database name.

淡水深流 2024-12-05 13:31:27

再次检查您的参数。添加 .mdf 文件的正确语法如下。连接到本地 SQL Server Express 实例时附加数据库文件:

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

为什么需要 Database 参数?如果指定的数据库已附加,SQL Server 不会重新附加它。它使用附加数据库作为连接的默认值。

请参阅:connectionstrings.com

Check your parameters again. The right syntax to add an .mdf-file is as follows. Attach a database file on connect to a local SQL Server Express instance:

Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

See: connectionstrings.com

提赋 2024-12-05 13:31:27

master数据库是系统数据库,包含SQL Server实例的系统信息、该实例包含的数据库信息等。 SQL Server 启动时会自动打开该数据库。
因此,当您要连接到 SQL Server 时,master 数据库已经存在并且无法附加。有关此数据库的信息,您可以在此处查看
我不明白为什么你需要附加这个数据库。也许您不需要附加此数据库,而只需连接到它?如果是这样,您需要更改代码:

string strConnection2 = "Data Source=.\\sqlexpress;initial catalog=master;Integrated Security=True;Connect Timeout=30;"

master database is the system database that contains system information about SQL Server instance, information about databases contained in this instance and so on. This database is opened automatically when SQL Server starts.
So, when you want to connect to SQL Server, master database is already exist and cannot be attached. Information about this database you can see here
I cannot understand why you need to attach this database. Maybe you don't need to attach this database, but only connect to it? If so, you need to change th code:

string strConnection2 = "Data Source=.\\sqlexpress;initial catalog=master;Integrated Security=True;Connect Timeout=30;"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文