I currently have a problem where I can't connect a database on my server but it works fine when running locally. I am using this connection string in the controller:
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
Controller code:
namespace PathTest.Controllers
{
public class HomeController : Controller
{
string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
public ActionResult Index()
{
DataTable daTbl = new DataTable();
using(SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Numbers", sqlCon);
sqlDa.Fill(daTbl);
}
return View(daTbl);
}
}
}
Error when running on server:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: 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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.)
Any help would be appreciated