0

I developed a windows based application where I use .mdf file for backend operations. When no data is passed through application into .mdf file that means a fresh .mdf file, I am able to attach this .mdf file in my SQL Server 2008 R2 successfully.

But when data is written into .mdf file through application, I am not able to attach that .mdf file. I am getting a SQL Server error:

enter image description here

Why version is changing automatically? Please help me

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RosiReddy
  • 27
  • 3
  • 14
  • The version did *not* change automatically. It was changed by your application, which has newer drivers and *obviously knows* that the file isn't going to be used as a server-based database because you told it so when you asked it to open it as a file-based database. BTW file-based databases and "backend operations" are a contradiction. Use a single server-based database instead of moving files around. At the very least you'll be able to backup your data which now is impossible – Panagiotis Kanavos May 12 '16 at 12:57
  • BTW if you are going to use SQL Express why aren't you using the latest version? – Panagiotis Kanavos May 12 '16 at 12:59
  • its not possible to create a database at client end. thats why I am including the mdf file in application folder itself – RosiReddy May 12 '16 at 13:10
  • 1
    A database is not a file like a Word or Excel document. At the very least, make sure you use the *latest* versionm of SQL Server Express, *not* the oldest – Panagiotis Kanavos May 12 '16 at 13:32
  • Please read the below comments made by @marc_s – RosiReddy May 12 '16 at 13:36
  • I tried as @marc_s instructed. but couldn't work – RosiReddy May 12 '16 at 13:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111769/discussion-between-rosi-reddy-and-panagiotis-kanavos). – RosiReddy May 12 '16 at 13:39

1 Answers1

0

You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version (706 = SQL Server 2012) of SQL Server down to an older version (661 = SQL Server 2008 R2) - the internal file structures are just too different to support backwards compatibility.

You can either get around this problem by

  • using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances

  • otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool

  • or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I am attaching the mdf file in both cases in same pc – RosiReddy May 12 '16 at 13:04
  • @RosiReddy: so obviously, you have **two instances** with different versions of SQL Server on that PC .... – marc_s May 12 '16 at 13:06
  • 1
    I deleted the database which i attached mdf file(which has no data) for first time. Then i tried to attach the mdf file where it has data – RosiReddy May 12 '16 at 13:08
  • @RosiReddy: and I'm still saying: you have **two** instances of SQL Server (the database engine) running, one is **2012**, one is **2008 R2**, and you won't ever be able to attach a `.mdf` from SQL Server 2012 to the SQL Server 2008 R2 instance. Be aware: the SQL Server 2012 instance might be a SQL Server Express and / or Local DB instance, too! – marc_s May 12 '16 at 13:10
  • Even after I uninstalled the sql localdb 2012. Still i am not able to attach the mdf file. shows same version error – RosiReddy May 12 '16 at 13:23