none
Wie kann ich das Connection Pooling beim Einsatz von OeDb nutzen? RRS feed

  • Frage

  • Ich habe eine .NET core 5 Web API und nutze für eine Datenbankverbindung mit Stored Procedures OleDb
    Nun möchte ich einen Connection Pool benutzen und ich suche mich dumm und dusselig im Netz.
    Wie kann ich meinen Code umbauen zu einen Connection Pool? Wo gibt es Beispiel-Code?
    Unten hänge ich mal meinen aktuellen Stand an.
    Bin glücklich über Links oder Code! Danke!

    (Ich bilde nur den wesentlichen Code ab, es fehlen Zeilen wie try/catch, Logging etc.)

        [ApiController]
        public class PartInformationsController : ControllerBase
        {
            [HttpGet("{partId}")]
            public ActionResult<PartDto> PartInformations(string partId)
            {
                Part part;
                IDatabaseConnection databaseConnection = GetDatabaseConnectionInstance();
                bool isConnect = databaseConnection.Connect();
                if (isConnect)
                {
                    IPartInformations partInformations = GetPartInformationsInstance();
                    DataSet ds = partInformations.Get(partId);
                    part = FillPartObject(ds.Tables[0].Rows);
                    return PartToDto(part);
                }
            }
        }

        public class DatabaseConnection : IDatabaseConnection
        {
            private readonly DatabaseConfig dbConfig;
            internal static OleDbConnection oleDbConnection;
    
            public DatabaseConnection(DatabaseConfig dbConfig)
            {
                this.dbConfig = dbConfig; // <-- DI
            }
    
            public bool Connect()
            {
                Boolean isConnected = false;
                oleDbConnection = new OleDbConnection(null);
                string connectionString = @"Provider=SQLOLEDB;Data Source=" + dbConfig.Server
                    + ";Initial Catalog=" + dbConfig.Db
                    + ";User ID=" + dbConfig.User
                    + ";Password=" + dbConfig.Password;
                oleDbConnection = new OleDbConnection(connectionString);
                oleDbConnection.Open();
                isConnected = true;
                return isConnected;
            }
    
            public void Disconnect()
            {
                oleDbConnection.Close();
            }
        }

        public class PartInformations : IPartInformations
        {
            protected readonly IServiceProvider serviceProvider;
    
            public PartInformations(IServiceProvider serviceProvider)
            {
                this.serviceProvider = serviceProvider; // <- DI
            }
    
            public DataSet Get(string sessionId, int userId, string partNumber)
            {
                string ssql = "pp_sp_PROJECT_Get_Partnumber";
                OleDbCommand sqlCMD = new OleDbCommand(ssql, DatabaseConnection.DatabaseConnection.oleDbConnection);
                sqlCMD.CommandType = CommandType.StoredProcedure;
    
                sqlCMD.Parameters.AddWithValue("@SSessionID", sessionId);
                sqlCMD.Parameters.AddWithValue("@NUserID", userId);
                sqlCMD.Parameters.AddWithValue("@SPartnumber", partNumber);
    
                OleDbDataAdapter MyData = new OleDbDataAdapter(sqlCMD);
                DataSet ds = new DataSet();
                MyData.Fill(ds);
                return ds;
            }
        }

    Freitag, 5. Februar 2021 19:59

Antworten

  • Hallo Frank,
    das soll im Connectionstring geschehen:

    https://www.visualbasicplanet.info/client-development/configuring-connection-pooling-with-ole-db-connections.html

    The following connection strings explicitly enable connection pooling by setting the OLE DB Services keyword to -1.
    OLE DB connection string for an Office Access database (assumes the Nwind.mdb file exists in the following path: C:\DataSources\Nwind.mdb):
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataSources\Nwind.mdb; OLE DB Services=-1

    HTH
    Grüße Alexander

    • Als Antwort markiert frank me Samstag, 6. Februar 2021 10:45
    Freitag, 5. Februar 2021 20:39

Alle Antworten

  • Hallo Frank,
    das soll im Connectionstring geschehen:

    https://www.visualbasicplanet.info/client-development/configuring-connection-pooling-with-ole-db-connections.html

    The following connection strings explicitly enable connection pooling by setting the OLE DB Services keyword to -1.
    OLE DB connection string for an Office Access database (assumes the Nwind.mdb file exists in the following path: C:\DataSources\Nwind.mdb):
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DataSources\Nwind.mdb; OLE DB Services=-1

    HTH
    Grüße Alexander

    • Als Antwort markiert frank me Samstag, 6. Februar 2021 10:45
    Freitag, 5. Februar 2021 20:39
  • Danke Alexander!
    Samstag, 6. Februar 2021 10:45