PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: nrh7 on August 27, 2024, 09:03:29 PM

Title: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 27, 2024, 09:03:29 PM
Greetings, I excuse myself if this is not the right forum to ask this.

One of my client has seen the power of iNomads and they gave me a request, to make iNomads panels that open, read and update a database on their Microsoft SQL Server database.

I'm VERY newbie on this topic, barely have managed to run iNomands on a local network, so this client has a desktop software made on Visual Basic 6, but they loved how easily I managed to open pvx panels on Google Chrome via iNomads.

So, they installed their SQL Server database in my laptop, I was looking at the manual and ended up reading about the ADO command but I haven't been able to connect to the SQL Server database, everything I tried gives me an error 15.

For this test I'm only trying to OPEN and CLOSE it without actually reading anything, just trying to simply open it but it's not working:
The commented lines (starting with ! ) are all the examples I've tried and failed.

SADI\SQLEXPRESS is supposed to be the server name, dbo.D_Inventory is the Table I want to connect to (open then close)

LET CHAN=HFN
! OPEN (CHAN,OPT="CONNECT='Provider=SADI\SQLEXPRESS'")"[ADO]SADI\SQLEXPRESS;dbo.D_Inventory"
! OPEN (CHAN)"[ADO]SADI\SQLEXPRESS"
! OPEN (CHAN)"[ADO]SADI\SQLEXPRESS[dbo.D_INVENTORY][]"
! OPEN (CHAN)"[ADO]SADI\SQLEXPRESS.WMPOS[dbo.D_INVENTORY][]"
! OPEN (CHAN,IOL=*)"[ tag ] SADI\SQLEXPRESS.WMPOS ; dbo.D_INVENTORY [;fileopt ]"
CLOSE (CHAN)

I know nothing about this, I also read about ODBC but I haven't even been able to learn where to activate the ODBC license, let alone use that feature.

I got this far by reading the "Microsoft SQL Server Interface" part of the pvx manual.

For now it's just OPEN CLOSE, after is, OPEN, READ (a record), WRITE (to update it) then CLOSE.


Thanks for any help, I may be able to sell a few Web and Wayfarer Licenses if I can pull this off.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: Devon Austen on August 28, 2024, 08:02:33 AM
Here is an example open using [ADO] for a SQL Server database

open (1,iol=*,opt=";USER=username;PSWD=password;Connect='Provider=SQLOLEDB;';DB=db_name;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]server_url;table_name"

Note that server_url is not the name of the SQL server but of the url of the machine it is running on. So if this is all running on the same machine right now then use localhost for the server_url.

For the table name I think you want to leave off the dbo. so just use D_INVENTORY.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 12:33:06 PM
Thanks for the info!

Playing around with the command you shared, I tested many codes but im sharing the following codes, all gave error 15:

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\Lenovo;D_INVENTORY"
! DUMP -  OSerr: HY000: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI;D_INVENTORY"
! DUMP - OSerr: HY000: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS;D_INVENTORY"
! DUMP -  OSerr: HY000: Login failed for user 'sa'.

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS;D_INVENTORY"
! DUMP -  OSerr: HY000: Login failed for user 'sa'.

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=SADI\SQLEXPRESS;KEY=key_field;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS;D_INVENTORY"
! DUMP -  OSerr: HY000: Login failed for user 'sa'.

=============================================================================

The SQL Server and PVX are on the same machine for this test.

Computer Name: SADI
Local PC User: Lenovo (no password)

SQL Server User: sa
SQL Server Pass: s123@123@

SQL Server Name: SADI\SQLEXPRESS
Database Name:  WMPOS
Table Name: D_INVENTORY

Not sure what the "key_field" is but the primary key for D_INVENTORY is PK_D_INVENTORY.

I'm still missing something  :'(


Thanks for all the help
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: Devon Austen on August 28, 2024, 12:57:59 PM
I assume what you would want based on the info provided is

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=PK_D_INVENTORY;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI;D_INVENTORY"

some of the errors you were getting were login errors so you may want to confirm user name and password.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 04:23:56 PM
Greetings

This literal code:

OPEN (CHAN,IOL=*,OPT=";USER=sa;PSWD=sa123@123@;Connect='Provider=SQLOLEDB;';DB=WMPOS;KEY=PK_D_INVENTORY;CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI;D_INVENTORY"

Still gave me the error 15 but with this dump information instead:

Error Status
 Error #15: Operating system command failed
 [Last OS err:No such file or directory]

Error Info
   Err: 15
   Erx: 150000
   Ret: 258
   OSerr: HY000: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
   Program: C:\CentralDrugInventory\test
   EntryPoint: 0
   Stno: 251
   Obj: 0
   Lfa: 61
   Lfo: 124
   LastPath: [ado]SADI;D_INVENTORY
   LastWinErr: 0
   Module: pvxado.c
   Line: 2063
   CTL: 10001

===================================================

Would this have anything to do with the ODBC License? I have yet to activate that, not sure where or how.

Or maybe it has to do with maybe the directories?

PVX is on C:\
The project is on C:\InventoryApp
and the SQL Server is installed on C:\Program Files\Microsoft SQL Server

I will check about the user and password as you mentioned though.

Thank you
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: Devon Austen on August 28, 2024, 04:36:34 PM
No when using the [ADO] interface ODBC is not used.

The error 15 means the error is coming not from PxPlus so the important error info is:

HY000: [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.

It sounds like it can't find the SQL server using the server url SADI.

I think when you used SADI\SQLEXPRESS it gaev an error about a login which means it connected and tried to login.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 04:57:04 PM
Yes it does, with SADI\SQLEXPRESS gives log in error, I will follow up with the software developer on the user/pass of sql server.


Side question, when I open the SQL Server manager it is set up so that I can use the Windows Authentication as log in method, so I use the PC name/password to connect to the database, can ADO command be used in such way too?
That it connects using my windows PC username/pass instead of the sa/sa123@123@ user?
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 05:08:55 PM
Extra info:

The developer uses this connection string on VisualBasic

MSDASQL;DRIVER={SQL Server};SERVER=EliteBox\SQLEXPRESS;trusted_connection=no;user id=sa;password=sa123@123@;database=WMPOS;

He asks me that IF I can connect to the database as a whole instead of trying to connect a specific table but that answer I do not know.

I shared his code so maybe it gives us a hint.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: martinp on August 28, 2024, 05:36:23 PM
If you don't provide a table it should connect to database itself then you can send SQL statements manually.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 06:10:49 PM
I created another user in the SQL Server called
pvx with password pvx123@123@ that is NOT an admin user like "sa"

I used the following code:
OPEN (CHAN,OPT=";USER=pvx;PSWD=pvx123@123@;Connect='Provider=SQLOLEDB';DB=WMPOS;")"[ado]SADI\SQLEXPRESS"
Then got this:
Error 15 - Dump = OSerr: HY000: Cannot open database "WMPOS" requested by the login. The login failed.

Then used:
OPEN (CHAN,OPT=";USER=pvx;PSWD=pvx123@123@;Connect='Provider=SQLOLEDB;';CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS"

It gave no error, so I'm assuming it connected correctly, I will try to read any record now and show it on screen.
Let's see how it goes.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 06:53:34 PM
This is the final product:

00041 LET CHAN=HFN
00042 MULTI_LINE READ MULTILINE.CTL,Y$
00043 OPEN (CHAN,OPT=";USER=pvx;PSWD=pvx123@123@;Connect='Provider=SQLOLEDB;';CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS"
00044 READ (CHAN,KEY="!SELECT [ITEM_ID],[ITEM_NAME],[ITEM_DESCRIPTION],[ITEM_ID_ORIGINAL],[LAST_UNIT_COST] FROM [WMPOS].[dbo].[D_INVENTORY] WHERE ITEM_ID_ORIGINAL ='"+Y$+"'")C1$,C2$,C3$,C4$,C5$
00045 PRINT C1$
00046 PRINT C2$
00047 PRINT C3$
00048 PRINT C4$
00049 PRINT C5$
00050 PRINT "========================================="
00051 CLOSE (CHAN)
00052 RETURN

It works wonderful because I input the internal code in the screen and run the code and it prints the info about the internal id.

Runs smooth!!! Finally....

However when I tried to run that same window via inomads it did not run nor it gave me any errors.

What changes do I have to make to inomads so that it can run, I use EZWeb Server on local network and google chrome.

THANKS GUYS!
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on August 28, 2024, 10:00:05 PM
Nevermind guys!
Ado is working well with chrome, its just the command "print" doesnt show anything but i can display information to a multiline or msgbox just fine.

So I guess im set for now, thanks!!!
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on September 09, 2024, 10:19:28 AM
Ok, stuck again  :o

I'm using the following code to read data from SQL Server DB using ADO:
READ (CHAN,KEY="!SELECT [ITEM_ID],[ITEM_NAME],[ITEM_ID_ORIGINAL] FROM [WMPOS].[dbo].[D_INVENTORY] WHERE ITEM_ID_ORIGINAL ='"+VALUE$+"'")F1$,F2$,F3$

Works well, it brings the item data, now I need to modify say, the name variable ITEM_NAME, how can I write/remove from SQL DB using ADO?

I tried to use the UPDATE SQL command instead of SELECT but it doesn't work.

I can read already, I need to be able to:
Write New Record
Update Existing Record
Delete Record

Thanks for the help team!
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: Devon Austen on September 10, 2024, 02:57:22 PM
You can execute SQL directly via the WRITE RECORD:

WRITE RECORD (CHAN) "INSERT INTO table_name VALUES (value1, value2, value3)"
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on September 12, 2024, 10:02:46 AM
Neat, I'll try that thanks!
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on July 09, 2025, 01:33:30 PM
Greetings again!, Been a while, this project has been renewed and I still need to figure out this part.
The whole point of thise project is to use iNomands to make a sort of webapp that connects to the client database on SQL Server.

These are a few things I tried, because actually the urgent thing now is to UPDATE a record rather than make a new entry (for now)

The code goes like:

LET V1$="BLAH BLAH"
LET V2$="3M115"
OPEN (CHAN,OPT=";USER=pvx;PSWD=pvx123@123@;Connect='Provider=SQLOLEDB;';CONCURRENCY=LOCK;EXTROPT=(UPDLOCK)")"[ado]SADI\SQLEXPRESS"

((EDIT: does that "UPDLOCK" line in the previous line have anything to do with preventing updating records?))



These attempts failed trying the INSERT INTO and UPDATE

WRITE RECORD (CHAN,KEY="!INSERT INTO [WMPOS].[dbo].[D_INVENTORY] ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'")

WRITE RECORD (CHAN,KEY="!UPDATE [WMPOS].[dbo].[D_INVENTORY] SET ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'")

Any hints on how to make that proper line code?

Thanks a bunch!!!
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on July 10, 2025, 08:20:36 AM
This one works:

READ (CHAN,KEY="!UPDATE [WMPOS].[dbo].[D_INVENTORY] SET ITEM_NAME = 'BLEH' WHERE ITEM_ID_ORIGINAL = '3M115'")

But it gives me an END of file error afterwards, which I think I can just skip it with an extra code on errors.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: Devon Austen on July 10, 2025, 09:13:50 AM
These:

WRITE RECORD (CHAN,KEY="!INSERT INTO [WMPOS].[dbo].[D_INVENTORY] ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'")

WRITE RECORD (CHAN,KEY="!UPDATE [WMPOS].[dbo].[D_INVENTORY] SET ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'")

Should be:

WRITE RECORD (CHAN)"INSERT INTO [WMPOS].[dbo].[D_INVENTORY] ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'"

WRITE RECORD (CHAN)"UPDATE [WMPOS].[dbo].[D_INVENTORY] SET ITEM_NAME ='"+V1$+" ' WHERE ITEM_ID_ORIGINAL ='"+V2$+"'"

The key="!<SQL>" syntax is for reads not writes. For writes you just pass in the SQL as what you are writing.

You read with a key= will work but since it does not return data you do after to ignore the error. I think using a write is better here since it doesn't try to read any response back.
Title: Re: Open, Read, Write/Update to a Microsoft SQL Server database.
Post by: nrh7 on July 10, 2025, 01:23:17 PM
Excellent!

Thank you very much!