Example of accessing SQL Server using ADO. NET
First inMicrosoft SQL Server Management StudioTestT-SQLStatement. In the following exampleT-SQLStatement.
Select * FromEmployeesWhereEmployeeidBetween2And 6
The query result is as follows:
So we are usingC #How to complete the data access function in the code when building an application. See the sample code:
Sample CodeA:
Using System;
Using System. Data;
Using System. Data. SqlClient;
Namespace SqlServerProvider
{
Class Program
{
Static void Main (string [] args)
{
// Set the connection string
String connString = @"
Server = .;
Integrated Security = True;
Initial Catalog = Northwind
";
// Set the query string in hard encoding Mode
String sqlqry = @ "select * from employees where employeeid between 2 and 6 ";
// Declare the data connection and data reader object variables
SqlConnection conn = null;
SqlDataReader reader = null;
Try
{
Conn = new SqlConnection (connString );
SqlCommand restart qry = conn. CreateCommand ();
Repeated qry. CommandType = CommandType. Text;