简体   繁体   English

连接到SQL Server数据库文件时遇到问题。 我究竟做错了什么?

[英]Having trouble connecting to a SQL Server database file. What am I doing wrong?

So I've got a little (and valid) .MDF database file (SQL Server). 因此,我有了一个(有效的) .MDF数据库文件(SQL Server)。 However when I try to access it (line 32 here ) or smaller context here... 但是,当我尝试访问它( 此处第32行)或此处较小的上下文时...

private void loadDataBaseButton_Click(object sender, EventArgs e)
{
    DialogResult result = openFileDialog1.ShowDialog();

    if (result == DialogResult.OK)
    {
        string databasePath = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
        //SqlConnection dataBaseConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlConnection dataBaseConnection = new SqlConnection(string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True", databasePath));

        try
        {
            dataBaseConnection.Open();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

However it throws an exception. 但是,它将引发异常。 Specifically it is an 具体来说是

Error 26 - Error Locating Server/Instance Specified 错误26-错误定位指定的服务器/实例

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - Error Locating Server/instance specified) (提供程序:SQL网络接口,错误:26-错误定位指定的服务器/实例)

The .MDF file is valid and everything else to my knowledge seems to check out. .MDF文件有效,据我所知其他所有内容似乎.MDF签出。 How do I fix this? 我该如何解决?

In your code problem is Connection string. 在您的代码问题是连接字符串。

connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20180630124430.mdf;Initial Catalog=aspnet-WebApplication1-20180630124430;Integrated Security=True"

this is currect connection string if you using to attached .mdf file. 如果使用附加的.mdf文件,则这是当前连接字符串。 if you connecte to sql server then connection string is 如果您连接到sql server,则连接字符串为

data source=DHARMESH-PC;initial catalog=AdventureWorks;user id=sa;password=sa123

Hope this will help you 希望这个能对您有所帮助

Thanks 谢谢

Update:- 更新:-

DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {
            string databasePath = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
            string dbname = openFileDialog1.FileName.Split('.')[0];
            //SqlConnection dataBaseConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True");
            string connection = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=" + databasePath + ";Initial Catalog=" + dbname + ";Integrated Security=True";
            SqlConnection dataBaseConnection = new SqlConnection(connection);

            try
            {
                dataBaseConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I have update your code . 我已经更新了您的代码。

You have to select only MDF file . 您只需选择MDF文件。

I believe if you're trying to connect to "SQLEXPRESS" that would imply that you are running SQL Server Express locally and attaching the database 我相信,如果您尝试连接到“ SQLEXPRESS”,则意味着您正在本地运行SQL Server Express并附加数据库

I've been able to successfully access localdb MDF files with a connection string like this: 我已经能够使用如下连接字符串成功访问localdb MDF文件:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDBFile.mdf;Integrated Security=True" providerName="System.Data.SqlClient"

You should also verify that the file is in the expected "DataDirectory." 您还应该验证文件是否在预期的“ DataDirectory”中。

If you are using 如果您正在使用

User Instance=true 用户实例= true

you must use named pipes so your connection will change from: 您必须使用命名管道,以便您的连接将从:

.\SQLEXPRESS

to

.\\SQLEXPRESS

The network protocol for user instances must be local Named Pipes. 用户实例的网络协议必须是本地命名管道。 A user instance cannot be started on a remote instance of SQL Server, and SQL Server logins are not allowed. 无法在SQL Server的远程实例上启动用户实例,并且不允许SQL Server登录。

sql-server-express-user-instances sql-server-express-user-instances

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM