Обсуждение: Executing SP in VB6
Dick Goulet
Senior Oracle DBA
Oracle Certified 8i DBA
From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br]
Sent: Monday, October 04, 2004 3:13 PM
To: pgsql-odbc@postgresql.org
Subject: [ODBC] Executing SP in VB6
----- Original Message -----From: Goulet, DickSent: Monday, October 04, 2004 4:15 PMSubject: RE: [ODBC] Executing SP in VB6May I ask why you want to use a stored procedure? A simple "Select fullname from users where user = '<value>';" should work just fine.Dick Goulet
Senior Oracle DBA
Oracle Certified 8i DBA
From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br]
Sent: Monday, October 04, 2004 3:13 PM
To: pgsql-odbc@postgresql.org
Subject: [ODBC] Executing SP in VB6Hello,Please, I just trying search in all WEB SITES about using STORED PROCEDURES with VISUAL BASIC, but I not found anything about this that can help me.I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAMEI need get the colum FULLNAME with parameter USER.How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 to resolve this litle problem ? (please, forgiven my english because I living in BRAZIL and not speak english).Thanks,Sandroyy
Hello my friend, I know. I have worked with VB about 6 years. I´m using ACCESS, ORACLE, SQL_SERVER, XBASE integrated with Visual Basic. Now, for my experience, I would like to use PostGresQL with VB6. OK ??!! Thanks for you colaboration. Sandroyy ----- Original Message ----- From: "Rick Sivernell" <res005ru@verizon.net> To: "Sandro Yaqub Yusuf" <sandro@proservvi.com.br> Sent: Monday, October 04, 2004 1:07 PM Subject: Re: [ODBC] Executing SP in VB6 > On Mon, 4 Oct 2004 16:24:33 -0300 > "Sandro Yaqub Yusuf" <sandro@proservvi.com.br> wrote: > >> Hello my friend, >> >> That is simple. I work developing programs in Visual Basic using complex >> Stored >> Procedures in SQL-SERVER 7/2000. I want to make STORED PROCEDURES in >> PostGresQL >> using Visual Basic 6. I need one example to begin. >> >> Thanks, >> >> Sandroyy >> ----- Original Message ----- >> From: Goulet, Dick >> To: Sandro Yaqub Yusuf ; pgsql-odbc@postgresql.org >> Sent: Monday, October 04, 2004 4:15 PM >> Subject: RE: [ODBC] Executing SP in VB6 >> >> >> May I ask why you want to use a stored procedure? A simple "Select >> fullname from >> users where user = '<value>';" should work just fine. >> >> Dick Goulet >> Senior Oracle DBA >> Oracle Certified 8i DBA >> >> >> >> >> >> ------------------------------------------------------------------------------ >> From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br] >> Sent: Monday, October 04, 2004 3:13 PM >> To: pgsql-odbc@postgresql.org >> Subject: [ODBC] Executing SP in VB6 >> >> >> Hello, >> >> Please, I just trying search in all WEB SITES about using STORED >> PROCEDURES with >> VISUAL BASIC, but I not found anything about this that can help me. >> >> I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAME >> I need get the colum FULLNAME with parameter USER. >> How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 to >> resolve >> this litle problem ? (please, forgiven my english because I live in >> BRAZIL and >> not speak english). >> >> Thanks, >> >> Sandroyy >> >> > Sandro > > You do not need to use stored procedure, VB has database objects for you > to use. > Personally I would use C++ and OLE DB. It is much better, faster and if > you are > working across OSes, you still connect to db using odbc. From your > description your > are on M$. ODBC or OLE DB do not care what the database is, as long as > there is a ODBC > driver. There are plenty of examples in books and on the net. > > cheers > > -- > Rick Sivernell > Dallas, Texas 75287 > 972 306-2296 > res005ru@verizon.net > Gentoo Linux > Registered Linux User #193859 > > .~. > / v \ > /( _ )\ > ^ ^ > In Linux we trust!
Sandro Yaqub Yusuf wrote: > Hello my friend, > > I know. > I have worked with VB about 6 years. I´m using ACCESS, ORACLE, > SQL_SERVER, XBASE integrated with Visual Basic. > Now, for my experience, I would like to use PostGresQL with VB6. OK ??!! Yes, it is possible to use PostgreSQL with VB6, via ODBC (I have posted numerous times in the past on this - see the archives if you are interested, I am not going to repeat myself here) - I am doing it as we speak. But I don't think this is what you are asking. If I am understanding you correctly, you are wanting to code stored procedures in VB and load them into your PostgreSQL DB, correct? As far as I know, this is *not* possible. You can, however, write SP's using PL/PgSQL, which is similar to PL on Oracle, from what I understand. Once you have your SP written, there should be a way to call/use it via ODBC. I have never done this, but I did find this post, which may give you some help: http://archives.postgresql.org/pgsql-novice/2004-04/msg00050.php Andrew Ayers > ----- Original Message ----- From: "Rick Sivernell" <res005ru@verizon.net> > To: "Sandro Yaqub Yusuf" <sandro@proservvi.com.br> > Sent: Monday, October 04, 2004 1:07 PM > Subject: Re: [ODBC] Executing SP in VB6 > > >> On Mon, 4 Oct 2004 16:24:33 -0300 >> "Sandro Yaqub Yusuf" <sandro@proservvi.com.br> wrote: >> >>> Hello my friend, >>> >>> That is simple. I work developing programs in Visual Basic using >>> complex Stored >>> Procedures in SQL-SERVER 7/2000. I want to make STORED PROCEDURES in >>> PostGresQL >>> using Visual Basic 6. I need one example to begin. >>> >>> Thanks, >>> >>> Sandroyy >>> ----- Original Message ----- From: Goulet, Dick >>> To: Sandro Yaqub Yusuf ; pgsql-odbc@postgresql.org >>> Sent: Monday, October 04, 2004 4:15 PM >>> Subject: RE: [ODBC] Executing SP in VB6 >>> >>> >>> May I ask why you want to use a stored procedure? A simple "Select >>> fullname from >>> users where user = '<value>';" should work just fine. >>> >>> Dick Goulet >>> Senior Oracle DBA >>> Oracle Certified 8i DBA >>> >>> >>> >>> >>> >>> ------------------------------------------------------------------------------ >>> >>> From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br] >>> Sent: Monday, October 04, 2004 3:13 PM >>> To: pgsql-odbc@postgresql.org >>> Subject: [ODBC] Executing SP in VB6 >>> >>> >>> Hello, >>> >>> Please, I just trying search in all WEB SITES about using STORED >>> PROCEDURES with >>> VISUAL BASIC, but I not found anything about this that can help me. >>> >>> I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAME >>> I need get the colum FULLNAME with parameter USER. >>> How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 >>> to resolve >>> this litle problem ? (please, forgiven my english because I live in >>> BRAZIL and >>> not speak english). >>> >>> Thanks, >>> >>> Sandroyy >>> >>> >> Sandro >> >> You do not need to use stored procedure, VB has database objects for >> you to use. >> Personally I would use C++ and OLE DB. It is much better, faster and >> if you are >> working across OSes, you still connect to db using odbc. From your >> description your >> are on M$. ODBC or OLE DB do not care what the database is, as long as >> there is a ODBC >> driver. There are plenty of examples in books and on the net. >> >> cheers >> >> -- >> Rick Sivernell >> Dallas, Texas 75287 >> 972 306-2296 >> res005ru@verizon.net >> Gentoo Linux >> Registered Linux User #193859 >> >> .~. >> / v \ >> /( _ )\ >> ^ ^ >> In Linux we trust! > > > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html > > > -- CONFIDENTIALITY NOTICE -- This message is intended for the sole use of the individual and entity to whom it is addressed, and may contain informationthat is privileged, confidential and exempt from disclosure under applicable law. If you are not the intendedaddressee, nor authorized to receive for the intended addressee, you are hereby notified that you may not use, copy,disclose or distribute to anyone the message or any information contained in the message. If you have received thismessage in error, please immediately advise the sender by reply email, and delete the message. Thank you.
Sub ParameterExample()
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim prm As ADODB.Parameter
cmd.ActiveConnection = Provider=OraOLEDB.Oracle;User ID=scott;Password=tiger;Data Source=oraDSN;
' Set the command's text, and specify that it is an SQL statement.
cmd.CommandText = "SELECT item_name FROM v_syouhin_info WHERE deptno = ?"
cmd.CommandType = adCmdText
' Set up a new parameter for the select.
Set prm = cmd.CreateParameter("deptno", adInteger, adParamInput, , 10)
cmd.Parameters.Append prm
' Create a recordset by executing the command.
Set rs = cmd.Execute
' Loop through the recordset and print the first field.
Do While Not rs.EOF
Debug.Print rs(0)
rs.MoveNext
Loop
' Close the recordset.
rs.Close
End Sub
Sub ParameterExample()
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim prm As ADODB.Parameter
cmd.ActiveConnection = Provider=OraOLEDB.Oracle;User ID=scott;Password=tiger;Data Source=oraDSN;
' Set the command's text, and specify that it is an SQL statement.
cmd.CommandText = "STORED_PROCEDURE_NAME" ' <-- changed
cmd.CommandType = adCmdStoredProc ' <-- changed this part too
' Set up a new parameter for the select.
Set prm = cmd.CreateParameter("deptno", adInteger, adParamInput, , 10)
cmd.Parameters.Append prm
' Create a recordset by executing the command.
Set rs = cmd.Execute
' Loop through the recordset and print the first field.
Do While Not rs.EOF
Debug.Print rs(0)
rs.MoveNext
Loop
' Close the recordset.
rs.Close
End Sub
----- Original Message -----From: Sandro Yaqub YusufTo: Goulet, DickSent: Tuesday, October 05, 2004 4:24 AMSubject: Re: [ODBC] Executing SP in VB6Hello my friend,That is simple. I work developing programs in Visual Basic using complex Stored Procedures in SQL-SERVER 7/2000.I want to make STORED PROCEDURES in PostGresQL using Visual Basic 6. I need one example to begin.Thanks,Sandroyy----- Original Message -----From: Goulet, DickSent: Monday, October 04, 2004 4:15 PMSubject: RE: [ODBC] Executing SP in VB6May I ask why you want to use a stored procedure? A simple "Select fullname from users where user = '<value>';" should work just fine.Dick Goulet
Senior Oracle DBA
Oracle Certified 8i DBA
From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br]
Sent: Monday, October 04, 2004 3:13 PM
To: pgsql-odbc@postgresql.org
Subject: [ODBC] Executing SP in VB6Hello,Please, I just trying search in all WEB SITES about using STORED PROCEDURES with VISUAL BASIC, but I not found anything about this that can help me.I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAMEI need get the colum FULLNAME with parameter USER.How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 to resolve this litle problem ? (please, forgiven my english because I living in BRAZIL and not speak english).Thanks,Sandroyy
Hi Yusef, I just realized that it probably isn't the VB code you wanted to see, but the pg stored procedure code. I don't have an example on hand, but presumably all you have to do is return a ref cursor from you pg procedure and the same VB code that you used for sql server would work on pg. Maybe... Anyway, I think that you should check the docs for "refcursor" - maybe section 37.8 and see if that is what you are looking for. Regards Iain ----- Original Message ----- From: Sandro Yaqub Yusuf To: Goulet, Dick Cc: pgsql-odbc@postgresql.org Sent: Tuesday, October 05, 2004 4:24 AM Subject: Re: [ODBC] Executing SP in VB6 Hello my friend, That is simple. I work developing programs in Visual Basic using complex Stored Procedures in SQL-SERVER 7/2000. I want to make STORED PROCEDURES in PostGresQL using Visual Basic 6. I need one example to begin. Thanks, Sandroyy ----- Original Message ----- From: Goulet, Dick To: Sandro Yaqub Yusuf ; pgsql-odbc@postgresql.org Sent: Monday, October 04, 2004 4:15 PM Subject: RE: [ODBC] Executing SP in VB6 May I ask why you want to use a stored procedure? A simple "Select fullname from users where user = '<value>';" should work just fine. Dick Goulet Senior Oracle DBA Oracle Certified 8i DBA From: Sandro Yaqub Yusuf [mailto:sandro@proservvi.com.br] Sent: Monday, October 04, 2004 3:13 PM To: pgsql-odbc@postgresql.org Subject: [ODBC] Executing SP in VB6 Hello, Please, I just trying search in all WEB SITES about using STORED PROCEDURES with VISUAL BASIC, but I not found anything about this that can help me. I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAME I need get the colum FULLNAME with parameter USER. How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 to resolve this litle problem ? (please, forgiven my english because I living in BRAZIL and not speak english). Thanks, Sandroyy
Hello Sandro. Good idea to use stored procedures. I'm using MS Access, so I guess you may have to adapt the code a
bit...
This code uses DAO, not ADO or whatever. I hope it will help you.
Here we are:
1) The stored procedure:
------------------------
CREATE FUNCTION public.search_your_tbl_name(varchar)
RETURNS SETOF your_tbl_name AS
'
SELECT * FROM public.your_tbl_name
WHERE lower(id) LIKE lower($1)
OR lower(foo1) LIKE lower($1)
OR lower(foo2) LIKE lower($1)
OR lower(foo3) LIKE lower($1)
OR lower(foo4) LIKE lower($1)
ORDER BY foo2
LIMIT 50
'
LANGUAGE 'sql' VOLATILE;
2) The VBA code:
----------------
Public Function global_dsn_name() As String
global_dsn_name = "your_dns_name"
End Function
Sub query_run(query As String, p As String)
On Error GoTo query_runError
Dim MyWorkspace As DAO.Workspace
Dim MyConnection As DAO.Connection
Dim MyRecordset As DAO.Recordset
Dim MySQLString As String
Dim MyODBCConnectString As String
Set MyWorkspace = CreateWorkspace("ODBCWorkspace", "", "", dbUseODBC)
MyODBCConnectString = "ODBC;DSN=" & global_dsn_name() & ";"
Set MyConnection = MyWorkspace.OpenConnection("Connection1", dbDriverNoPrompt, , MyODBCConnectString)
MySQLString = "SELECT * FROM public." & """" & query & """" & "('" & p & "');"
Set MyRecordset = MyConnection.OpenRecordset(MySQLString, dbOpenDynamic)
With MyRecordset
Do While Not .EOF
Debug.Print _
MyRecordset("id") & " / " & _
MyRecordset("foo1") & " / " & _
MyRecordset("foo2") & " / " & _
MyRecordset("foo3") & " / " & _
MyRecordset("foo4")
.MoveNext
Loop
End With
MyRecordset.Close
Set MyRecordset = Nothing
MyConnection.Close
Set MyConnection = Nothing
MyWorkspace.Close
Set MyWorkspace = Nothing
query_runExit:
Exit Sub
query_runError:
MsgBox "Error in query_run."
Resume query_runExit
End Sub
3) How you use it:
------------------
query_run("search_your_tbl_name", "test%")
4) MS Access & reports:
-----------------------
The problem with MS Access is that sometimes you may want to use the result of your stored procedure in a report, for
example.In this case, you have to store the query instead of simply running it. You won't need this with VB6, but I put
thecode here also, it might help others.
Sub search_store(query As String, p As String)
On Error GoTo search_storeError
Dim MyDatabase As DAO.DataBase
Dim MyQueryDef As DAO.QueryDef
Set MyDatabase = CurrentDb()
If (QueryExists(query)) Then MyDatabase.QueryDefs.Delete query
Set MyQueryDef = MyDatabase.CreateQueryDef(query)
MyQueryDef.Connect = "ODBC;DSN=" & global_dsn_name() & ";"
MyQueryDef.SQL = "SELECT * FROM public." & """" & query & """" & "('" & p & "');"
MyQueryDef.ReturnsRecords = True
MyQueryDef.Close
Set MyQueryDef = Nothing
MyDatabase.Close
Set MyDatabase = Nothing
search_storeExit:
Exit Sub
search_storeError:
MsgBox "Error in search_store."
Resume search_storeExit
End Sub
This routine creates a querydef called "search_your_tbl_name" you can incorporate in a report.
________________________________
De : pgsql-odbc-owner@postgresql.org [mailto:pgsql-odbc-owner@postgresql.org] De la part de Sandro Yaqub Yusuf
Envoyé : lundi, 4. octobre 2004 21:13
À : pgsql-odbc@postgresql.org
Objet : [ODBC] Executing SP in VB6
Hello,
Please, I just trying search in all WEB SITES about using STORED PROCEDURES with VISUAL BASIC, but I not found anything
aboutthis that can help me.
I have a table: USERS with colums IDUSER, USER, PASSWORD, FULLNAME
I need get the colum FULLNAME with parameter USER.
How I do to create and execute a STORED PROCEDURE in VISUAL BASIC 6 to resolve this litle problem ? (please, forgiven
myenglish because I living in BRAZIL and not speak english).
Thanks,
Sandroyy
Hello my dears friends,
Thanks for yours colaborations.
It´s here the part of solution about my problem with colaboration of
everbody that help me (Philippe Lang, Iain, Andrew Ayers, Rick Sivernell,
Corey W. Gibbs):
---------------------------------------------------------------------------------------------------------------------------------------
POSTGRESQL:
CREATE OR REPLACE FUNCTION sp_user_search(varchar)
RETURNS SETOF user AS
' select * from user where user = $1'
LANGUAGE 'sql' VOLATILE;
---------------------------------------------------------------------------------------------------------------------------------------
VISUAL BASIC 6 with ADO 2.8 without STORED PROCEDURE:
Dim adoBD As ADODB.Connection
Dim rsTB As New ADODB.Recordset
Set adoBD = New ADODB.Connection
adoBD.ConnectionString =
"driver=PostgreSQL};server=localhost;database=SICCEV;port=5432;uid=ryan;pwd=displace;"
adoBD.Open
Set rsTB = adoBD.Execute("select * from user where user = 'Sandro';")
Do While Not rsTB.EOF
MsgBox rsTB!Usuario
rsTB.MoveNext
Loop
rsTB.Close
adoBD.Close
RESULT OF EXECUTION: Sandro Yaqub Yusuf
---------------------------------------------------------------------------------------------------------------------------------------
VISUAL BASIC 6 with ADO 2.8 with STORED PROCEDURE:
Dim adoBD As ADODB.Connection
Dim rsTB As New ADODB.Recordset
Set adoBD = New ADODB.Connection
adoBD.ConnectionString =
"driver=PostgreSQL};server=localhost;database=SICCEV;port=5432;uid=ryan;pwd=displace;"
adoBD.Open
Set rsTB = adoBD.Execute("select sp_user_search('Sandro');")
Do While Not rsTB.EOF
MsgBox rsTB(0)
rsTB.MoveNext
Loop
rsTB.Close
adoBD.Close
RESULT OF EXECUTION: (1,Sandro,123,"Sandro Yaqub Yusuf")
---------------------------------------------------------------------------------------------------------------------------------------
The question is how I do for to isolate the columns when come
(1,Sandro,123,"Sandro Yaqub Yusuf"). I would like to get only the column
FULLNAME using the STORED PROCEDURE. Have I do a function in VB for to
isolate the RESULTS of STORED PROCEDURES in POSTGRESQL ?
Thanks more one times...
Sandroyy
Hi Sandro
are you saying that rsTB(0) = "(1,Sandro,123,"Sandro Yaqub Yusuf")"?
That would be interesting, but not very useful.
Does rsTB(1) fail as an invalid column?
Does rsTB.Fields("user").value work? (It should return "Sandro" according
to your data).
Regards
Iain
----- Original Message -----
From: "Sandro Yaqub Yusuf" <sandro@proservvi.com.br>
To: <pgsql-odbc@postgresql.org>
Sent: Tuesday, October 05, 2004 11:35 PM
Subject: Thanks - Part One - Re: [ODBC] Executing SP in VB6
> Hello my dears friends,
>
> Thanks for yours colaborations.
>
> It´s here the part of solution about my problem with colaboration of
> everbody that help me (Philippe Lang, Iain, Andrew Ayers, Rick Sivernell,
> Corey W. Gibbs):
>
>
---------------------------------------------------------------------------------------------------------------------------------------
>
> POSTGRESQL:
>
> CREATE OR REPLACE FUNCTION sp_user_search(varchar)
> RETURNS SETOF user AS
> ' select * from user where user = $1'
> LANGUAGE 'sql' VOLATILE;
>
>
---------------------------------------------------------------------------------------------------------------------------------------
>
> VISUAL BASIC 6 with ADO 2.8 without STORED PROCEDURE:
>
> Dim adoBD As ADODB.Connection
> Dim rsTB As New ADODB.Recordset
>
> Set adoBD = New ADODB.Connection
>
> adoBD.ConnectionString =
> "driver=PostgreSQL};server=localhost;database=SICCEV;port=5432;uid=ryan;pwd=displace;"
> adoBD.Open
>
> Set rsTB = adoBD.Execute("select * from user where user = 'Sandro';")
>
> Do While Not rsTB.EOF
> MsgBox rsTB!Usuario
>
> rsTB.MoveNext
> Loop
>
> rsTB.Close
> adoBD.Close
>
> RESULT OF EXECUTION: Sandro Yaqub Yusuf
>
>
---------------------------------------------------------------------------------------------------------------------------------------
>
> VISUAL BASIC 6 with ADO 2.8 with STORED PROCEDURE:
>
> Dim adoBD As ADODB.Connection
> Dim rsTB As New ADODB.Recordset
>
> Set adoBD = New ADODB.Connection
>
> adoBD.ConnectionString =
> "driver=PostgreSQL};server=localhost;database=SICCEV;port=5432;uid=ryan;pwd=displace;"
> adoBD.Open
>
> Set rsTB = adoBD.Execute("select sp_user_search('Sandro');")
>
> Do While Not rsTB.EOF
> MsgBox rsTB(0)
>
> rsTB.MoveNext
> Loop
>
> rsTB.Close
> adoBD.Close
>
> RESULT OF EXECUTION: (1,Sandro,123,"Sandro Yaqub Yusuf")
>
>
---------------------------------------------------------------------------------------------------------------------------------------
>
> The question is how I do for to isolate the columns when come
> (1,Sandro,123,"Sandro Yaqub Yusuf"). I would like to get only the column
> FULLNAME using the STORED PROCEDURE. Have I do a function in VB for to
> isolate the RESULTS of STORED PROCEDURES in POSTGRESQL ?
>
> Thanks more one times...
>
> Sandroyy
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend