2013-04-24 37 views
0

我將在Visual Studio 2012(C#)中創建一個SQL Server數據庫文件(.MDF文件)。winform應用程序中的SQL Server連接字符串

我工作的桌面應用程序,我添加了一個新的.MDF文件的項目,但我不知道什麼是我的連接字符串,我得到這個錯誤,當我嘗試連接到我的DB:

SqlConnection SQLConnection = new SqlConnection("Data Source=db\\ofoghdb.mdf"); 
SQLConnection.Open(); 

錯誤:

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: 26 - Error Locating Server/Instance Specified)

我相當熟悉web開發SQL Server中,但我要在桌面(WinForm的)應用程序來使用它,我得到上述錯誤

+1

有此錯誤的幾個原因。 http://www.sswug.org/articlesection/default.aspx?TargetID=44331 – 2013-04-24 08:43:05

回答

1

也許你的connectionString想這個?

connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=databaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\my.mdf" 
1

嘗試指定一個完整的數據庫路徑:

SqlConnection SQLConnection = new SqlConnection(@"Data Source=C:\ofoghdb.mdf"); 
SQLConnection.Open(); 

查看更多here

+0

我又得到了同樣的錯誤 – 2013-04-24 08:52:58

0

您應該使用這樣的連接到數據庫服務器:

SqlConnection oSQLConn = new SqlConnection(); 
oSQLConn.ConnectionString = "Data Source=(local);" + 
          "Initial Catalog=mySQLServerDBName;" + 
          "Integrated Security=SSPI"; 
+0

那麼m數據庫文件名在哪裏呢? – 2013-04-24 08:57:07

+0

它是mySQLServerDBName?連接字符串中的 – 2013-04-24 08:59:57

+0

據我所知,我們必須使用dbserver名稱而不是.mdf文件名或路徑 – AnandPhadke 2013-04-24 09:10:23

0

試試這個連接字符串。

SqlConnection SQLConnection = new SqlConnection(@"Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=DB\MyData.mdf"); 
SQLConnection.Open(); 
0

試試這個

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
    + Environment.GetFolderPath(Environment.SpecialFolder.Personal) 
    + "\\folder name\\Databass name " + ";Persist Security Info=False;");