I have just finished development of a C# 2010 windows desktop application that uses SQL Server 2005 Express.
I gave it to a colleague to test on his Windows 7 (64bit) laptop.
The program ran as expected on day one, connecting the database to the local instance of SQL Server Express when it made the first db call with a connection string like this:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|MyDB.mdf;Database=MyDB;integrated security=true;user instance=true;
The next day when he ran the program it failed with the following exception:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "MyDB" requested by the login. The login failed.
Login failed for user 'Tack-PC\Tack'.
File activation failure. The physical file name "C:\Program Files (x86)\MyCo\MyApp\MyDB_log.LDF" may be incorrect.
The log cannot be rebuilt when the primary file is read-only.
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
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 MyApp.DataGateway.getTable(String sql)
at MyApp.frmMain.InitialiseList()
at MyApp.frmMain.frmMain_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.FormsControl.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I also noticed the log file (.ldf
) that SQL Server creates after attaching the DB for the first time was missing.
Does anyone have any idea why or how it could have disappeared?
The first thing I did on reading the exception was (a bit heavy handed I know, but I just wanted to get him back up and running while he could still dedicate time to testing for me and I could research the problem in parallel) to give full permissions to the working folder for all users, so it could at least create a new log file.
This resulted in the program successfully running, but all data he entered on day one was not gone.
Can anybody shed light on this situation for me? Why did the log file vanish?
And while we are at it, why was all the data lost? Wouldn't it be in the mdf file, which was still there?