Visual C# corrupting SQL Server files? RRS feed

  • Question

  • I've noticed an error that I've been able to reproduce, and I'm not sure whether it's a bug or a setting I need to adjust.

    Basically, whenever I use the database viewer in Visual C# 2005 Express, the database file seems to be corrupted after that.  It initally will display the contents of the database.  However, when I then close C# and enter SQL Server Management Studio Express and try to open the same database, I get this error.

    http://members.cox.net/mihind24/sqlerror.JPG   (the link button didn't work, not sure why)

    I have had success writing Console applications using SQL Server Express.  I can open up connections and basically do everything fine, and repeatedly use the database.  However, if I open the database in Database Viewer within the C# IDE, or if I use an item like a DataGridView where I have to select the database, it fails after I close Visual C#.

    I'm not sure what's going on, but it feels as if it's not closing the database correctly, or just is somehow corrupting the database file.

    Is this a known issue?  Or is there any way to fix this on my end?  Thanks!

    Monday, July 16, 2007 8:26 PM

All replies

  • Forgot to add...the database I am using is a sample database provided by Microsoft called Northwind.  I am using it in conjunction with a book I purchased that uses the database in its examples.

    To test my error further, I created my own database in SQL Server Management Studio Express, and when I opened the database in the Database Viewer in Visual C# Express, closed it, then attempted to open it in SQL Server Management Studio, it open without a problem. 

    I'm confused why a database I create would not be corrupted, but one of Microsoft's own databases corrupts after it is opened and closed in C#.
    Monday, July 16, 2007 9:25 PM
  • I'm also trying to get a grip on ADO and have noticed the same behavior with the Northwind database.

    I wrote a WindowsForms app and accessed the database without incident. Then I closed that and wrote a Console app. When I tried to access Northwind I got an "Error: 18456, Severity: 14, State: 16". (Check your server error log and see if you're getting the same error).

    After a lot of manipulating and hunting online I've concluded that the database isn't getting closed. The errors that are reported are OS errors, not SQL errors. I'm now in the process of trying to determine exactly how ADO opens and closes files (because there is no File.FileOpen in any of my code that I can see...)



    Tuesday, July 24, 2007 12:51 PM
  • I've found a solution to the problem, although it is not an ideal solution.  However, without knowing anything else, it suffices.

    What I found out is that Visual C# apparently creates it's own log file, northwnd_log.ldb instead of using the existing northwnd.ldb

    I tried something to see if it worked.  I deleted the existing northwnd.ldb, and renamed Visual C#'s log file from northwnd_log.ldb to northwnd.ldb.  I tried opening the database in SQL Server Management again, and luckily, it worked.  After that I just made a batch file that deletes the old one and renames the new one to the same as the old one to avoid having to do it manually each time I use Visual C#.

    Not the perfect solution, but much better than reinstalling Northwind each time.
    Tuesday, July 24, 2007 11:23 PM