1 private void ConnSqlServer()
2 {
3 //连接数据库
4 MIConnection conn = new MIConnection();
5 try
6 {
7 conn.Open();
8 //XY绑定:含有x,y坐标点的图层放置在Sql Server数据库中,按xy坐标生成点图层
9
10 //建立XY空间方案
11 MapInfo.Data.SpatialSchemaXY spatialSchemaXY = new SpatialSchemaXY();
12 MapInfo.Geometry.CoordSysFactory coordSysFactory = new CoordSysFactory();
13 spatialSchemaXY.XColumn = "min_lvl";
14 spatialSchemaXY.YColumn = "max_lvl";
15 spatialSchemaXY.NullPoint = "0.0,0.0";
16 spatialSchemaXY.StyleType = MapInfo.Data.StyleType.None;
17 spatialSchemaXY.DefaultStyle = new MapInfo.Styles.SimpleVectorPointStyle(34, Color.Red, 15);
18 spatialSchemaXY.CoordSys = MapInfo.Engine.Session.Current.CoordSysFactory.CreateFromPrjString("1,0");
19 //建立TableInfo
20 // MapInfo.Data.TableInfoServer ti = new MapInfo.Data.TableInfoServer("Customers", "Driver={SQL Server};Server=LIXIAOHUI;Database=test;Uid=sa;Pwd=sa;", "select * from Us_Cust", MapInfo.Data.ServerToolkit.Odbc);
21 TableInfoServer tis = new TableInfoServer("TestConn");
22 tis.ConnectString = "DRIVER={SQL SERVER};SERVER=(local);DATABASE=pubs;UID=sa;PWD=;";
23 tis.Toolkit = ServerToolkit.Odbc;
24 tis.Query = "select * from jobs";
25 tis.SpatialSchema = spatialSchemaXY;
26 tis.Temporary = true;
27 MapInfo.Data.Table table = null;
28 table = conn.Catalog.OpenTable(tis);
29 if (table != null)
30 {
31 FeatureLayer fl = new FeatureLayer(table);
32 fl.VolatilityHint = MapInfo.Mapping.LayerVolatilityHint.CacheIfPossible;
33 MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[0];
34 map.Layers.Add(fl);
35 MessageBox.Show("Open Server data successful!");
36 }
37 }
38 catch (SqlException sqlEx)
39 {
40 MessageBox.Show(sqlEx.ToString());
41 }
42 finally
43 {
44 conn.Close();
45 }
46 }
2 {
3 //连接数据库
4 MIConnection conn = new MIConnection();
5 try
6 {
7 conn.Open();
8 //XY绑定:含有x,y坐标点的图层放置在Sql Server数据库中,按xy坐标生成点图层
9
10 //建立XY空间方案
11 MapInfo.Data.SpatialSchemaXY spatialSchemaXY = new SpatialSchemaXY();
12 MapInfo.Geometry.CoordSysFactory coordSysFactory = new CoordSysFactory();
13 spatialSchemaXY.XColumn = "min_lvl";
14 spatialSchemaXY.YColumn = "max_lvl";
15 spatialSchemaXY.NullPoint = "0.0,0.0";
16 spatialSchemaXY.StyleType = MapInfo.Data.StyleType.None;
17 spatialSchemaXY.DefaultStyle = new MapInfo.Styles.SimpleVectorPointStyle(34, Color.Red, 15);
18 spatialSchemaXY.CoordSys = MapInfo.Engine.Session.Current.CoordSysFactory.CreateFromPrjString("1,0");
19 //建立TableInfo
20 // MapInfo.Data.TableInfoServer ti = new MapInfo.Data.TableInfoServer("Customers", "Driver={SQL Server};Server=LIXIAOHUI;Database=test;Uid=sa;Pwd=sa;", "select * from Us_Cust", MapInfo.Data.ServerToolkit.Odbc);
21 TableInfoServer tis = new TableInfoServer("TestConn");
22 tis.ConnectString = "DRIVER={SQL SERVER};SERVER=(local);DATABASE=pubs;UID=sa;PWD=;";
23 tis.Toolkit = ServerToolkit.Odbc;
24 tis.Query = "select * from jobs";
25 tis.SpatialSchema = spatialSchemaXY;
26 tis.Temporary = true;
27 MapInfo.Data.Table table = null;
28 table = conn.Catalog.OpenTable(tis);
29 if (table != null)
30 {
31 FeatureLayer fl = new FeatureLayer(table);
32 fl.VolatilityHint = MapInfo.Mapping.LayerVolatilityHint.CacheIfPossible;
33 MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory[0];
34 map.Layers.Add(fl);
35 MessageBox.Show("Open Server data successful!");
36 }
37 }
38 catch (SqlException sqlEx)
39 {
40 MessageBox.Show(sqlEx.ToString());
41 }
42 finally
43 {
44 conn.Close();
45 }
46 }