Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

MSSQL: CLR-Funktion

Empfohlene Antworten

Veröffentlicht

Hallo zusammen,

im Rahmen meines Abschlussprojekts muss ich CLR-Funktionen für den MSSQL-Server programmieren.

Nun stehe ich vor dem Problem, das ich innerhalb einer Funktion auf denselben Server zugreifen möchte.

Was ich dazu im Internet gefunden habe, wäre:


using(SqlConnection connection = new SqlConnection("context connection=true")) 

{

    connection.Open();

    // Use the connection

}
Allerdings gibt mir das immer ein
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
zurück. Habe jetzt erstmal eine kleine Testfunktion geschrieben, damit ich nicht immer alles Komipilieren muss. Testfunktion:
     

[Microsoft.SqlServer.Server.SqlFunction(DataAccess = Microsoft.SqlServer.Server.DataAccessKind.Read)]

    public static string test()

    {

        using (SqlConnection conn = new SqlConnection("context connection=true"))

        {

            //create a command from the current context

            SqlCommand cmd = conn.CreateCommand();


            //execute something

            cmd.CommandText = "select @@version";


            conn.Open();


            return (string)cmd.ExecuteScalar();

        }

    }

Fehlermeldung:

Meldung 6522, Ebene 16, Status 1, Prozedur test, Zeile 0

.NET Framework-Fehler beim Ausführen der benutzerdefinierten Routine oder des benutzerdefinierten Aggregats 'test':

System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

System.NullReferenceException:

bei System.Data.SqlServer.Internal.ClrLevelContext.GetCurrentContextForLobAccess(CClrLobContext* pLobContext)

bei System.Data.SqlServer.Internal.ClrLevelContext.GetXvarWlobStream(CXVariantBase* pxvarSource, XvarLOBStreamInitCode eCode, Int64 lcid, SqlCompareOptions compareOpts, CClrLobContext* pLobContext)

.

Würde mich über jegliche Lösungsansätze freuen.

LG

EDIT: Der Assembly wird mithilfe von C# programmiert.

Bearbeitet von Serear

  • Autor

Habe den Code jetzt mal abgeändert in

    

 [Microsoft.SqlServer.Server.SqlFunction(DataAccess = Microsoft.SqlServer.Server.DataAccessKind.Read)]

    public static int test()

    {

        using (SqlConnection conn = new SqlConnection("context connection=true"))

        {

            //create a command from the current context

            SqlCommand cmd = conn.CreateCommand();


            //execute something

            cmd.CommandText = "select count(*) from Mitarbeiter";


            conn.Open();

            int i = (int)cmd.ExecuteScalar();

            i++;

            return i;

        }

    }

Was im ungefähren dem entspricht, was ich den auch vor habe.

Ich möchte mit den Daten innerhalb dieser Funktion weiter arbeiten.

Dies läuft glatt durch, nur sehe ich momentan absolut nicht, warum es beim anderen nicht geklappt hat.

Bin davon ausgegangen das "System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. " sich auf die Context-Verbindung bezogen hat.

Lg

  • Autor

Moin,

erstmal Danke für deine Antwort.

Glaube allerdings nicht das es dadran lag, da die eigentliche Ausführung des SQL-Statements auch in meinem Code erst nach dem Connection.Open() erfolgte.

Lg

Malte

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.