Friday, May 29, 2009

(Q&A) Can i connect visual basic to AS400?

From: Bryan Arancon

Please find steps below on how you can create AS400 connection using visual basic.

Step 1:
'Define the ODBC Connection string
Dim MyODBCConnection As New OdbcConnection("Driver={Client Access ODBC Driver (32-bit)};" & _
"System=AS400Name;" & _
"TRANSLATE=1;" & _
"Uid=UserID;" & _
"Pwd=User Password")
'Open the connection
MyODBCConnection.Open()

Step2:
'Define the SQL statement to extract the data from the AS400
Dim selectCMD As OdbcCommand = New OdbcCommand("SELECT * FROM LIBRARY.FILE WHERE FILEFIELD='YourChoice' order by FILEFIELD", MyODBCConnection)

Step3:
'Initialize the reader
Dim dr As OdbcDataReader = selectCMD.ExecuteReader

Step4:
Try
'Set the mouse to show a Wait cursor
Me.Cursor = Cursors.WaitCursor
'start the Read loop
While dr.Read
'Note: the numbers in double quotes represent the column number from the AS400 database
'Add the data to the list view
Dim listItem As New ListViewItem(dr.GetString("2"))
listItem.SubItems.Add(dr.GetString("3"))
ListView1.Items.Add(listItem)
'End the loop
End While
'Reset the cursor
Me.Cursor = Cursors.Default

Catch ex As Exception
End Try

Step 5:
'Close the connection
MyODBCConnection.Close()
MyODBCConnection.Dispose()



No comments:

Post a Comment