简体   繁体   中英

SQL Server DataReader.GetField returned null instead of geography data

I have a Microsoft SQL Server 2008 (SP3) database. I was able to write some geospatial data into a table using the Geography data type.

I am now trying to read the data out of the database but I am getting an error:

DataReader.GetField(2) returned null

There is of course information in the data column.

The code below is straight forward. I'm using the .NET Framework 4.7. Is there something obvious that I'm not handling?

string sql = @"
    SELECT *
    FROM Locations
    WHERE LocID= " + tableOutput.OID.ToString();

System.Data.DataSet ds = Database.RequestData(sql);

static public System.Data.DataSet RequestData(string sql)
{
    System.Data.DataSet ds = null;
    try
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            using (SqlDataAdapter importer = new SqlDataAdapter(sql, conn))
            {
                ds = new System.Data.DataSet();
                importer.Fill(ds, "Data");
                return ds;
            }
        }
    }
    catch (Exception e)
    {
        Logger.Write(e.Message);
        return ds;
    }
}

There were 2 things I needed to resolve this:

First download the Microsoft.SqlServer.Types from the NuGet manager.

I then needed to manually add the dependency into the App.config in the root project directory

  <dependentAssembly>
      <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-14.0.0.0" newVersion="14.0.0.0" />
  </dependentAssembly>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM