Итак, следующий код дает мне довольно запутанное исключение.
public void BuildTable()
{
using (SqlConnection connection = new SqlConnection("Data Source=ylkx1ic1so.database.windows.net;Initial Catalog=hackathon;Persist Security Info=True;User ID=REDACTED;Password=REDACTED"))
{
SqlGeographyBuilder builder = createTestPoint();
SqlGeography myGeography = builder.ConstructedGeography;
connection.Open();
DataTable myTable = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM myTable", connection))
{
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
adapter.Fill(myTable);
myTable.Rows.Add(6, "Test", myGeography);
adapter.Update(myTable);
}
}
}
private SqlGeographyBuilder createTestPoint()
{
SqlGeographyBuilder builder = new SqlGeographyBuilder();
builder.SetSrid(4326);
builder.BeginGeography(OpenGisGeographyType.Point);
builder.BeginFigure(31, -85);
builder.EndFigure();
builder.EndGeography();
return builder;
}
В строке myTable.Rows.Add(6, "Test", myGeography); я получаю следующее исключение:
Type of value has a mismatch with column typeCouldn't store <POINT (-85 31)> in placemark Column. Expected type is SqlGeography.
Чего я не понимаю, так это почему это не сработает. Во время отладки я пробовал это:
for (int i = 0; i < myTable.Columns.Count; i++)
{
Debug.WriteLine(myTable.Columns[i].DataType);
}
Debug.WriteLine(myGeography.GetType());
И мой выход был:
System.Int32
System.String
Microsoft.SqlServer.Types.SqlGeography
Microsoft.SqlServer.Types.SqlGeography
Итак, я определенно пытаюсь поместить объект SqlGeography в столбец, который принимает объекты SqlGeography.