所以,下面的代码给了我一个相当混乱的异常。
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 对象的列中。