簡體   English   中英

如何在VS Express 2012 for Web中連接到SQL Server數據庫

[英]How to connect to SQL Server database in VS Express 2012 for Web

我正在嘗試學習使用C#(在ASP.NET網站之外)連接到SQL Server數據庫並使用SQL Server數據庫執行功能。 當我嘗試跑步

SqlConnection con = new SqlConnection("Data Source=.; database=fengshuidb; integrated security = SSPI");
SqlCommand cmd = new SqlCommand("Select * from emails", con);
con.Open();

連接時,我在con.Open()指令中收到以下錯誤。

從VS:

“建立與SQL Server的連接時發生了與網絡相關或特定於實例的錯誤。找不到服務器或無法訪問該服務器。請驗證實例名稱正確並且已將SQL Server配置為允許遠程連接。(提供者:命名管道提供程序,錯誤:40-無法打開與SQL Server的連接)”

我需要做什么來配置與數據庫的連接? 我需要什么安全設置? 這個主題有什么好的資源嗎?

嘗試以這種方式輸入連接

SqlConnection myConnection = new SqlConnection("Database=fengshuidb;Server=[Your_PC_Name]\\SQLEXPRESS;Integrated Security=True;connect timeout = 30");

如果數據庫是本地的,則“ [Your_PC_Name]”是本地計算機的名稱。 還請看一下此鏈接: http : //msdn.microsoft.com/zh-cn/library/jj653752%28v=vs.110%29.aspx

嘗試使用SqlConnectionStringBuilder創建有效的連接字符串

您在連接部分錯過了初始目錄。

更改為此:

Data Source=Your sql connection ;Initial Catalog=Your Database name;Integrated Security=True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM