连接字符串 ASP.NET LocalDB
Connection string ASP.NET LocalDB
我目前遇到无法连接服务器上的数据库的问题,但在 运行 本地时它工作正常。我在控制器中使用这个连接字符串:
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
控制器代码:
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);
}
}
}
服务器 运行 时出错:
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.)
如有任何帮助,我们将不胜感激
首先,我相信您不想 运行 生产服务器上的 localdb。您应该在您的服务器上部署(如果尚未部署)一个 SQL 服务器实例,因此连接字符串应为:
"Server=[Server IP/Name];Database=MSSQLLocalDB;User Id=[Some User Created];Password=[Password of User Created];"
如果我错了,而你想要服务器上的 localdb,你需要检查 SQL Server Express 服务是否 运行ning。
如果有,检查数据库文件是否存在,如果不存在,可以使用工具sqllocaldb.
为express实例创建本地数据库
最后,您可以保留两个连接字符串,通过 .config 文件使用配置管理器(您可以在调试配置文件上使用本地数据库,在发布配置文件上使用服务器数据库)。
希望对你有帮助!
我目前遇到无法连接服务器上的数据库的问题,但在 运行 本地时它工作正常。我在控制器中使用这个连接字符串:
@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Test.mdf';";
控制器代码:
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);
}
}
}
服务器 运行 时出错:
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.)
如有任何帮助,我们将不胜感激
首先,我相信您不想 运行 生产服务器上的 localdb。您应该在您的服务器上部署(如果尚未部署)一个 SQL 服务器实例,因此连接字符串应为:
"Server=[Server IP/Name];Database=MSSQLLocalDB;User Id=[Some User Created];Password=[Password of User Created];"
如果我错了,而你想要服务器上的 localdb,你需要检查 SQL Server Express 服务是否 运行ning。
如果有,检查数据库文件是否存在,如果不存在,可以使用工具sqllocaldb.
为express实例创建本地数据库最后,您可以保留两个连接字符串,通过 .config 文件使用配置管理器(您可以在调试配置文件上使用本地数据库,在发布配置文件上使用服务器数据库)。
希望对你有帮助!