Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' This is your query string, exactly the same from DAS. Dont tell me you dont know DAS...
- ' The only difference is that this query contains '@username' and '@password'
- ' These you can say are placeholders for values you are going to put in the future.
- ' Nothing more, nothing less. They are just placeholders.
- Dim myQuery as String = "SELECT * FROM Accounts WHERE Username = @username AND Password = @password"
- ' Create a new DataAdapter from the query and the connection, you shouldnt worry about this
- ' but if you want to learn more, go read the documentation about this.
- da = New OleDb.OleDbDataAdapter(myQuery,con)
- ' Declare the command, OleDbCommand is special object that deals with the query string for you
- ' and does stuff like adding parameters etc. again, if you want to know more, read the docs.
- ' myQuery is ur query, con is the OleDbConnection object u made called 'con'
- Dim cmd As New OleDbCommand(myQuery, con)
- ' This part is important. This is what the placeholders exist for, to be replaced.
- ' If you take a look into your query above, you can see there are 2 things that start with @
- ' followed by some name, i.e @username, @password. You have to tell VB which placeholder
- ' you wanna replace with what value.
- ' So, basically what this does, is it tries to find the placeholder in the query text (myQuery)
- ' that is '@username' and '@password' and replaces it with whatever value is in uservar and passvar,
- ' in this case 'uservar' is "John" and 'passvar' is "123"
- Dim uservar As String = "John"
- Dim passvar As String = "123"
- cmd.Params.Add("@username", uservar) ' <- uservar is a string variable, lets say uservar is "John"
- cmd.Params.Add("@password", passvar) ' <- passvar is a string variable, lets say passvar is "123"
- ' Open the connection
- con.Open()
- ' OleDbDataReader is a special object as well, it is special in that it can hold data as well as change the data it holds.
- ' you wonder how, i shall tell u now. 'reader' is a OleDbDataReader object.
- ' OleDbDataReader object is special cuz the data within it is actually a single row that is returned from
- ' the query (myQuery) when ran to your database. So to access a column of this row, you can simply do reader(i),
- ' 'i' being the index of the column you want to retrieve data from. Now you're wondering, 'hmm this isnt useful if it only
- ' gives me back a single row, what if multiple rows is returned?'
- ' Well thats what the '.Read()' function is for. Its quite special in how it works. alotta special thingies here.
- ' Basically how it works, whenever the '.Read()' function is called, in this case 'reader.Read()',
- ' The function will either return a 'True' or a 'False' depending on if there is another row after the current row.
- ' If it is 'True', the data within OleDbDataReader object, in this case 'reader', will get replaced with the data in the next row.
- ' So reader will have a whole new set of data within it.
- Dim reader As OleDbDataReader = cmd.ExecuteReader()
- ' Put it in a loop to keep reading for rows.
- While reader.Read()
- ' Opens a messagebox to show the 2nd column for each row returned.
- MsgBox(reader.GetString(1))
- End While
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement