使用C++和.NET Framework连接SQL Server

问题描述 投票:0回答:1

我正在 .NET Framework 中使用 C++ 语言创建一个应用程序,我尝试连接 SQL Server,但无法连接它。错误的按摩是

[System.Data.SqlClient.SqlException:“建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误”。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)']

我的这个应用程序的代码是

    #pragma endregion
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
        try
        {
            String^ connectionstring = "Data Source=localhost\sqlexpress;Initial Catalog=root_info;Integrated Security=True;";
            SqlConnection con(connectionstring);
            con.Open();
            String^ sqlquery = "insert into root_table values('"+this->textBox1+"','"+this->textBox2 +"')";
            SqlCommand cmd(sqlquery, % con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox::Show("datasubmited successfully", "success", MessageBoxButtons::OK);


        }
        catch (Exception^ ex)
        {
            throw ex;
        }
    }
    };
}
.net sql-server c++-cli
1个回答
0
投票

也许问题不在于 C++ 代码,而在于 SQL Express 实例的连接性。

您是否尝试过使用 SQL Server Management Studio 连接到此实例(从同一台计算机)?如果它也失败了,至少您可以将您的代码排除在故障排除之外。

© www.soinside.com 2019 - 2024. All rights reserved.