.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

import from Mysql into Mtext

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Sgear
537 Views, 4 Replies

import from Mysql into Mtext

Hi

I convert Excel file to Shape file and import it to Mysql database
when I connect to MYSQL I get the data but can not get the Text only Point

 

is it possible I can do this in VB.NET connect to MYSQL insert it as Mtext or Block X,Y?
I have postcode and house number was thinking about query from postcode and insert the house number as Mtext or Block

anyone here who can help me how I can do this Please


Regards
Sgear

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Sgear

Try this sample, tested on SQLEXPRESS 2008, AutoCAD 2014

        public static List<object[]> GetRoomRecords()
        {
            List<object[]> records = new List<object[]>();
            //custom connection string
            string connStr =
       "Data Source=.\\SQLEXPRESS;Initial Catalog=MySQLAutoCAD;Integrated Security=True;";
            string sqlQuery =
        @"SELECT [ID]
        ,[postcode]
        ,[roomnumber]
        ,[description]
        ,[X]
        ,[Y]
        FROM [MySqlAutoCAD].[dbo].[Rooms]
        WHERE ([postcode]=99211 AND [roomnumber]> 99)";// change on your condition here
            SqlDataAdapter sqlAdapter = null;
            using (SqlConnection sqlCon = new SqlConnection(connStr))
            {
                try
                {
                    sqlAdapter = new SqlDataAdapter(sqlQuery, sqlCon);
                    using (SqlCommand sqlCommand = new SqlCommand(sqlAdapter.SelectCommand.CommandText))
                    {
                        System.Data.DataSet dataset = new System.Data.DataSet();
                        sqlAdapter.Fill(dataset, "Table1");// default table name, do not you change it

                        System.Data.DataTable dt = dataset.Tables["Table1"];
                        foreach (System.Data.DataRow dr in dt.Rows)
                        {
                            object[] items = dr.ItemArray;
                            records.Add(items);
                        }

                    }
                    sqlCon.Close();
                    return records;
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.Print(ex.Message);
                    return null;

                }
                
            }
        }
        [CommandMethod("msql")]
        public static void AddRecordsFromSQL()
        {
            List<object[]> data = new List<object[]>();
            try
            {
                data = GetRoomRecords();
            }
            catch
            {
                return;
            }
            if (data != null)
            {
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

                Editor ed = doc.Editor;

                Database db = doc.Database;

                try
                {
                    using (DocumentLock doclock = doc.LockDocument())
                    {
                        
                        using (Transaction tr = doc.TransactionManager.StartTransaction())
                        {
                            doc.TransactionManager.EnableGraphicsFlush(true);

                            BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);

                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                            foreach (object[] item in data)
                            {
                                int roomnumber = Convert.ToInt32(item[2], CultureInfo.CurrentCulture);
                                string description = Convert.ToString(item[3], CultureInfo.CurrentCulture);
                                double X = Convert.ToDouble(item[4], CultureInfo.CurrentCulture);
                                double Y = Convert.ToDouble(item[5], CultureInfo.CurrentCulture);
                                Point3d pt = new Point3d(X, Y, 0);

                                MText mtx = new MText();

                                mtx.SetDatabaseDefaults();

                                mtx.Attachment = AttachmentPoint.MiddleCenter;

                                mtx.Contents = string.Format("{0}\n\n{1}", roomnumber, description);

                                mtx.Normal = Vector3d.ZAxis;

                                mtx.Location = pt;

                                mtx.Location = mtx.Location;
                                ObjectId mtxId = btr.AppendEntity(mtx);

                                tr.AddNewlyCreatedDBObject(mtx, true);

                                tr.TransactionManager.QueueForGraphicsFlush();
                            }

                            ed.Regen();
                            doc.TransactionManager.FlushGraphics();
                            tr.Commit();
                        }
                    }
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage(ex.StackTrace);
                }
                finally
                {

                }
            }
        }

 

Message 3 of 5
Sgear
in reply to: Anonymous

 

 

fixo Thanks this help me a Lot !!! this is mutch faster using VB.NET 🙂

tell me what is best to do when you insert Mtext like this about font size
change the font when you insert or can you insert all text into one block or somthing so when you change the font you change all

this is my sample in VB.NET

1. I use command to open the form
2. in combobox I select postcode then I insert data into Listview
3. I insert into Autocad from Listview , in Combobox I can select font size

what is missing I need to insert ProgressBar to know when I am finish to insert to Autocad 🙂
mabye to fast for the ProgressBar hehe

the attachment is a .rar file just rename it to .rar
I don't use winzip

Regards
Sgear

Message 4 of 5
Anonymous
in reply to: Sgear

I haven't installed MYSQL on my machine,

sorry,I could not more help

Message 5 of 5
Sgear
in reply to: Anonymous

 

HI

 

No worries : thanks again

 

Regards

Sgear

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost