Retrieve or Read Data from Database - Oledb .NET E06

Retrieve or Read Data from Database - Oledb .NET E06 In this episode i'll cover multiple examples on how to retrieve or read data from database let's call this a read from db so for this purpose we are we're gonna.

Use uh the reader so let's declare two readers for now uh this variable will be oledb reader data reader yeah.

And this just in case we want to you know i use the reader uh multiple readers in the same process i'll just demonstrate that now.

So before we start writing this code here uh database is pretty cool uh you know like query designer that you can use to quickly uh.

Generate sql for example let's say that we want to uh get all the states the unique states from here either you can just drag this here.

And group them and just run this query so you'll see this list yeah so if you go to uh the sql you'll see this uh you know sql created.

Retrieve or Read Data from Database - Oledb .NET E06

Or you can also say um select distinct state from client that will basically return the same results like this okay um so if i get this sql.

Now and try to execute it from here let's call this this article and let me copy this part if the connection is open um.

We want to use this and then um our dr1 is gonna be um command dot uh reader execute reader okay and then.

We will read the data i'm gonna get rid of this while we'll look through the data um dr1 dot read and then we'll maybe just debug.

This uh dr1 first field value if it is string if it is a number then we can force it to.

Print the string in this fashion oops sorry now let's add a button to do this i'll put this label as read for now.

    And when that button is click we want to

    Read i'm going to run this suppose that we just created so this is our sql if the connection is open.

    Then we want to i think uh going to open with this query and then it's going to execute reader uh so up here we are executing.

    Non-query okay this one is the reader okay that's the difference here now it's going to read the data and then it's gonna look through this data point.

    Still and and then we're gonna just print it out here in the output okay um okay opening in debug mode .

    So these are the you know unique state list so if you go back here run this this is the same list that we see here okay uh let's try some more examples.

    We'll try linking multiple tables as well i'm gonna set this now let's say that um .

    We want to bring in all the data from this table then we can just say select all or select you know asterisk means all from clients.

    If i just run this now it's going to be one field but it printed all the data set here okay from client one till 500 um so if you want to print multiple data.

    Then we will replicate this string um like two three four five let's say we want to print five fields uh one two.

    Or i think we can just

    Okay just ignore that so the one zero one two three four so this will be five fields starting.

    From zero to four um and then we want to format them as well so say.

    Data um i'll just call this field field one i don't want to write the name for now so i'll just say fill one and then here going to be zero let me just put this here for now.

    Two three four five so this will be these values will be written to this format okay so one two three and four.

    Let's try and run this okay this is all showing field one but you know like this is client um the name the company uh the address and so on here yeah so if you want to.

    Specify for example here this is going to be a customer id and this is going to be first name first name last name like that.

    So it will print you know like in this placeholder this value will be printed that's the idea here okay so as you can see here now the first name last name and so on is coming.

    Out here so uh using this data you know that has been returned you can populate this data maybe you can export it you can uh load it to combo box data grid um.

    You know like uh you can use it for anything let's try more examples let's say that we want to link these two tables um so again we can you know like quickly.

    Generate this oops so you go from here as well so these two are already linked so if you double.

    Click here you'll be able to see the join type so here include rows where bot you know i bought the tables have this key yeah.

    Uh if you want to do a left or right you know include all from clients but uh only the ones that are matching from the other table and so on now let's say that we want to just.

    Return the matching records from bot tables i'll just click on this one okay um let's say that we want to generate um let's say we want to pull.

    Data for let's say certain date let's try um on the 20th for this day yeah.

    So i'll put a criteria here in the criteria let's put this date okay so it returned all this data uh now let's try and run this i'm going to copy this or you can also.

    Straightforward it from here if you're already familiar uh with the sql you can easily write it from here so select this field from this table.

    From this table and then the join is inner join type and the key is customer id and the where condition is this yeah where the date is this even if you.

    Don't have these brackets also it's fine like this is also good so we're saying return all the data off by linking these two tables using the customer id and then the.

    Filter is on the date that is 10 16. let's run this now um now here the names are going to be different so the first one is the customer id.

    Second one is the date and the third one is amount just get rid of this for now.

    zero one two so all these are the data for 10 16. okay uh now you can also specify you know like multiple criterias um.

    For example here we're saying transition data is this and then you can put more criteria and let's say clients let's say we want to bring in.

    All the data for you know like this date and for the state of state is equal to ta maybe not so if we go back to this.

    And we can say so there's a state called ta so we want to return all the data all the transactions for certain date but specifically for this.

    This particular state only let's run this okay so these are for that particular state only so if you look at this client here c2 of all these lines.

    You can validate it manually as well by going here but it will be working okay um now one more thing i want to mention.

    Here is you can also put in parameters um so instead of this ta or this state you can specify um question mark like this okay and then.

    Here you can put in the parameters like this parameters.net and then here you can specify the name and data type so let's say we want to.

    Call this state um and then here you can specify the data type uh all in the led.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=mzBX0kOMCTY
Previous Post Next Post