在 C# 中打开或使用 master.mdf 数据库时出现问题
我定义了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正在尝试打开“主”数据库?名为“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.
再次检查您的参数。添加 .mdf 文件的正确语法如下。连接到本地 SQL Server Express 实例时附加数据库文件:
为什么需要 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:
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
master数据库是系统数据库,包含SQL Server实例的系统信息、该实例包含的数据库信息等。 SQL Server 启动时会自动打开该数据库。
因此,当您要连接到 SQL Server 时,master 数据库已经存在并且无法附加。有关此数据库的信息,您可以在此处查看
我不明白为什么你需要附加这个数据库。也许您不需要附加此数据库,而只需连接到它?如果是这样,您需要更改代码:
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: