2013-04-10 54 views
2

我是這個世界的新手,坦率地說,我不喜歡它的複雜性,除此之外怎麼樣。連接到.sdf

我創建了一個名爲Sample.sdf的新數據庫,然後我創建了一個新的ASP.NET MVC3應用程序。我在讀,你必須將.sdf數據庫複製到項目的根目錄。所以我這樣做了,我試圖創建一個新的ADO.net連接到這個緊湊的數據庫,但我應該這樣做嗎?

我不能在web.config中使用連接字符串對象並將其配置爲指向該數據庫嗎?如果有,有沒有辦法測試連接?

我很好奇,因爲我打算在實體框架中使用它。

更新:

所以我想通了一些東西出來:

其中.sdf已進入App_Data文件夾,然後從他們的基礎上創建的一個ADO.net實體模型現有數據庫。它會自動查看數據庫,併爲您完成剩下的工作,或者至少在我的情況下它已經完成。

然而,它拋出一個錯誤,當點擊下一個實體數據模型嚮導,艇員選拔我的數據連接後,我得到了:

Unable to create new database.

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)

System.Data.SqlClient.SqlException (0x80131904): 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)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.SqlServer.Management.Common.ConnectionManager.InternalConnect(WindowsIdentity impersonatedIdentity)
at Microsoft.SqlServer.Management.Common.ConnectionManager.Connect()

我真的不知道如何着手。

+1

發生'SqlException'是因爲您嘗試使用* full-blown * SQL Server類連接到您的SQL Server ** Compact **數據庫 - 這是行不通的。當你創建實體數據模型時,你沒有選擇'SQL Server Compact'作爲你的數據庫類型,或者你的連接字符串被其他方式弄壞了。 – 2013-04-10 16:12:23

回答

0

我相信你是在正確的道路上,但因爲這是第一次,我會建議您遵循微軟的步步步行通過它可以在這裏找到:

數據庫第一種方法:http://msdn.microsoft.com/en-us/data/jj206878.aspx

一般的學習資源頁面在這裏:http://msdn.microsoft.com/en-us/data/ee712907

這就是我在開始使用MVC和實體框架時所用到的。這非常簡單直接,我向你保證,你會變得喜歡它。

我希望這會有所幫助。