|
Tech Note 22: How to connect to databases (Oracle, etc)February 04, 2005© NS BASIC Corporation. All rights reserved. |
|
|
NS Basic/Desktop makes it easy to connect to databases such as SQL Server, Access and Oracle. The ADODB object is designed just for this purpose. With NS Basic/Desktop's easy form design capability, it's easy and fast to create front end applications for databases.
Coupled with NS Basic's ability to deal directly with data in Excel, Word and other COM aware applications, NS Basic/Desktop can also be a powerful middleware solution.
The following deals with accessing an Oracle database. Much the same procedure is used for other databases. To see an example of an Access database, check out ADODEMO in the Samples.
The following is contributed by mizuno-ami in Japan
Connecting to databases is not difficult using NS Basic/Desktop. Using the connection method that provided by Microsoft called 'ADO', it can connect to not only Microsoft's databases (such as SQL Server, Access and ADO) but also Oracle Databases.
For example, imagine there is an Oracle database with the following connection information:
Sub CommandButton1_Click Dim objADO Dim objRs Dim strSQL Dim strUserID Listbox1.Clear strUserID=TextBox1.Text 'Create ADO Object Set objADO = CreateObject("ADODB.Connection") 'Open Oracle DB by ADO objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=TESTDB; UID=USER001; PWD=PASS001;" 'Set SQL strSQL="SELECT USERID || ':' || USERNAME FROM M_USERLIST WHERE USERID LIKE '" & stUserID & "%' ORDER BY USERID" 'Set DynaSet(RecordSet) Object Set objRS = objADO.Execute(strSQL) 'Show records data Do Until objRS.Eof = True 'Show the field Listbox1.addItem objRS(0) 'move next record objRS.MoveNext Loop 'Close ADO objADO.Close End Sub |
Sub CommandButton2_Click Dim strSQL Dim UserID Dim UserName 'Create ADO Object by ADDOBJECT: it is the same method by using CreateObject() AddObject "ADODB.Connection","objADO" 'Open Oracle DB by ODBC objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=TESTDB; UID=USER001; PWD=PASS001;" 'Get TextBox data UserID=TextBox2.Text UserName=TextBox3.Text 'SQL strSQL="INSERT INTO M_USERLIST(USERID,USERNAME) VALUES('" & UserID & "','" & UserName & "')" 'Begin Transaction objADO.BeginTrans objADO.Execute strSQL 'Commit Transaction objADO.CommitTrans 'Close ADO objADO.Close End Sub |
Sub CommandButton2_Click Dim strSQL Dim UserID Dim UserName On Error Resume Next 'Create ADO Object by ADDOBJECT: it is the same method by using CreateObject() AddObject "ADODB.Connection","objADO" 'Open Oracle DB by ODBC objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=TESTDB; UID=USER001; PWD=PASS001;" 'Get TextBox data UserID=TextBox2.Text UserName=TextBox3.Text 'SQL strSQL="INSERT INTO M_USERLIST(USERID,USERNAME) VALUES('" & UserID & "','" & UserName & "')" 'Begin Transaction objADO.BeginTrans objADO.Execute strSQL 'Check the error If objADO.Errors.Count=0 Then 'Commit Transaction objADO.CommitTrans Else 'Rollback Transaction objADO.RollbackTrans End if 'Close ADO objADO.Close End Sub |
Sub CommandButton2_Click Dim strSQL Dim UserID Dim UserName Dim lngRecAff On Error Resume Next 'Create ADO Object by ADDOBJECT: it is the same method by using CreateObject() AddObject "ADODB.Connection","objADO" 'Open Oracle DB by ODBC objADO.Open "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=TESTDB; UID=USER001; PWD=PASS001;" 'Get TextBox data UserID=TextBox2.Text UserName=TextBox3.Text 'SQL strSQL="INSERT INTO M_USERLIST(USERID,USERNAME) VALUES('" & UserID & "','" & UserName & "')" 'Begin Transaction objADO.BeginTrans objADO.Execute strSQL,lngRecAff 'Check the error If objADO.Errors.Count=0 Then If lngRecAff=1 Then 'Commit Transaction objADO.CommitTrans Else 'Rollback Transaction objADO.RollbackTrans End if Else 'Rollback Transaction objADO.RollbackTrans End if 'Close ADO objADO.Close End Sub |