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

 Server Error in '/' Application.
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. 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. 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. Source Error: Line : DB db = new DB("DBConnectionString");
Line : using (SqlConnection conn = new SqlConnection(db.ConnectionString)) {
Line : conn.Open();
Line : using (SqlCommand cmd = conn.CreateCommand()) {
Line : cmd.CommandText = sql; Source File: F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs Line: Stack Trace: [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.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +
System.Data.SqlClient.SqlConnection.Open() +
ReadWriterOffice.Samples.TransmitLonLa.ExportExcel() in F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs:
ReadWriterOffice.Samples.TransmitLonLa.Page_Load(Object sender, EventArgs e) in F:\cs\office\ReadWriterOffice\ReadWriterOffice\Samples\TransmitLonLa.aspx.cs:
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +
System.Web.UI.Control.OnLoad(EventArgs e) +
System.Web.UI.Control.LoadRecursive() +
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) + Version Information: Microsoft .NET Framework Version:4.0.; ASP.NET Version:4.0.30319.1

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

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

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

05-08 08:42