无法连接到 SQL Server 数据库文件。我究竟做错了什么?

串行连接器zrcool

所以我有一个小的(有效的).MDF数据库文件(SQL Server)。但是,当我尝试访问它(此处为第 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);
        }
    }
}

但是它会抛出异常。具体来说它是一个

错误 26 - 定位指定的服务器/实例时出错

与 SQL Server 建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:SQL 网络接口,错误:26 - 指定的定位服务器/实例时出错)

.MDF文件有效,据我所知,其他所有内容似乎都已检查完毕。我该如何解决?

佛法

在您的代码中,问题是连接字符串。

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

如果您使用附加的 .mdf 文件,这是当前的连接字符串。如果您连接到 sql server 那么连接字符串是

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

希望能帮到你

谢谢

更新:-

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);
            }
        }

我已经更新了你的代码。

您必须只选择 MDF 文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用JDBC连接到Sql Server数据库

PyOdbc无法连接到SQL Server实例

使用Tedious和Node JS无法连接到本地SQL Server数据库

无法使用Management Studio连接到RDS SQL Server数据库

无法连接到SQL Server:SQL Server实例故障。(系统数据)

无法连接到我的SQL Server Management Studio中的arvixe数据库

Powershell无法连接到SQL Server

Django:无法连接到Microsoft SQL Server

无法使用JDBC连接到SQL Server

SQL Server 2016数据库邮件将无法连接到邮件服务器

创建数据库后无法连接到Microsoft SQL Server数据库(“系统找不到指定的文件”)

如何使用SQL Server 2014成功连接到Foxpro数据库文件?

连接到SQL Server数据库

Azure上的.NET Core无法连接到SQL Server数据库

无法通过Docker连接到SQL Server数据库

无法连接到SQL Server:无法检索此请求的数据

连接到VPN时无法连接到本地Sql Server

SQL Server 2008 R2无法连接到Management Studio中的本地数据库

SQL Server 2008无法连接到实例

我无法使用VB.NET连接到我的SQL Server数据库

无法连接到我的PC中的SQL Server实例

无法将 Visual Studio 连接到 SQL Server 数据库

无法在端口 1433 上连接到 SQL Server 数据库服务器

无法从 RHEL 7 上的 .Net 核心应用程序连接到 SQL Server 数据库

无法使用 codeigniter 连接到 SQL SERVER 数据库

无法连接到 SQL Server

ASP.NET MVC 无法连接到我的本地 SQL Server 数据库

无法连接到 SQL Server

由于登录失败错误,无法连接到 SQL Server 数据库