Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Collections.Generic;
Using System.Text;
Namespace Database Learning
{
Class Program
{
static void Main (string[] args)
{
Use lightweight SqlDataReader to display data
Specify the connection string for the SQL Server provider
String connstring = "Server=192.168.99.45;database =northwind;uid =sa;pwd=1";
To establish a Connection object
SqlConnection sqlconn = new SqlConnection (connstring);
Open connection
Sqlconn.open ();
Specify a command object for the connection above
SqlCommand Thiscommand = Sqlconn.createcommand ();
Thiscommand.commandtext = "Select Customerid,companyname from Customers";
Executes DataReader for the specified Command object
SqlDataReader thissqldatareader = Thiscommand. ExecuteReader ();
As long as there is data
while (Thissqldatareader.read ())
//{
Output data
Console.WriteLine ("\t{0}\t{1}", thissqldatareader["CustomerId"], thissqldatareader["CompanyName"]);
//}
Turn off Read
Thissqldatareader.close ();
Close connection
Sqlconn.close ();
Console.ReadLine ();
Displaying data using a dataset
Query string
String Thiscommand = "Select Customerid,companyname from Customers";
Create the SqlDataAdapter object with two parameters, one for the query string and one for the connection object
SqlDataAdapter sqldap = new SqlDataAdapter (Thiscommand,sqlconn);
Creating a DataSet Object
DataSet Thisdataset = new DataSet ();
Fill the DataSet with the SqlDataAdapter fill method with two parameters, one for the created dataset instance and one for the filled table
Sqldap.fill (Thisdataset, "customers");
Show Query Results
foreach (DataRow therow in thisdataset.tables["Customers"). Rows)
{
Console.WriteLine (therow["CustomerID"] + "T" + therow["CompanyName"]);
}
Sqlconn.close ();
Console.ReadLine ();
}
}
}