Results 1 to 18 of 18

Thread: Database Corruption issue with Sql Express localdb

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Angry Database Corruption issue with Sql Express localdb

    There are even bigger issues using Sql Express SqlLocalDB. If you connect to the Sql Express database .mdf file using the AttachDbFilename= method, it locks the database up. The next time you try to access it with an SQL tool or the Sql Server Management Studio, it shows the database as (Pending Recovery). Had to drop the database and recreate it.

    Happened twice. Recreated database, loaded sample tables. Opened database.mdf using attachfile method, ran a simple query against a 9 row table, closed recordset and the database, then closed the application. Tried to view table with SSMS, showed database as (Pending Recovery).

    I guess I will have to go back to the Access 2007 database if I can solve the issue with not being able to run the compact process from within Visual Basic.

  2. #2
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Database Corruption issue with Sql Express localdb

    You're only attaching the database one time correct? or are you using the attachDbFileName each time you run your program?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    This error occurs the first and only time the file is touched. Currently, I have only one test function that touches the Sql Express .mdf file. The rest of the app still uses the Access 2007 database.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Database Corruption issue with Sql Express localdb

    So, why are you using that method to attach the database? Essentially, if that is causing you trouble, then the best solution is to not use it. What problem are you trying to solve that you need that method for?

    After all, if you are worried about database corruption, going back to Access is not exactly a solution. Database corruption in Access is more interesting than in SQL Server, since it is so nearly unpredictable, but it still happens.
    My usual boring signature: Nothing

  5. #5
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Database Corruption issue with Sql Express localdb

    Are you attaching the database through your app or SSMS?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    Attaching via the applications. This an application for distribution to unknown users. I need to create the database and load some tables with default values then distribute with the application. It is likely that none of the ultimate users would have SqlServer installed on their computers. That is the reason for wanting to use SqlLocalDB and attach via a filename.

    Perhaps the issues is because I created the database with an SQL tool connected to SQLServer via a DSN name. Then connected to the resultant .mdf file using DBattachFileName via the application.

    I will see if I can run localdb and create an instance on the same computer that has SQLServer Express running on it.

    I know what you mean about Access. I hoped to switch to localdb to avoid the self-bloating, self-corrupting issues associated with Access.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Database Corruption issue with Sql Express localdb

    Are you using SQL Server Express 2012? I see that one has a new option for local DBs. Someday I may have to move there. For now, I'm in an organization that is using 2008 R2, though, so not just yet.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    Yes. I am using SQL Server Express 2012.

  9. #9

    Re: Database Corruption issue with Sql Express localdb

    May I ask why you're using:
    Code:
    AttachDbFilename=
    I've been around the block and back a few times with SQL Server and I've never had a need to do this.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    Because this will be part of a distributed app. The .mdf and .ldf will be included in the install pack. Users will not have SQLserver installed. I will do a silent install with SQLLocalDB.msi and SQLNCLI.msi. Many posts on various forums and some on MSDN say that is the way to go.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    Problem Solved: After the silent install of SQLExpress localdb and SQLServer client tools, I create the instance with the following:
    SqlLocalDB create RCMDS
    SqlLocalDB start RCMDS

    Then I create the database with the function below (requires - Imports System.Data.SqlClient). After that I create necessary tables and insert any required records.

    Code:
      Private Function CreateDatabase() As Boolean
        ' Declare Variables
        Dim sqlString As String
        Dim myConn As SqlConnection = New SqlConnection("Server=(localdb)\RCMDS;database=master")
    
        sqlString = ""
        sqlString += "Create Database RCMDataStore ON PRIMARY" + vbCrLf
        sqlString += " ( NAME = RCMDataStore," + vbCrLf
        sqlString += "   FILENAME = 'C:\RecipeMinder\DataStore\RCMDSData.mdf'," + vbCrLf
        sqlString += "   SIZE = 20MB," + vbCrLf
        sqlString += "   MAXSIZE = 500MB," + vbCrLf
        sqlString += "   FILEGROWTH = 5MB )" + vbCrLf
        sqlString += "LOG ON" + vbCrLf
        sqlString += " ( NAME = RCMDSLog," + vbCrLf
        sqlString += "   FILENAME = 'C:\RecipeMinder\DataStore\RCMDSLog.ldf'," + vbCrLf
        sqlString += "   SIZE = 10MB," + vbCrLf
        sqlString += "   MAXSIZE = 500MB," + vbCrLf
        sqlString += "   FILEGROWTH = 5MB )" + vbCrLf
        sqlString += " COLLATE Latin1_General_CI_AI" + vbCrLf
    
        Dim myCommand As SqlCommand = New SqlCommand(sqlString, myConn)
    
        Try
          myConn.Open()
          myCommand.ExecuteNonQuery()
          MessageBox.Show("Database created successfully", _
                          "MyProgram", MessageBoxButtons.OK, _
                           MessageBoxIcon.Information)
        Catch ex As Exception
          MessageBox.Show(ex.ToString())
        Finally
          If (myConn.State = ConnectionState.Open) Then
            myConn.Close()
          End If
        End Try
    
        Return True
    
      End Function   '   END  CreateDatabase()

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Database Corruption issue with Sql Express localdb

    In other words: Problem solved in the most painful way imaginable.

    This is an issue that I will be dealing with in less than a year, hopefully, but am not dealing with right now. I've used the kind of solution you showed there in a previous application, but would really prefer to have an easier way to do the same work in the future.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2013
    Location
    Somewhere
    Posts
    70

    Re: Database Corruption issue with Sql Express localdb

    I guess I am "old school". Coding it that way might take more lines of code, but at least it works AND I have control. I tried at least 15 or more supposed "working" examples from various postings. Didn't have any more time to expend. It works - so I moved on to the next item on the To-Do list.

    Been in this business for 30+ years supporting SQLServer, Oracle, DB2, and teradata and VERY seldom does the hoped for "easier" way work. Most often searching for the "easier" way turns into hours and hours of frustration - trying this and trying that until you finally "happen" across something that works but then it breaks something else. Hope you are fortunate and find the easier way.

  14. #14
    Addicted Member
    Join Date
    Oct 2008
    Location
    Califorina
    Posts
    235

    Re: Database Corruption issue with Sql Express localdb

    What about doing the silent install then restoring a backup of the blank database?

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Database Corruption issue with Sql Express localdb

    Quote Originally Posted by Whizzo View Post
    Attaching via the applications. This an application for distribution to unknown users. I need to create the database and load some tables with default values then distribute with the application. It is likely that none of the ultimate users would have SqlServer installed on their computers. That is the reason for wanting to use SqlLocalDB and attach via a filename.

    Perhaps the issues is because I created the database with an SQL tool connected to SQLServer via a DSN name. Then connected to the resultant .mdf file using DBattachFileName via the application.

    I will see if I can run localdb and create an instance on the same computer that has SQLServer Express running on it.

    I know what you mean about Access. I hoped to switch to localdb to avoid the self-bloating, self-corrupting issues associated with Access.
    Yup. that's where you went wrong. With a LocalDBFile, you'll notice the lack of an ldf that goes with it during distribution... it'll be generated later when it's attached. What you had though was a full db, including the ldf, which the mdf was aware of... when you told it to attach the mdf, it went looking for the ldf... and that's what hung it up. Best I've found is to create it as a localdbfile from VS right from the start.

    Quote Originally Posted by formlesstree4 View Post
    May I ask why you're using:
    Code:
    AttachDbFilename=
    I've been around the block and back a few times with SQL Server and I've never had a need to do this.
    It's a newish option... a way to distribute SQL databases in a fairly painless way... w/o having to also distribute scripts or stuff like that. Of all the times I've seen it used... this is the first one I've heard of failing.

    Quote Originally Posted by Whizzo View Post
    Because this will be part of a distributed app. The .mdf and .ldf will be included in the install pack. Users will not have SQLserver installed. I will do a silent install with SQLLocalDB.msi and SQLNCLI.msi. Many posts on various forums and some on MSDN say that is the way to go.
    Generally speaking it is... as I jsut mentioned about... this is the first I've heard about it failing like this... but I think it's because of how it began life originally...

    Quote Originally Posted by smendoza View Post
    What about doing the silent install then restoring a backup of the blank database?
    Oddly, that's even more work... you still have to first create the database, then restore to it.


    Probably too late to mention this, but given the limited visiblity the database needs to be... you could have gone with SQL Server Company Edition (SQLCE) ... No need to isntall SQL Server... just the SQLCE assemblies (which isn't much) and then the sdf (the database file).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Corruption issue with Sql Express localdb

    Isn't AttachDBFilename all about working with a DB in single user mode?? Basically it's a USER INSTANCE of the DB.

    Why would you expect to be able to do that and also work the DB through SSMS?

    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    The OP attacks the feature for what it's not designed to do - what's up with that?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Database Corruption issue with Sql Express localdb

    Quote Originally Posted by techgnome View Post
    Probably too late to mention this, but given the limited visiblity the database needs to be... you could have gone with SQL Server Company Edition (SQLCE) ...

    -tg
    Company???

    I have always heard that as Compact.
    My usual boring signature: Nothing

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Database Corruption issue with Sql Express localdb

    Maybe that's why I've had problems with it before. :P

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width