Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. "Unable to load DLL 'SqlServerSpatial110.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
  2.  
  3. PM> Install-Package Microsoft.SqlServer.Types
  4.  
  5. SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
  6.  
  7. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  8. private static extern IntPtr LoadLibrary(string libname);
  9.  
  10. private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
  11. {
  12. var path = Path.Combine(nativeBinaryPath, assemblyName);
  13.  
  14. if (!File.Exists(path))
  15. {
  16. throw new FileNotFoundException($"{path} not found");
  17. }
  18.  
  19. var ptr = LoadLibrary(path);
  20. if (ptr == IntPtr.Zero)
  21. {
  22. throw new Exception(string.Format(
  23. "Error loading {0} (ErrorCode: {1})",
  24. assemblyName,
  25. Marshal.GetLastWin32Error()));
  26. }
  27. }
  28.  
  29. public static void LoadNativeAssembliesv13(string rootApplicationPath)
  30. {
  31. var nativeBinaryPath = Environment.Is64BitProcess
  32. ? Path.Combine(rootApplicationPath, @"SqlServerTypesx64")
  33. : Path.Combine(rootApplicationPath, @"SqlServerTypesx86");
  34.  
  35. LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll");
  36. LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial130.dll");
  37. }
  38.  
  39. Utilities.LoadNativeAssembliesv13(Environment.CurrentDirectory); //WPF
  40. Utilities.LoadNativeAssembliesv13(HttpRuntime.BinDirectory); //ASP.NET
  41.  
  42. private List<SqlGeometry> SelectGeometries(string connectionString)
  43. {
  44. SqlConnection connection = new SqlConnection(connectionString);
  45. var command = new SqlCommand(select shapeCol from MyTable, connection);
  46. connection.Open();
  47. List<SqlGeometry> geometries = new List<SqlGeometry>();
  48. SqlDataReader reader = command.ExecuteReader();
  49. if (!reader.HasRows)
  50. {
  51. return new List<SqlGeometry>();
  52. }
  53. while (reader.Read())
  54. {
  55. //approach 1: using WKB. 4100-4200 ms for hundred thousands of records
  56. //geometries.Add(SqlGeometry.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes((byte[])reader[0]), srid).MakeValid());
  57. //approach 2: using WKB. 3220 ms for hundred thousands of records
  58. //geometries.Add(SqlGeometry.Deserialize(reader.GetSqlBytes(0)));
  59. //approach 3: exception occur if you forget proper assembly redirection. 2565 ms for hundred thousands of records
  60. geometries.Add((SqlGeometry)reader[0]);
  61. }
  62. connection.Close();
  63. return geometries;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement