2011-06-19 161 views
3

我是Visual C#的新手。 (我來自javaland)無法從C#連接到SQL Server Compact#

我使用IDE的Data -> Add New Data Source工具來創建我的應用程序可以連接的數據庫。 我可以從IDE的數據庫瀏覽器打開數據庫,但是我無法連接到應用程序的 數據庫。

該數據庫是一個沒有密碼或加密的SQL Server Compact 3.5文件。這是自動生成的連接字符串:

Data Source=|DataDirectory|\test.sdf 

但是當我運行下面的代碼...

SqlConnection con = new SqlConnection("Data Source=|DataDirectory|\\test.sdf"); 
    con.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)

con.Open();

我在做什麼錯?

感謝您的幫助!

回答

6

您應該使用SqlCeConnection類,而不是SqlConnection。您可能需要添加對system.data.sqlserverce程序集的引用。 SqlConnection類用於連接到完整版本的SQL Server(包括Express)。

+0

謝謝!現在工作很好。 – DRJTower

+0

絆倒了我大的時間,謝謝。 –

+0

謝謝!這真的幫助了我! –