2013-04-10 33 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和实体框架时所用到的。这非常简单直接,我向你保证,你会变得喜欢它。

我希望这会有所帮助。