Midrange News for the IBM i Community


Posted by: clbirk
how to exchange info without using odbc
has no ratings.
Published: 14 Dec 2013
Revised: 17 Dec 2013 - 3781 days ago
Last viewed on: 24 Apr 2024 (4467 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

how to exchange info without using odbc Published by: clbirk on 14 Dec 2013 view comments(3)

Recently I gave an hour talk at our local ibmi user group and it was titled "Every thing I need to know, I learned by Kindergarten". Well not exactly true but having grown up with computers since the 1960's forward, by the time I hit college, I was pretty fluent.

Anyhow I won't bore you with those details but I want to present to you an alternate way of giving access to other systems without using odbc or ole db, etc. I presented this as one of many ideas.  I have a c# program that runs on a pc that grabs files and sends them to one of our two large digital presses depending on what the press operator selects. We need to check the ibmi to see if the order has been put on hold when we make this scan as sometimes c/s will stop an order. While I could have muddled through the stuff in c# to do the odbc connection, I choose a much simplier approach.

That approach was a web service, the c# program calls the web service which then the ibmi responds. They were quite simple to add to the c# program and a very simple php (or it could have been rpg (cgi)) program to write. So maybe you have to give information to another server, etc. and you really don't want to open up the ibmi wide open or want to go through the hassles of such on the other end. I do have our scales running odbc to the ibmi and it works great (reads and writes) but I didn't want to figure out the c# odbc stuff here as it has been a few years and I have php running on my box.

here is what the php looks like (I was only needing a simple response but I could have passed back other values or xml, etc.

Here is the php code. It is done as a GET (that is, the variable and value are in
the url like www.xyz.com?file=1234.php) instead of POST but I could have did
either.
<?
require ('connect_db2.php'); //connect to database
$pdfname = $_GET['file'];
$sql="SELECT * FROM CUSTSRV/STOPPDF WHERE PDFNAME ='$pdfname'";
$query = db2_exec($conn, $sql);
if(!$query) {
echo "Error code: " . db2_stmt_error();
echo "Error message: " . db2_stmt_errormsg();
}
else {
$values = db2_fetch_array($query);
if (strlen(trim($values[0])) != 0)
echo "BAD";
else
echo "OK";
}
?>
and connect_db2.php could be as simple as:
<?
$user='xxx';
$pw='xxxxxx';
$options = array('i5_naming' => DB2_I5_NAMING_ON );
$conn = db2_pconnect("*LOCAL","$user","$pw", $options); // note pconnect (persistent
connection)
if (!$conn) {
die;
}
?>

and the c# code:

There is a multitude of ways to call a webservice in c#, but below is simple and to the point... so
urls is the url I call with the pdf name (strCopy).


string urls = "http://192.168.1.203:10088/executive/stoppdf.php?file=" + strCopy;
HttpWebRequest request = WebRequest.Create(urls) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string answer = reader.ReadToEnd();

if (answer != "OK")
{
MessageBox.Show("this pdf is on an order that is marked as stopped", answer,
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
}

While this one seeks info, you could use the same sort of code to update something in an ibm i db
table again by calling a php webservice and taking the information and updating a table (instead of
reading it). In the above, if I don't get back the word OK, then we pop up a message box. (and
you will see the error at the top,see word BAD, had it been some database connectivity issue, etc.
it would have appeared there also and yes truly that isn't a "stop" issue so I could modify my
messages but you get the idea and I never have db connectivity issues).

It is fast. Persistent db connections in php are super fast over regular connections.  

We call from the ibmi other web services like credit card validations, etc. no reason the i can't be a hub that other servers within call us. We have to play with everyone else, because when we do, the world realizes that we are not our own little thing that is hard to work with. Gone are the days of twinax, bysnch, APPC.

just a thought. (by the way, works slick and is fast, etc.).

chris

 

 

 

 

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: Ringer
Premium member *
Comment on: how to exchange info without using odbc
Posted: 10 years 4 months 8 days 22 hours 29 minutes ago
Edited: Tue, 17 Dec, 2013 at 15:16:12 (3781 days ago)

Well, I do like your enthusiasm and attitude. And FYI, you left yourself wide open to SQL injection.

For example if $pdfname contains 1' or 1=1 -- 

which becomes WHERE PDFNAME ='1' or 1=1 -- COMMENTS NOW and returns ALL rows. 

Try db2_prepare, db2_bind_param, and db2_execute instead. 

Chris Ringer

Posted by: clbirk
Premium member *
Comment on: how to exchange info without using odbc
Posted: 10 years 4 months 8 days 19 hours 57 minutes ago

True, I was showing a simple example and in this case and yes you are 100% correct.

 

Posted by: Ringer
Premium member *
Comment on: how to exchange info without using odbc
Posted: 10 years 4 months 8 days 19 hours 12 minutes ago

No problem. I was just letting the next future reader know. Thanks!