如何使用c++和.net框架连接Microsoft sql server

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

我正在 .net 框架中使用 C++ 语言创建一个应用程序,我尝试连接 Microsoft SQL 服务器,但无法连接它。错误按摩是

[System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to 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.