SqlGeography test case

CSharp · Console

/Program.cs

using System;
using System.Data;
using Microsoft.SqlServer.Types;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Xml;

					
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello SqlGeography");
		var dt= GetGeoTable();
		SerializeToJson(dt);
		
	}
	
	//create datatable with SqlGeography datatype
	static DataTable GetGeoTable()
        {
            DataTable dataTable = new DataTable("table");
            dataTable.Columns.Add("f1", typeof(SqlGeography));
            dataTable.Columns.Add("id", typeof(int));

            DataRow newRow = dataTable.NewRow();           
            newRow["f1"] = SqlGeography.Point(20, 10, 4326); //lat long
            newRow["id"] = 1;
            dataTable.Rows.Add(newRow);

            newRow = dataTable.NewRow();
            newRow["f1"] = SqlGeography.Point(30, 15, 4326);
            newRow["id"] = 2;
            dataTable.Rows.Add(newRow);

            foreach (DataRow row in dataTable.Rows)
            {
                string ID = row["id"].ToString();
                string f1 = row["f1"].ToString();
                Console.WriteLine("{0} {1}", ID, f1);
            }            
            return dataTable;
        }
	//Serialize datatable to Json
	
	 static string SerializeToJson(DataTable dt)
        {   

            ITraceWriter traceWriter = new MemoryTraceWriter();           
            try
            {
                var options = new JsonSerializerSettings
                {   
                    Formatting = Newtonsoft.Json.Formatting.Indented,
                      TraceWriter = traceWriter,                   
                  
                };
                var json = JsonConvert.SerializeObject(dt, options);
                Console.WriteLine(json);
                return json;
            }
            catch (Exception ex)
            {
                Console.WriteLine(traceWriter);
                Console.WriteLine(ex);
               
            }           
            
            return "";           
        }

}
;