Building SQL commands and using ADO.NET classes to access
database without the need of data oriented controls is considered as direct
data access.
There are a number of steps you should follow to achieve
direct data access:
- Import relevant namespace
- create an instance object of SqlConnection class
- set the ConnectionString property of the connection object
- Open the connection
- Create an instance object of SqlCommand
- set the CommandText property of the command with your sql statement
- set the Connection property of the command object with the SqlConnection object you’ve created
- execute the command
If you are retrieving data (i.e: if you are using Select sql
statement) you need to use SqlDataReader class to extract the data from the
SqlCommand:
- create a variable object of type SqlDataReader. Note: SqlDataReader does not use the new keyword that means it has no constructor
- Execute command reader. Command reader (SqlCommand.ExecuteReader() ) is a method that executes the command against the database and at the same time returns a SqlDataReader with the result
- Traverse the rows of SqlDataReader one after one by using Read() method
- after you call the Read() method use the data in the reader by column index or name
- close the reader and the connection if you are done (important)
Example:
For how to use Parameters go to my channel http://youtube.com/nuux14
THANK YOU IT'S HELPFUL TUTORIAL
ReplyDeleteWe hope to carry on this fantastic tryning
DeleteIf ALLAH SAYS