Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Microsoft.Data.SqlClient not working.#2307

Unanswered
mateli asked this question inQ&A
Jan 16, 2024· 1 comments· 3 replies
Discussion options

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.12.0
  • Operating System: Windows 11
  • .NET Runtime: 8.0.0

Details

from pythonnet import load
load("coreclr")
import clr
clr.AddReference(r"Microsoft.Data.SqlClient.dll")
from Microsoft.Data.SqlClient import SqlConnection
try:
conn = SqlConnection("")
except Exception as e:
print(e)
print (System.Environment.StackTrace)
exit()

Output:
Microsoft.Data.SqlClient is not supported on this platform.
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeConstructorWithoutAlloc(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Environment.get_StackTrace()
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at Python.Runtime.PropertyObject.tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp) in /tmp/build-via-sdist-h3j67_wc/pythonnet-3.0.3/src/runtime/Types/PropertyObject.cs:line 65

More info:
Various online sources suggest that failure to find "Microsoft.Data.SqlClient.SNI.dll" (native part of the implementation) is the root cause of this failure. However putting this dll in the path or manually loading it seems to make no difference. Either it is manually loaded by SqlClient in a weird way or something else is causing this error.

Another suggested solution is to use a very old version of SqlClient or the System.Data.SqlClient and neither are good options as they are obsolete and unreliable.

Also adding a non-empty connection string doesn't matter because SqlClient do not seem to get to the point where this is processed.

You must be logged in to vote

Replies: 1 comment 3 replies

Comment options

Ran into this also. Through trial and error, I copied DLLs from a build output in the following order:

  1. Copy all DLLs from\bin\Release\net8.0 (this includedMicrosoft.Data.SqlClient.dll).

    ErrorSystem.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.

  2. Copy Microsoft.Data.SqlClient.dll and the other DLLs from following location and replace the existing one from\bin\Release\net8.0\runtimes\win\lib\net8.0

    Error:System.DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)

  3. Copy Microsoft.Data.SqlClient.SNI.dll from:bin\Release\net8.0\runtimes\win-x64\native

Worked!

You must be logged in to vote
3 replies
@mateli
Comment options

You copied them where?

@geographika
Comment options

I had a DLL with a reference toMicrosoft.Data.SqlClient.dll, and had the same error as you. I wasn't trying to useMicrosoft.Data.SqlClient.dll directly though. I copied my DLL and all dependent ones into a single folder and added that tosys.path.

import sysfrom pythonnet import loadload("coreclr")import clrsys.path.append("/folder/with/dlls") # My.dll and all dependent DLLsclr.AddReference("My.dll")

image

@vfofanov
Comment options

@geographika Thank you a lot! You save my day))

I prepared MSBuild targets filePrepareRuntimes.targets for automate this operation. This.targets specific fornet8 ornet9 andMicrosoft.Data.SqlClient Version=5.2.2. The difference will be inruntimes subfolders

How to use it

  1. Add the attached file near your.csproj
  2. AddImport to your.csproj(somewhere inside<Project>)
<ImportProject="$(MSBuildThisFileDirectory)..\PrepareRuntimes.targets" />
  1. Build
  2. You will findruntime-specific folder in output folder
  3. For get runtime information in Python I'm using the next script(checked only on windows x64)
importplatformdefget_runtime():system=platform.system().lower()# e.g., 'windows', 'linux', 'darwin'architecture=platform.machine().lower()# e.g., 'x86_64', 'aarch64'# based on https://stackoverflow.com/questions/45125516/possible-values-for-uname-m# based on https://en.wikipedia.org/wiki/Uname# Mapping of system and architecture to custom platform namesifsystemin ['windows','windows_nt']andarchitecture=='amd64':return'win-x64'elifsystem=='linux'andarchitecturein ['arm','arm64','aarch64_b','aarch64','armv8b','armv8l']:return'linux-arm64'elifsystem=='darwin':return'osx-arm64'elifsystem=='linux':return'linux-x64'else:return'unknown'runtime=get_runtime()print(f"The script is running on:{runtime}")
  1. Use runtime to select runtime specific subfolder
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@mateli@geographika@vfofanov
Converted from issue

This discussion was converted from issue #2306 on January 16, 2024 15:49.


[8]ページ先頭

©2009-2025 Movatter.jp