I am creating an app with C++ language in .NET Framework I am trying to connect with SQL Server but I can't connect with it. The error massage is
[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)']
My code for this app is
#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;
}
}
};
}
System::Object^ senderis not a valid C++.\smeans in your connection string? Escape symbols correctly!throw exwill wipe the stack trace.