我在本地系统上安装了SQL Server 2008安装程序,而在.NET 4.5中安装了C#中的以下代码。本地无法连接到SQL Server
private Boolean ConnectDatabase()
{
if(!this.IsInitialized())
{
return false;
}
else
{
try
{
String ConnectionString = "User ID=" + this.DatabaseUsername + ";" +
"Password=" + this.DatabasePassword + ";" +
"Server=" + this.DatabaseHost + "," + this.DatabasePort + ";" +
"Database=" + this.DatabaseResource + ";";
this.DatabaseConnection = new SqlConnection(ConnectionString);
this.DatabaseConnection.Open();
return true;
}
catch (Exception e)
{
return false;
}
}
}
我可以连接到SSMS中的服务器,但使用相同的凭据我无法在C#中获得连接。
连接字符串如下:
User ID=sa;Password=******;Server=CPUNAME\MSSQL,1433;Database=DBNAME;
凡CPUName
,DBName
,和**与它们的相应的值替换。我也尝试过使用Windows Credentials和各种不同的服务器端点,包括我的NB名称,Loopback IP,静态内部IP,等等等等。防火墙关闭。 TCP在服务器上启用。
我得到以下异常:
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: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)
有什么建议?
是sqlexpress数据库吗? – 2013-03-26 16:22:19
否定的。 SQL Server企业版。服务正在运行。 – DigitalJedi805 2013-03-26 16:28:29
我怀疑你的SERVER部分不正确。试用(本地)或只是你的电脑的名字。 – Steve 2013-03-26 16:29:17