Presentation is loading. Please wait.

Presentation is loading. Please wait.

‘Tirgul’ # 8 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #8.

Similar presentations


Presentation on theme: "‘Tirgul’ # 8 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #8."— Presentation transcript:

1 ‘Tirgul’ # 8 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #8

2 ‘Tirgul’ # 8 Objectives SQL Queries & Statements Useful SQL techniques DB Comparison

3 ‘Tirgul’ # 8 retrieve All records SELECT * FROM Users Use Constants: Use inside Function: Public Const GET_ALL_USERS = "Select * From Users" Public Function GetAllUsers() As ADODB.Recordset '... Set RS = Connection.execute(GET_ALL_USERS) '... End Function

4 ‘Tirgul’ # 8 retrieve Some fields SELECT UserID, UserName FROM Users Use Constants: Inside a function Public Const GET_USERS_FIELDS = "Select UserID, UserName” & _ “ From Users" Public Function GetUsersFields() As ADODB.Recordset '... Set RS = Connection.execute(GET_USERS_FIELDS) '... End Function

5 ‘Tirgul’ # 8 Conditions SELECT UserID, UserName FROM Users WHERE UserName = 'James ‘ SELECT UserID, UserName FROM Users WHERE UserID < 10 SELECT UserID, UserName FROM Users WHERE UserID < 10 OR UserName = 'James'

6 ‘Tirgul’ # 8 Conditions Cannot build all string Use inside Function: Public Const GET_USER_BY_NAME = "SELECT UserID, UserName FROM Users WHERE UserName = '" Public Function GetUserByName(strUserName As String) As ADODB.Recordset '... Dim strQuery As String strQuery = GET_USER_BY_NAME & strName & "'" Set RS = Connection.execute(strQuery) '... End Function

7 ‘Tirgul’ # 8 Sorting SELECT * FROM Users ORDER BY UserName DESC SELECT * FROM Users ORDER BY UserName SELECT * FROM Users ORDER BY UserName ASC SELECT * FROM Users ORDER BY UserName, UserID DESC

8 ‘Tirgul’ # 8 Sorting Use Constants: Use inside Function: Public Const GET_USERS_ORDER_DESC = "SELECT * FROM Users ORDER BY UserName DESC" Public Function GetUserOrderDesc() As ADODB.Recordset '... Set RS = Connection.execute(GET_USERS_ORDER_DESC) '... End Function

9 ‘Tirgul’ # 8 Add records INSERT INTO Users VALUES (10, 'James Crowley', 'PASSWORD') INSERT INTO Users (UserName, UserID) VALUES ('James Crowley', 10) INSERT INTO Users (UserName, Password) VALUES ('James Crowley', 'PASSWORD')

10 ‘Tirgul’ # 8 Add records Use Constants: Use inside Function: Public Const INSERT_USER = "INSERT INTO Users (UserID,” & _ UserName) VALUES (‘" Public Function InsertUser(strUserID As String, _ strUserName As String) '... Dim strQuery As String strQuery = INSERT_USER & strUserID & "'" & strUserName & "')" Set RS = Connection.execute(strQuery) '... End Function Little an Evil.. No Return value

11 ‘Tirgul’ # 8 Another option. Use Array: Public Function InsertUser(UserDataArr) '... Dim strQuery As String strQuery = INSERT_USER & UserDataArr(0) & "'" & UserDataArr(0) & "')" Connection.execute(strQuery) '... End Function Public Function InsertUser(UserDataArr) '... RS!UserID = UserDataArr(0) RS!UserName = UserDataArr(1) '... End Function

12 ‘Tirgul’ # 8 Update records UPDATE Users SET UserName = 'James Smith', Password = 'NEWPASSWORD' WHERE UserID = 10 UPDATE Furniture SET Price = 10 WHERE ItemType = 'Cupboard'

13 ‘Tirgul’ # 8 Update records Use Constants: Use inside Function: Public Const UPDATE_USER = "UPDATE Users SET UserName = ‘" Public Function UpdateUser(strUserName As String) '... Dim strQuery As String strQuery = UPDATE_USER & strUserName & "'" Connection.execute(strQuery) '... End Function

14 ‘Tirgul’ # 8 Delete a record DELETE FROM Users WHERE UserID = 10 DELETE FROM Furniture WHERE Price < 10 TRUNCATE TABLE authors TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE

15 ‘Tirgul’ # 8 SQL Transaction statements BEGIN TRANSACTION COMMIT TRANSACTION ROLLBACK TRANSACTION

16 ‘Tirgul’ # 8 Transaction Use Public Function Insert() As Boolean Conn.BeginTrans 'Add 'Delete 'Update If Err.Number <> 0 Then Conn.RollBack Insert = False Exit Function End If Conn.Commit End Function

17 ‘Tirgul’ # 8 Commercial Databases Access –Simple yet useful –For a single/double work stations –Limit data size MS SQL Server –Very common –Only Microsoft… –Ease of use –Time-to-market Oracle –More “Commercial” –Known as a powerful tool –Multi platform – Win/Unix/Linux IBM DB2 –More “Commercial” –Multi platform – Win/Unix/Linux –Useful for IBM family & tools


Download ppt "‘Tirgul’ # 8 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #8."

Similar presentations


Ads by Google