Author Topic: C3D2011 & migrating old MDB files to SQL  (Read 2879 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4099
  • C3D user & customizer
C3D2011 & migrating old MDB files to SQL
« on: December 14, 2010, 09:57:34 AM »
Not sure where else to post this, and an exhaustive search has gotten me no where. As we all know, x64 applications can no longer use MS.Jet to open Access MDB files. Autodesk has built-in functions for C3D to convert old C3D mdb files to SQL Server Compact 3.5 format.

I need to read some older MDB files that C3D ignores, but have been unable to figure out how to update and read those files programatically (c#). I cannot use any solution which involves installing other software, as this may be run an many other PC's. Since C3D2011 installs (or ensures it exists on the system) SQL Server Compact 3.5, I think this is about all I have to work with.

Any ides, thoughts, pointers to good reading material, etc. will be greatly appreciated. It may help to know that I've never used SQL* for anything, so maybe I'm just missing something obvious in my searches.

n.yuan

  • Bull Frog
  • Posts: 348
Re: C3D2011 & migrating old MDB files to SQL
« Reply #1 on: December 14, 2010, 11:06:24 AM »
Not like SQL Server, which comes with ready-to-use tool (SQL Server Management Studio/Express) for importing/exporting data from/to other type of databases, SQL Server Compact is a file based database without management tool coming with it.

I did a google search that yield very few links to tools to move data in/out SQL Server Compact DB. There are a few third party tools do what you want.

However, if the said MDB database is not too big/complicated (say hundreds of tables), I'd simply manually re-create the tables in the SQL Server Compact (you can do it with SQL Server Management Studio), and then write a small EXE in VB/VB.NET/C# (or VBA in Access) to move the data in tables from MDB to the *.sdf.

The code to retrieve data from a table and update into another table would be the same except for corresponding SQL statement "SELECT..." and "INSERT INTO...". So, just write 2 subs that take a "SELECT..."/"INSERT..." as parameter, and run against each table, it'll be fairly easy.

Pseudo-code:

static main()
{
   DataTable tbl=null;
   
   using ([connection=connection to MBD])
   {
      tbl=GetData(connection,"Select...From tbl1...");
   }

   Using ([connection=Connection to SDF])
   {
      UpdateData(connection,tbl,"INSERT INTO tbl1...");
   }
}

private DataTable GetData(OldDbConnection connection,string selectStatement)
{
   DataTable tbl=DataTable();
   //Create OldDbCommand with the "Select..."statement
        //Create OldDbDataAdapter with the OleDbCommand
        OldDbDataAdapter.Fill(tbl);
   return tbl
}

private void UpdateData(OldDbConnection connection,DataTable tbl,string insertStatement)
{
   //Create OldDbCommand with the Insert...statement
   //Note, each inserting value in the INSERT... startement should be made as parameter
   //like: insert into tbl1 (field1,field2...) VALUES (@param1,@param2...)
   
   for each DataRow r in tbl.Rows)
   {
      for each DataColumn col in tbl.Columns
      {
         SqlCommand1.Parameters.AddWithValue("@Param1",r[col.Name]);
      }

      SqlCommand1.ExecuteNonQuery();
   }
}


If you have quite a few MBD files to transter data from, it is worth to take a few hours to polish your code for such a tool

Jeff_M

  • King Gator
  • Posts: 4099
  • C3D user & customizer
Re: C3D2011 & migrating old MDB files to SQL
« Reply #2 on: December 14, 2010, 06:39:57 PM »
Thanks for that, Norman.  Perhaps I should've thought this out a bit more before posting. I don't necessarily need/want the SQL conversion, as these are not database's that I am saving. I merely want to read the existing mdb files, extract the data, and finally use that data inside C3D. This must run entirely within AutoCAD, which is why I though the SQL route was the way to go since that is the direction Autodesk has taken with their C3D database formats.

Here's what I've used in (x86) versions of C3D:
Code: [Select]

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;
            fbd.Description = "Select Project Root";
            DialogResult res = fbd.ShowDialog();
            if (res == DialogResult.OK)
            {
                string datapath = fbd.SelectedPath + @"\cogo\groups.mdb";
                if (!File.Exists(datapath))
                {
                    AcApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\n...Selected folder is not a valid LandDesktop Project folder, try again.");
                    return;
                }
                else
                {
                    OleDbConnectionStringBuilder connStr = new OleDbConnectionStringBuilder();
                    connStr.Add("Provider","Microsoft.Jet.OLEDB.4.0");
                    connStr.Add("Data Source", datapath);
                    OleDbConnection dbConn = new OleDbConnection(connStr.ConnectionString);
                    dbConn.Open();
                    OleDbDataReader rdr = null;
                    try
                    {
                        OleDbCommand cmd = new OleDbCommand("SELECT * FROM GROUPPROPS", dbConn);
                        rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            //my code to modify C3D Point Groups
                         }
                     }
                     catch { }
                  }
             }
As you can see, it's really rather simple, or at least it was until C3D2011(x64) was released....in which case I get the dreaded "Microsoft.Jet.4.0" is not registered.....

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: C3D2011 & migrating old MDB files to SQL
« Reply #3 on: December 14, 2010, 06:50:11 PM »
So it would seem you are updating the point group migration functionality.

and...I don't see any non application install way around right now....
Be your Best


Michael Farrell
http://primeservicesglobal.com/

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8788
  • AKA Daniel
Re: C3D2011 & migrating old MDB files to SQL
« Reply #4 on: December 14, 2010, 10:54:31 PM »
I "think" you can use SQL server to convert your .MDBs to  .MDFs.. I also "think" that compact can attach to .MDF files  :?

Jeff H

  • Needs a day job
  • Posts: 6150

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: C3D2011 & migrating old MDB files to SQL
« Reply #6 on: December 15, 2010, 05:05:34 AM »

Yes Jeff.

BUT, most people (clients)  in commercial circumstances install the 32 bit version of Office on X64 otherwise all their favourite apps wont work ..... and both can't be installed so the x64 driver won't be available.

not a really optimum situation for anyone attempting to provide solutions.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: C3D2011 & migrating old MDB files to SQL
« Reply #7 on: December 16, 2010, 11:22:47 PM »
If the data is static could you convert to .sdf and deploy with project?

Also you can add the necessary dll's and the user will not need Sql Server(In your case they will already have it) and will not need admin rights.

Also SqlMetal can be used to allow LINQ to SQL for SQL compact