2013-11-28 53 views
2

請原諒我,如果這很容易,我已經看到類似的帖子,但我是新的C#,並一直在努力,所以任何幫助將不勝感激。連接到Visual Studio 2012中的SQL Server數據庫C#

我試圖連接到Visual Studio 2012中的本地數據庫SQL Server,但到目前爲止沒有運氣。

我從我的本地數據庫的屬性中獲取了我的連接字符串,其圖片位於左側窗格中。

public void connection() 
{ 

    string connectionString = "Data Source=(localdb)\v11.0;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"; 

    SqlConnection con = new SqlConnection(connectionString); 

    try 
    { 
     con.Open(); 
     lblConnectionTest.Text = "Connected successfully"; 

    } 
    catch (SqlException ex) 
    { 
     lblConnectionTest.Text = ex.Message; 
    } 


} 

在這一點上,所有我試圖做的是建立與數據庫的連接,基本上寫出「連接成功」等,如果它連接。

目前與我有什麼,我收到以下錯誤:

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: Named Pipes Provider, error: 40 -  
Could not open a connection to SQL Server) 

什麼我錯在這裏做什麼?

非常感謝!

enter image description here

+0

嘗試使用服務器的IP,如下所示:IP \ v11.0 –

回答

1

如果您在C#中,可以創建一個應用程序配置文件(App.config),它將會連接字符串與其名稱。

這是在app.config示例代碼

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    </configSections> 
    <connectionStrings> 
     <add name="myConnString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\AppsTest\Nov17RoomBooking\dbRoomBooking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
    providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
</configuration> 

你的代碼應該是這樣的:

private static string sqlConnectionString = ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString; 

    private void DeletingDataBase() 
    { 
     using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       string sqlQuery = "SELECT, INSERT, UPDATE, DELETE"; 
       cmd.Connection = sqlConn; 
       cmd.CommandText = sqlQuery; 
       try 
       { 
        cmd.Connection.Open(); 
        cmd.ExecuteNonQuery(); 
       } 
       catch { } 
       finally 
       { 
        cmd.Connection.Close(); 
       } 
      } 
     } 
    } 
+0

我還建議您嘗試使用LINQ,它真的很好,當你與數據庫交互。 :) 希望這可以幫助。 – Jin

+0

謝謝@Jeyn儘管我在一年前確實問過這個問題!儘管如此,我很高興看到另一個答案,事實上,我最喜歡你的,因此將它標記爲已被接受。 – Chris

+0

感謝您接受我的答案@ user2854993 :)很高興喜歡它。 – Jin

2

首先,我建議您設置的conenction串在你的配置文件。

鏈接:http://msdn.microsoft.com/fr-fr/library/ms254494(v=vs.110).aspx

第二個問題,最佳實踐設置你的連接using bloc

鏈接:http://msdn.microsoft.com/fr-fr/library/system.data.sqlclient.sqlconnection(v=vs.110).aspx

數據源是您的目標SQL服務器,服務器的訪問屬性,並獲得名字,但對於你的情況我認爲你想遠程訪問,所以確保你的防火牆服務器配置爲允許。

+0

只需在此添加提示:初始目錄在連接字符串中缺失。 – sprinter252

+0

確實,您必須添加初始目錄誰定義您的數據庫 –

0

我鼓勵你開始學習LINQ to SQL。在我看來,這是處理數據和與數據庫交互的更好方式。我看到你正在嘗試連接到MySql,但我真的不知道在使用LINQ時是否可能。我希望有人回答這個問題。

0

您的連接字符串錯誤。

正確語法MS SQL Server 2012的快(內置)

讓想我的數據庫文件的路徑:

C:\用戶\ Zohair \文檔\的Visual Studio 2012 \項目\ Learning2 \ Learning2 \ Sample.mdf

我的連接字符串將是:

SqlConnection conn = new SqlConnection("Data Source=(localdb)\\v11.0;Integrated Security=true;AttachDbFileName=C:\\Users\\Zohair\\Documents\\Visual Studio 2012\\Projects\\Learning2\\Learning2\\Sample.mdf"); 

注意:如果在使用單斜槓時出現錯誤(\),則用雙斜槓替換它們(\\

0

實際上,你可以解決這一切與點擊幾下,即時通訊使用的Visual Studio並通過將我的ms sql表格拉到aspx文件中,它實際上形成了表格,甚至爲您建立了所有的連接字符串。

+0

Hi @Siim Konks,謝謝你的回覆,雖然這個問題差不多一年半了:) – Chris

相關問題