[英]The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured
[英]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
我試圖通過構建一個基本的Asp.net MVC Web應用程序來學習MVC,該應用程序在Instances-->v11.0
文件夾中有一個Sample.mdf
文件(示例數據庫)。 Sql Server 2012 localdb安裝在我的Windows 10上。我能夠使用SQL Server Management Studio 2017中的服務器名稱(localdb)\\ V11.0以及命令提示符訪問,創建數據庫和表(Employee)。 我正在使用Windows身份驗證並使用本地IIS作為Web服務器,但是當我嘗試通過C#代碼(VS 2015)訪問相同時,我收到以下錯誤:
建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱是否正確,以及SQL Server是否配置為允許遠程連接。 (提供程序:SQL網絡接口,錯誤:50 - 發生本地數據庫運行時錯誤。無法創建自動實例。請參閱Windows應用程序事件日志以獲取錯誤詳細信息
在行中: con.Open();
員工控制員:
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
string connectionString = ConfigurationManager.ConnectionStrings["DBACESS"].ConnectionString;
List<Employee> employees = new List<Employee>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("Select Id,Name,City from Employee", con);
cmd.CommandType = CommandType.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
employees.Add(employee);
}
}
}
return View(employees);
}
}
網址 :
本地主機/ Web應用程序/雇員
連接字符串 :
<add name="DBACESS" connectionString="server=(localdb)\v11.0;database=Sample.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
在調試時我發現在執行con.open()
之前,沒有設置con對象的ServerVersion屬性並顯示錯誤:
'ServerVersion'引發了'System.InvalidOperationException'類型的異常?
員工模型類:
`public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}`
驗證模式:
`<system.web>
<authentication mode="Windows" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>`
我嘗試過堆棧溢出和論壇的各種修復,但沒有任何效果。
1)嘗試過mssqllocaldb或v12.0等其他實例,但沒有成功。
2)在IIS中從ApplicationPoolIdentity將身份類型更改為本地系統但沒有成功(事實上嘗試了所有可能的方案)
3)在IIS中的DefaultAppPool的 高級設置中將加載用戶配置文件設置為True ,但沒有成功。
4)在applicationHost.config中設置setProfileEnvironment="true"
(但沒有成功。)
C:\\ WINDOWS \\ SYSTEM32 \\ INETSRV \\ CONFIG
5) Sql Server配置管理器中的SQL Server服務中沒有項目。 這聽起來並不奇怪,因為我找到了localdb的以下信息:
LocalDB不創建任何數據庫服務; LocalDB進程在需要時自動啟動和停止。 應用程序只是連接到“Data Source =(localdb)\\ v11.0”,LocalDB進程作為應用程序的子進程啟動。 關閉此過程的最后一次連接幾分鍾后,該過程將關閉。
6)在Sql Server配置管理器的 客戶端協議選項卡中為TCP / IP 啟用了端口號 1433.此外,我已經創建了自己的入站規則,允許訪問TCP / IP的端口號1433和Windows中的動態端口程序防火牆 。但沒有成功。
7)在SSMS中 - >右鍵單擊服務器 ---> 屬性 - > 連接選項卡我勾選了允許遠程連接到此服務器但沒有成功。
8)這是我登錄的usermapping。 為此用戶啟用了db_owner和public 。 用戶映射
9) 服務器角色是我的登錄用戶的公共和sysadmin 。
10)在SSMS中 - >右鍵單擊服務器 ---> 屬性 - > 安全選項卡我已啟用SQL服務器和Windows身份驗證模式,但沒有成功。
11)重新安裝SQL server localdb但沒有快樂。
12)我正在附加Windows事件日志文件以獲取詳細錯誤。 Windows事件日志文件
我是ASP.net MVC的初學者。 快速幫助將受到高度贊賞。從最近三天開始,但沒有快樂。 提前致謝。
使用標准SQL安全性而不是Windows Integrated - 它更容易。 在SQL Server中設置一個新登錄名,並使用該登錄名為您嘗試訪問的數據庫的新用戶(使用戶成為數據庫的“所有者”)。 然后使用此連接字符串,將用戶名和密碼替換為您設置的登錄憑據:
Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
問題與您的代碼無關,它純粹是SQL服務器的身份驗證問題,幾乎肯定與SQL服務器接收的身份驗證憑據有關。 您收到消息的事實表明您的代碼正在運行並嘗試連接,並且在身份驗證的最后階段失敗。 使用SQL服務器登錄可以更清楚地傳遞您正在進行的身份驗證,並有助於消除問題的原因。
幾乎嘗試了一切后, 沒有任何效果。 Atlast我卸載了 localdb 2012並安裝了SQL server express 2012.It就像一個魅力!!但我不知道localdb中的真正問題:(。現在我使用的是實體框架和連接字符串(由ADO .net生成) 實體數據模型 )是:
<add name="EmployeeContext" connectionString="metadata=res://*/Models.EmployeeDataModel.csdl|res://*/Models.EmployeeDataModel.ssdl|res://*/Models.EmployeeDataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(local)\MYDEVEXPRESS12;initial catalog=Sample;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.