README.md
December 4, 2022 ยท View on GitHub
Description
This is a short tutorial explaining how you would go about reading data from your database on to an application or an ASP.NET web page.
More Info
| Submitted On | |
| By | Tyler Love |
| Level | Beginner |
| User Rating | 3.6 (32 globes from 9 users) |
| Compatibility | C#, ASP.NET |
| Category | Databases |
| World | .Net (C#, VB.net) |
| Archive File |
Source Code
<size="12px">1. Create a sqlConnection to your database using the sqlConnection tool in your tool box. Name it sqlMyCon
2. Create a sql command using the sqlCommand tool in your toolbox that will query and return a row that you are wanting. Name it sqlMyCmd. In this case I am wanting to return a specific row so I would do something like this:
<color="#0000FF">
SELECT * FROM MyTable
WHERE (ColumnName = '1234567')
That would return all the columns in the the table "MyTable". Where there was a row in the Column "ColumnName" that has the integer "1234567" in it.
3. Now you have done with that you get to write a few lines of code. So here is the example of how I would go about reading the data that is returned and inserting it into a web page or application. (Pretend I have a label named "lblData")
<color="#0088FF">
sqlMyCon.Open();
<color="#3CF13C">
// This creates a datareader
<color="#0088FF">
SqlDataReader reader = sqlMyCmd.ExecuteReader();
reader.Read();
<color="#3CF13C">
//This will select what column's data it will
// return using "GetString" remember it is zerobased
<color="#0088FF">
lblData.Text = reader.GetString(2);
sqlMyCon.Close();
4. Run it and make sure it works. If you would like to see something that uses that and does work go to http://www.junebughunter.net I made that site and all the content comes from a database using almost that exact same thing.