VERIFIED SOLUTION i

How to create temp tab file from a SQL Server Database in MapXtreme.NET

1.Create MI connection object connection
2.Create TableInfo object for access sql server table from Data base
3.Query on feature layer
4.put result in feature collection.
5.Create new temp table and add same result from feature collection and also apply style on temp table
=============================================================
//Open Connection 
MIConnection Connection = new MIConnection(); 
Connection.Open(); 
 
//Create TableInfo for SQL connection 
TableInfoServer ti = new TableInfoServer("QUERY1", "DRIVER={SQL Server};DLG=SQL_DRIVER_NOPROMPT;SERVER=???;DATABASE=???;uid=???;pwd=???", 
@"select obj,MI_PRINX,MI_STYLE from USA", MapInfo.Data.ServerToolkit.Odbc); 
 
ti.CacheSettings.CacheType = CacheOption.All; 
ti.MbrSearch = true; 
 
MapInfo.Data.SearchInfo si; 
MapInfo.Data.Catalog cat; 
 
//Create Table for SQL layer 
MapInfo.Data.Table ntbl = MapInfo.Engine.Session.Current.Catalog.OpenTable(ti); 
FeatureLayer lyr = mapControl1.Map.Layers["QUERY1"] as FeatureLayer; 
 
si = MapInfo.Data.SearchInfoFactory.SearchAll(); 
cat = MapInfo.Engine.Session.Current.Catalog; 
IResultSetFeatureCollection irfc = cat.Search("QUERY1", si); 
 
MapInfo.Data.IFeatureCollection ifc = irfc as IFeatureCollection; 
 
Column[] cls = new Column[ntbl.TableInfo.Columns.Count]; 
ntbl.TableInfo.Columns.Clone().CopyTo(cls, 0); 
 
//Create MemTable TableInfo 
TableInfoMemTable tiMt = new TableInfoMemTable("testMemTable"); 
tiMt.Temporary = true; 
tiMt.Columns.Add(cls); 
 
//Create MemTable 
MapInfo.Data.Table tbl = Connection.Catalog.CreateTable(tiMt); 
SimpleInterior sInterior; 
sInterior = new SimpleInterior(8, Color.Red, Color.AliceBlue); 
CompositeStyle cStyle = new CompositeStyle(sInterior); 
 
//Populate MemTable 
foreach (MapInfo.Data.Feature ftr in irfc) 
 
string st1 = ftr[ftr.Columns[1].ToString()].ToString(); 
string stnm = ftr[ftr.Columns[2].ToString()].ToString(); 
MapInfo.Data.MICommand micomm = Connection.CreateCommand(); 
micomm.CommandText = "insert into " + tbl.Alias + " (obj,MI_PRINX,MI_STYLE) values(@obj,@MI_PRINX,@MI_STYLE)"; 
micomm.Parameters.Add("@obj", ftr.Geometry); 
micomm.Parameters.Add("@MI_PRINX", 1); 
micomm.Parameters.Add("@MI_STYLE", cStyle); 
int exec = micomm.ExecuteNonQuery(); 
micomm.Dispose(); 
 
//Create Native TableInfo and set path to save to 
TableInfoNative tiNat = (TableInfoNative)TableInfoFactory.CreateFromFeatureCollection(MapInfo.Data.TableType.Native, tbl); 
tiNat.TablePath = "c:\\" + tiMt.Alias + ".TAB"; 
//Write metadata for native tab 
tiNat.WriteTabFile(); 
 
//Create Native tab 
Table nativeTable = Session.Current.Catalog.CreateTable(tiNat); 
 
//Close Native tab 
nativeTable.Close(); 
 
//Reopen Native tab 
Table nativeTable2 = Session.Current.Catalog.OpenTable(@"c:\testMemTable.TAB"); 
 
//Populate Native tab 
foreach (Feature feature in tbl) 
//this is a workaround as when closing and reopening the Tab file causes the geomery column to change from SP_GEOMETRY to obj 
feature.Columns[0].Alias = "obj"; 
 
nativeTable2.InsertFeature(feature); 
 
nativeTable2.Close(); 
 
Environment Details
Products affected: MapXtreme.NET ™
UPDATED:  September 20, 2017