For example in my last project i had to work with a lot of data sources as SQL Server, MySQL and Access all together.
All those above are data sources which have connectors for ODBC.
This means we can deal with all DBMS (and much more) simply installing ODBC connectors in the robot machine and using only this VBO to deal with that.
The key is to pass the driver info to the VBO so connection string could be composed.
So this are the VBO actions i created to deal with query.
Read in collection
The action you need the most.Using the SqlCommand variable you can also do something more than "Select * from table" but you can also filter before getting the data.
The first code block is faster but will not work with lot of data, and the second one will be slower but it will work everytime.
Pay attention kiddo, BP passes parameter by value and not by reference, so you could need to do the "business work" in the VBO action, in order not to go "Out of memory".
OdbcConnection connection = new OdbcConnection(connectionString); result = new DataTable(); exception = ""; try { connection.Open(); OdbcCommand command = new OdbcCommand(sqlCommand, connection); OdbcDataReader dataReader = command.ExecuteReader(); result.Load(dataReader); } catch (Exception ex) { exception = ex.Message; } finally { connection.Close(); }
And this is the "big data" one.
OdbcConnection connection = new OdbcConnection(connectionString); result = new DataTable(); exception = ""; try { connection.Open(); OdbcCommand command = new OdbcCommand(sqlCommand, connection); OdbcDataReader dataReader = command.ExecuteReader(); DataTable resultSchema = dataReader.GetSchemaTable(); ListlistCols = new List (); if (resultSchema != null) { foreach (DataRow drow in resultSchema.Rows) { string columnName = System.Convert.ToString(drow["ColumnName"]); DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"])); column.Unique = (bool)drow["IsUnique"]; column.AllowDBNull = (bool)drow["AllowDBNull"]; column.AutoIncrement = (bool)drow["IsAutoIncrement"]; listCols.Add(column); result.Columns.Add(column); } } DataRow dataRow = null; while (dataReader.Read()) { dataRow = result.NewRow(); for (int i = 0; i < listCols.Count; i++) { dataRow[((DataColumn)listCols[i])] = dataReader[i]; } result.Rows.Add(dataRow); } } catch (Exception ex) { exception = ex.Message; } finally { connection.Close(); System.GC.Collect(); }
Execute Command
OdbcConnection connection = new OdbcConnection(connectionString); exception = ""; try { connection.Open(); OdbcCommand command = new OdbcCommand(sqlCommand, connection); command.ExecuteNonQuery(); } catch (Exception ex) { exception = ex.Message; } finally { connection.Close(); }
Execute Commands in a transaction
OdbcConnection connection = new OdbcConnection(connectionString); OdbcTransaction transaction = null; exception = ""; try { connection.Open(); transaction = connection.BeginTransaction(); OdbcCommand command = null; foreach (DataRow dr in sqlCommands.Rows) { string sqlCommand = dr["Command"].ToString(); command = new OdbcCommand(sqlCommand, connection, transaction); command.ExecuteNonQuery(); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); exception = ex.Message; } finally { connection.Close(); }
Changing the ConnectionString will just change the DataSource type and help creating a "universal" repository.
And... that's it for now!
Great Article - very informative
ReplyDeleteThank you Ian :)
DeleteHi great article :) I'm trying to do the same with HANA database and the problem is the Dll that I'm using. I tested with 32 and 64 bits but I always get the same error "lilibSQLDBCHDB.dll' could not be opened -- 'An attempt was made to load a program with an incorrect format. '"
ReplyDeletehow did you install the connectors to your robots?
thanks
Hi, thank you. I think a good strategy could be to connect to this DB using a ODBC connector (https://help.sap.com/viewer/52715f71adba4aaeb480d946c742d1f6/2.0.00/en-US/66a4169b84b2466892e1af9781049836.html). It seems SAP HANA support that, so you will just use the driver, no need to install it into the BP root.
DeleteHi Valerio,
DeleteThanks for the help. as you comment the problem was the connection String. this is the correct one DRIVER={HDBODBC};UID=myUser;PWD=myPassword;SERVERNODE=myServer:30015 with this Blue Prism is able to connect and doing sql query :)
thank u
Great!
DeleteHi liltawa, were you able to fix this issue? When I run the same String Connection I'm facing a Privode error message. How can it be fixed? Thanks
ReplyDelete