【问题标题】:SQL Insert DataSQL 插入数据
【发布时间】:2011-06-24 18:01:01
【问题描述】:

我正在尝试将要素数据 - 多边形插入 SQL 2008。ID-type:nvarchar, geom=>type:geometry

这是我的代码:

foreach (FeatureDataRow dsRow in ds.Tables[0])
{
  string ID = (string)dsRow["Name"];
  SharpMap.Geometries.Polygon geom = (SharpMap.Geometries.Polygon)dsRow.Geometry;  

   SqlConnection con = new SqlConnection(MapHelper.GetSQLConnectionString());
   string cmdStr = "INSERT INTO table1 (ID, geom) VALUES (@ID, geometry::STGeomFromWKB(@geom))";
   SqlCommand cmd = new SqlCommand(cmdStr, con);

   //Store parameters with values to the collection
   cmd.Parameters.AddWithValue("ID", ID);
   cmd.Parameters.AddWithValue("geom", geom); //.DBType ???

   con.Open();
   cmd.ExecuteNonQuery();
  con.Close();
}

我在 cmd.ExecuteQuery 上收到错误:不存在从对象类型 SharpMap.Geometries.Polygon 到已知托管提供程序本机类型的映射

我不知道为 geom 参数为 .DBTYPE 设置什么。

【问题讨论】:

    标签: c# asp.net sql ado.net


    【解决方案1】:

    根据这里:http://msdn.microsoft.com/en-us/library/bb933882(v=SQL.105).aspx

    听起来类型是 varbinary(max)。这意味着 DbType.VarBinary

    当然,我还没有使用过空间类型。

    【讨论】:

      【解决方案2】:

      您可以将代码更改为:

      cmd.Parameters.AddWithValue("geom", geom.AsBinary());

      SharpMap 中的所有几何对象都实现了 AsBinary() 和 AsText() 方法,这将返回几何的二进制/文本表示。在您的插入语句中,您使用的是geometry::STGeomFromWKB(),它需要二进制输入。因此,将 AsBinary 方法返回的字节数组作为参数值传递。

      DbType 显然是 VarBinary。

      【讨论】:

        【解决方案3】:

        您应该使用geometry::STGeomFromWKB() 采用的参数类型。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-24
          • 1970-01-01
          • 2017-10-31
          • 1970-01-01
          • 1970-01-01
          • 2017-10-21
          相关资源
          最近更新 更多