combit List & Label 27 - .NET Hilfe
combit.Reporting.DataProviders Namespace / DbConnectionDataProvider Klasse
Member Beispiele


In diesem Thema
    DbConnectionDataProvider Klasse
    In diesem Thema

    Abstrakte Basisklasse für Datenprovider, die auf Daten einer IDbConnection zugreifen. Die konkreten Implementierungen in dieser Assembly sind die OleDbConnectionDataProvider Klasse, die OracleConnectionDataProvider Klasse und die SqlConnectionDataProvider Klasse. Sie können Ihre eigenen spezifischen Ableitungen hinzufügen, die IDbConnection verwenden, wenn Sie Zugriff auf die Datenbank-Schemainformationen für Ihren .NET Datenprovider haben (Tabellen und Relationen). Diese Klasse ist serialisierbar.

    Um Ihre eigene Ableitung zu schreiben, überschreiben Sie die Init() Methode und übergeben alle Tabellen und Relationen an das protected Provider Member. Beachten Sie dazu das untenstehende Beispiel, um zu sehen wie dies in der SqlConnectionDataProvider Klasse gemacht wird. Solange Ihr Provider konform zu ISO SQL ist, sollten es nicht erforderlich sein weitere Änderungen vorzunehmen, die Basisklasse kümmert sich automatisch um das gesamte SQL Handling.

    Überschreiben Sie die OnTranslateFilterSyntax Methode um das standardmäßige Verhalten für die Filtersyntax-Übersetzung zu ändern. Beachten Sie dazu das IAdvancedFiltering Interface für Details und beachten Sie ebenfalls das untenstehende Beispiel, um zu sehen wie diese Methode in der OracleConnectionDataProvider Klasse überschrieben wird. In diesem Fall wird nur die Behandlung der SUBSTRING SQL Funktion geändert, da der Name dieser Funktion in Oracle SUBSTR ist.

    Objektmodell
    DbConnectionDataProvider Klasse
    Syntax
    'Deklaration
     
    
    Public MustInherit Class DbConnectionDataProvider 
       Implements ICanHandleUsedIdentifiers, IDataProvider, combit.Reporting.ISupportsLogger 
    public abstract class DbConnectionDataProvider : ICanHandleUsedIdentifiers, IDataProvider, combit.Reporting.ISupportsLogger  
    public ref class DbConnectionDataProvider abstract : public ICanHandleUsedIdentifiers, IDataProvider, combit.Reporting.ISupportsLogger  
    Beispiele
    protected override void Init()
    {
        if (Initialized)
            return;
    
        List<String> passedRelationNames = new List<string>();
    
        Connection.Open();
        try
        {
            DataTable dt = (Connection as SqlConnection).GetSchema("Tables");
    
            foreach (DataRow dr in dt.Rows)
            {
                string tableSchema = dr["TABLE_SCHEMA"].ToString();
                string tableType = dr["TABLE_TYPE"].ToString();
                string parentTableName = dr["TABLE_NAME"].ToString();          
                switch (tableType)
                {
                    case "BASE TABLE":
                        if ((SupportedElementTypes & DbConnectionElementTypes.Table) == 0)
                            continue;
                        break;
                    case "VIEW":
                        if ((SupportedElementTypes & DbConnectionElementTypes.View) == 0)
                            continue;
                        break;
                    default:
                        continue;
                }
    
                // pass table
                SqlConnection newConnection = DbCommandSetDataProviderHelper.CloneConnection(Connection) as SqlConnection;
                SqlCommand cmd = new SqlCommand("Select * From \"" + (String.IsNullOrEmpty(tableSchema) ? parentTableName+"\"" : tableSchema + "\".\"" + parentTableName) + "\"", newConnection);
                Provider.AddCommand(cmd as IDbCommand, parentTableName);
    
                // pass relations
                string commandText = "sp_fkeys @pktable_name = '" + parentTableName + "'";
                if (!String.IsNullOrEmpty(tableSchema))
                {
                    commandText+= ", @pktable_owner = '"+tableSchema+ "'";
                }
                cmd = new SqlCommand(commandText, Connection as SqlConnection);
                SqlDataReader reader = cmd.ExecuteReader();
                
                while (reader.Read())
                {
                    string childColumnName = reader.GetSqlString(7).ToString();
                    string parentColumnName = reader.GetSqlString(3).ToString();
    
                    // combined primary key, add key field to last relation on stack
                    if (reader.GetInt16(8) > 1)
                    {
                        IDataProvider providerInterface = (Provider as IDataProvider);
                        DbCommandTableRelation lastRelation = providerInterface.Relations[providerInterface.Relations.Count - 1] as DbCommandTableRelation;
                        lastRelation.ChildColumnName += '\t' + childColumnName;
                        lastRelation.ParentColumnName += '\t' + parentColumnName;
                        continue;
                    }
                    string childTableName = reader.GetSqlString(6).ToString();
                    string relName = reader.GetSqlString(2).ToString() + "2" + childTableName;
                    int relationIndex = 1;
                    string formatString = relName+"{0}";
    
                    while (passedRelationNames.Contains(relName))
                    {
                        relName = String.Format(formatString, relationIndex);
                        relationIndex++;
                    }
                    passedRelationNames.Add(relName);
                    Provider.AddRelation(relName, parentTableName, childTableName, parentColumnName, childColumnName);                        
                }
                reader.Close();
            }
        }
        finally
        {
            Connection.Close();
            Initialized = true;
        }
    }
    Protected Overloads Overrides Sub Init()
        If Initialized Then
            Return
        End If
    
        Dim passedRelationNames As List(Of [String]) = New List(Of String)()
    
        Connection.Open()
        Try
            Dim dt As DataTable = (TryCast(Connection, SqlConnection)).GetSchema("Tables")
    
            For Each dr As DataRow In dt.Rows
                Dim tableSchema As String = dr("TABLE_SCHEMA").ToString()
                Dim tableType As String = dr("TABLE_TYPE").ToString()
                Dim parentTableName As String = dr("TABLE_NAME").ToString()
    
                Select Case tableType
                    Case "BASE TABLE"
                        If (SupportedElementTypes And DbConnectionElementTypes.Table) = 0 Then
                            Continue Select
                        End If
                        Exit Select
                    Case "VIEW"
                        If (SupportedElementTypes And DbConnectionElementTypes.View) = 0 Then
                            Continue Select
                        End If
                        Exit Select
                    Case Else
                        Continue Select
                End Select
    
                ' pass table
                Dim newConnection As SqlConnection = TryCast(DbCommandSetDataProviderHelper.CloneConnection(Connection), SqlConnection)
                Dim cmd As New SqlCommand("Select * From """ + (If([String].IsNullOrEmpty(tableSchema), parentTableName + """", tableSchema + """.""" + parentTableName)) + """", newConnection)
                Provider.AddCommand(TryCast(cmd, IDbCommand), parentTableName)
    
                ' pass relations
                Dim commandText As String = "sp_fkeys @pktable_name = '" + parentTableName + "'"
                If Not [String].IsNullOrEmpty(tableSchema) Then
                    commandText += ", @pktable_owner = '" + tableSchema + "'"
                End If
    
                cmd = New SqlCommand(commandText, TryCast(Connection, SqlConnection))
                Dim reader As SqlDataReader = cmd.ExecuteReader()
    
                While reader.Read()
                    Dim childColumnName As String = reader.GetSqlString(7).ToString()
                    Dim parentColumnName As String = reader.GetSqlString(3).ToString()
    
                    ' combined primary key, add key field to last relation on stack
                    If reader.GetInt16(8) > 1 Then
                        Dim providerInterface As IDataProvider = (TryCast(Provider, IDataProvider))
                        Dim lastRelation As DbCommandTableRelation = TryCast(providerInterface.Relations(providerInterface.Relations.Count - 1), DbCommandTableRelation)
                        lastRelation.ChildColumnName += ControlChars.Tab + childColumnName
                        lastRelation.ParentColumnName += ControlChars.Tab + parentColumnName
                        Continue While
                    End If
                    Dim childTableName As String = reader.GetSqlString(6).ToString()
                    Dim relName As String = reader.GetSqlString(2).ToString() + "2" + childTableName
                    Dim relationIndex As Integer = 1
                    Dim formatString As String = relName + "{0}"
    
                    While passedRelationNames.Contains(relName)
                        relName = [String].Format(formatString, relationIndex)
                        System.Math.Max(System.Threading.Interlocked.Increment(relationIndex),relationIndex - 1)
                    End While
                    passedRelationNames.Add(relName)
                    Provider.AddRelation(relName, parentTableName, childTableName, parentColumnName, childColumnName)
                End While
                reader.Close()
            Next
        Finally
            Connection.Close()
            Initialized = True
        End Try
    End Sub
    protected override void OnTranslateFilterSyntax(object sender, TranslateFilterSyntaxEventArgs e)
    {
        switch (e.ArgumentType)
        {
            case LlExpressionArgumentType.Function:
                switch (e.Name.ToString().ToUpper())
                {
                    case "MID$":
                        if (e.ArgumentCount == 2)
                            e.Result = (String.Format("(SUBSTR({0},{1}))", e.Argument1.ToString(), e.Argument2.ToString() + "+1"));
                        else
                            e.Result = (String.Format("(SUBSTR({0},{1},{2}))", e.Argument1.ToString(), e.Argument2.ToString() + "+1", e.Argument3.ToString()));
                        break;
                    case "LEFT$":
                        e.Result = (String.Format("(SUBSTR({0},0,{1}))", e.Argument1.ToString(), e.Argument2.ToString()));
                        break;
                    case "RIGHT$":
                        e.Result = (String.Format("(SUBSTR({0},-{1},{1}))", e.Argument1.ToString(), e.Argument2.ToString()));
                        break;
                    default:
                        e.Result = null;
                        break;
                }
                break;
            default:
                e.Result = null;
                break;
        }
    }
    Vererbungshierarchie
    Anforderungen

    Plattformen: Windows 8.1 bis Windows 10 (Version 1909 - 21H2), Windows 11 (21H2), Windows Server 2012 - 2022

    Siehe auch