Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "Unable to load DLL 'SqlServerSpatial110.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
- PM> Install-Package Microsoft.SqlServer.Types
- SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr LoadLibrary(string libname);
- private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
- {
- var path = Path.Combine(nativeBinaryPath, assemblyName);
- if (!File.Exists(path))
- {
- throw new FileNotFoundException($"{path} not found");
- }
- var ptr = LoadLibrary(path);
- if (ptr == IntPtr.Zero)
- {
- throw new Exception(string.Format(
- "Error loading {0} (ErrorCode: {1})",
- assemblyName,
- Marshal.GetLastWin32Error()));
- }
- }
- public static void LoadNativeAssembliesv13(string rootApplicationPath)
- {
- var nativeBinaryPath = Environment.Is64BitProcess
- ? Path.Combine(rootApplicationPath, @"SqlServerTypesx64")
- : Path.Combine(rootApplicationPath, @"SqlServerTypesx86");
- LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll");
- LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial130.dll");
- }
- Utilities.LoadNativeAssembliesv13(Environment.CurrentDirectory); //WPF
- Utilities.LoadNativeAssembliesv13(HttpRuntime.BinDirectory); //ASP.NET
- private List<SqlGeometry> SelectGeometries(string connectionString)
- {
- SqlConnection connection = new SqlConnection(connectionString);
- var command = new SqlCommand(select shapeCol from MyTable, connection);
- connection.Open();
- List<SqlGeometry> geometries = new List<SqlGeometry>();
- SqlDataReader reader = command.ExecuteReader();
- if (!reader.HasRows)
- {
- return new List<SqlGeometry>();
- }
- while (reader.Read())
- {
- //approach 1: using WKB. 4100-4200 ms for hundred thousands of records
- //geometries.Add(SqlGeometry.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes((byte[])reader[0]), srid).MakeValid());
- //approach 2: using WKB. 3220 ms for hundred thousands of records
- //geometries.Add(SqlGeometry.Deserialize(reader.GetSqlBytes(0)));
- //approach 3: exception occur if you forget proper assembly redirection. 2565 ms for hundred thousands of records
- geometries.Add((SqlGeometry)reader[0]);
- }
- connection.Close();
- return geometries;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement