Data access:
corresponding namespaces:System.Data.SqlClient;
SqlConnection: Connection Object
SqlCommand: Command Object
SqlDataReader: Reader Object
Create a connection string
String connstring = "server=.; Database=mydb;user=sa;pwd=123 ";
Connecting Objects
SqlConnection conn = new SqlConnection (connstring);
To Create a Command object
SqlCommand cmd = conn. CreateCommand ();
write the SQL statement to execute
Cmd.commandtext = "SELECT * from Info";
Open Connection
Conn. Open ();
perform operation (read operation, return reader object)
SqlDataReader dr = cmd. ExecuteReader ();
Perform operation (add-and-revise operation, return number of rows)
Cmd. ExecuteNonQuery ();
working with Data
if (Dr. HasRows)--This is a Boolean-type value that returns True or false
{
Dr. Read ();
Console.WriteLine (Dr[0]);
Console.ReadLine ();
}
Else
{
Console.WriteLine (" Read failed! ");
}
Close Connection
Conn. Close ();
Dr. Read () is a database data access pointer that goes down one line each time it executes, returns true if there is content , and Dr Access is the current row data collection. You can use an index or column name to access the corresponding data
SQL database--data access