首页 > 代码库 > An attempt to attach an auto-named database for file

An attempt to attach an auto-named database for file

在用VS自带的 .mdf读取(joint)时,报错:

 1 Server Error in / Application. 2 An attempt to attach an auto-named database for file F:\cs\office\ReadWriterOffice\ReadWriterOffice\App_Data\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 3  4 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 5  6 Exception Details: System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file F:\cs\office\ReadWriterOffice\ReadWriterOffice\App_Data\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. 7  8 Source Error: 9 10 Line 95:             DB db = new DB("DBConnectionString");11 Line 96:             using (SqlConnection conn = new SqlConnection(db.ConnectionString)) {12 Line 97:                 conn.Open();13 Line 98:                 using (SqlCommand cmd = conn.CreateCommand()) {14 Line 99:                     cmd.CommandText = sql;15 16 17 Source File: F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs    Line: 9718 19 Stack Trace:20 21 [SqlException (0x80131904): An attempt to attach an auto-named database for file F:\cs\office\ReadWriterOffice\ReadWriterOffice\App_Data\Database.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.]22    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +500959823    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +23424    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +227525    System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +3526    System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +18327    System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +23928    System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +19529    System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +23230    System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +18531    System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +3332    System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +52433    System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +6634    System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +47935    System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +10836    System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +12637    System.Data.SqlClient.SqlConnection.Open() +12538    ReadWriterOffice.Samples.TransmitLonLa.ExportExcel() in F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs:9739    ReadWriterOffice.Samples.TransmitLonLa.Page_Load(Object sender, EventArgs e) in F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs:2440    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +1441    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +3542    System.Web.UI.Control.OnLoad(EventArgs e) +9143    System.Web.UI.Control.LoadRecursive() +7444    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +220745 46  47 48 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 
View Code

解决方案是将连接字符串中的"AttachDbFilename"指定为绝对目录.

经过测试,我发现不是(joint)特殊,其实普通的select语句也会报一样的错,我猜想原因是sql比较特殊,这里应该是找不到,因为我的数据库比较大,所以我是从别的地方拷贝过来的,应该是日志文件不一致.所以需要制定绝对路径.

ref: http://stackoverflow.com/questions/12566036/an-attempt-to-attach-an-auto-named-database-for-file-database1-mdf-failed

An attempt to attach an auto-named database for file