public ConnectResponse Connect(ConnectRequest request) { ConnectResponse response = new ConnectResponse(); // Overeni, zda byl zadan Connection string; if (StringUtils.IsBlank(request.ConnectionString)) { response.Error = "Blank connection string."; } else { // Otevreni ODBC. try { PooledOdbcConnection pooledOdbcConnection = OdbcConnectionPool.Instance.OdbcConnect(request.ConnectionString, request.UsingOleDb); response.ConnectionID = pooledOdbcConnection.ID; response.Success = true; response.ConnectionAutoCloseTime = pooledOdbcConnection.ConnectionAutoCloseTime; } catch (OdbcException x) { response.Error = OdbcConnectionPool.GetOdbcError(x); } catch (Exception x) { response.Error = x.ToString(); } } return(response); }
public ExecuteResponse ExecuteCommand(ExecuteRequest request) { ExecuteResponse response = new ExecuteResponse(); if (StringUtils.IsBlank(request.ConnectionID)) { response.Error = "Blank connection ID."; } else if (StringUtils.IsBlank(request.Query)) { response.Error = "Query is blank."; } else { try { PooledOdbcConnection pooledOdbcConnection = OdbcConnectionPool.Instance.GetConnection(request.ConnectionID); if (pooledOdbcConnection.OdbcConnection != null) { OdbcConnection connection = pooledOdbcConnection.OdbcConnection; using (OdbcCommand command = new OdbcCommand(request.Query, connection)) { int affectedRows = command.ExecuteNonQuery(); response.AffectedRows = affectedRows; } } else { OleDbConnection connection = pooledOdbcConnection.OleDbConnection; using (OleDbCommand command = new OleDbCommand(request.Query, connection)) { int affectedRows = command.ExecuteNonQuery(); response.AffectedRows = affectedRows; } } response.Success = true; response.ConnectionAutoCloseTime = pooledOdbcConnection.ConnectionAutoCloseTime; } catch (OdbcException x) { response.Error = OdbcConnectionPool.GetOdbcError(x); } catch (Exception x) { response.Error = x.ToString(); } } return(response); }
private void CloseConnection(PooledOdbcConnection pooledOdbcConnection) { if (pooledOdbcConnection.OdbcConnection != null) { pooledOdbcConnection.OdbcConnection.Close(); } if (pooledOdbcConnection.OleDbConnection != null) { pooledOdbcConnection.OleDbConnection.Close(); } }
public void CloseConnection(string connectionID) { lock (olock) { if (connections.ContainsKey(connectionID)) { PooledOdbcConnection pooledOdbcConnection = connections[connectionID]; connections.Remove(connectionID); CloseConnection(pooledOdbcConnection); } else { throw new ArgumentException("Connection ID (" + connectionID + ") not found in connection pool."); } } }
public PooledOdbcConnection OdbcConnect(string connectionString, bool usingOleDb) { PooledOdbcConnection pooledOdbcConnection = null; OdbcConnection odbcConnection = null; OleDbConnection oleDbConnection = null; if (usingOleDb) { oleDbConnection = new OleDbConnection(connectionString); //oleDbConnection.ConnectionString = connectionString; oleDbConnection.Open(); } else { odbcConnection = new OdbcConnection(connectionString); //odbcConnection.ConnectionString = connectionString; odbcConnection.Open(); } lock (olock) { string connectionID = Guid.NewGuid().ToString(); while (connections.ContainsKey(connectionID)) { connectionID = Guid.NewGuid().ToString(); } DateTime connectionAutoCloseTime = DateTime.Now.AddMinutes(5); pooledOdbcConnection = new PooledOdbcConnection { ID = connectionID, OdbcConnection = odbcConnection, OleDbConnection = oleDbConnection, ConnectionAutoCloseTime = connectionAutoCloseTime }; connections.Add(connectionID, pooledOdbcConnection); } return(pooledOdbcConnection); }
public PooledOdbcConnection GetConnection(string connectionID) { PooledOdbcConnection connection = null; lock (olock) { if (connections.ContainsKey(connectionID)) { connection = connections[connectionID]; DateTime connectionAutoCloseTime = DateTime.Now.AddMinutes(5); connection.ConnectionAutoCloseTime = connectionAutoCloseTime; } else { throw new ArgumentException("Connection ID (" + connectionID + ") not found in connection pool."); } } return(connection); }
public SelectResponse Select(SelectRequest request) { SelectResponse response = new SelectResponse(); if (StringUtils.IsBlank(request.ConnectionID)) { response.Error = "Blank connection ID."; } else if (StringUtils.IsBlank(request.Query)) { response.Error = "Query is blank."; } else { try { PooledOdbcConnection pooledOdbcConnection = OdbcConnectionPool.Instance.GetConnection(request.ConnectionID); List <RowData> rows = new List <RowData>(); response.Rows = rows; if (pooledOdbcConnection.OdbcConnection != null) { OdbcConnection connection = pooledOdbcConnection.OdbcConnection; using (OdbcCommand command = new OdbcCommand(request.Query, connection)) { using (OdbcDataReader reader = command.ExecuteReader(CommandBehavior.Default | CommandBehavior.KeyInfo)) { if (request.ReturnSchemaTable) { response.SchemaTable = getSchemaTable(reader); } while (reader.Read()) { RowData rowData = new RowData(); for (int i = 0; i < reader.FieldCount; i++) { rowData.Add(reader.GetName(i), getColumnValue(reader, i)); } rows.Add(rowData); } } } } else { OleDbConnection connection = pooledOdbcConnection.OleDbConnection; using (OleDbCommand command = new OleDbCommand(request.Query, connection)) { using (OleDbDataReader reader = command.ExecuteReader(CommandBehavior.Default | CommandBehavior.KeyInfo)) { if (request.ReturnSchemaTable) { response.SchemaTable = getSchemaTable(reader); } while (reader.Read()) { RowData rowData = new RowData(); for (int i = 0; i < reader.FieldCount; i++) { rowData.Add(reader.GetName(i), getColumnValue(reader, i)); } rows.Add(rowData); } } } } response.Success = true; response.ConnectionAutoCloseTime = pooledOdbcConnection.ConnectionAutoCloseTime; } catch (OdbcException x) { response.Error = OdbcConnectionPool.GetOdbcError(x); } catch (Exception x) { response.Error = x.ToString(); } } return(response); }