Ado. NET composition: Terminal Data set: include: Dataset/datatable/datarow .... NET Framework data providers: including Connection/dataadapter/datareader/command
Ado. NET creates a connection to SQL Server through the SqlConnection class, SqlConnection represents a database connection, ADO. NET, such as the connection of the IDisposable interface, you can use using for resource management. SqlCommand represents a command submitted to the server (SQL statement, etc.), the CommandText property is the SQL statement to execute, and the ExecuteNonQuery method executes a non-query statement (Update, Insert, Delete, etc.). The ExecuteScalar method of SqlCommand is used to execute the query and return the first column of the first row in the result set returned by the query, because the type of the return value cannot be determined, so the return value is the object type. Executes a executereader with multiple rows of result sets.
SqlDataReader reader = cmd. ExecuteReader ();..
while (reader. Read ())
{Console.WriteLine (reader). GetString (1));
}
Reader's GetString, GetInt32 and other methods only accept integer parameters, that is, the sequence number, with the GetOrdinal method according to the column name dynamic get the serial number. Why use using. Close: Can be opened after closing. Dispose: Destroyed directly and cannot be used again. The using of Dispose, which calls Dispose,sqlconnection, FileStream, and so on after the scope has been scoped, makes the judgment that there is no close, and if not, close then dispose.
The SQL statement uses @username to "replace with arguments here" to add parameters to SqlCommand's parameters. Ways to protect against injection exploits: Do not use SQL statement stitching and assign values through parameters. Cmd.commandtext = "SELECT * from t_users where [email protected] and [email protected]";
Cmd. Parameters.Add (New SqlParameter ("UserName", "admin"));
Cmd. Parameters.Add (New SqlParameter ("Password", Password));
SqlDataReader for small data volumes of data, only trouble, the advantages can be neglected. Ado. NET provides a mechanism for the dataset to populate the query results into local memory so that the connection is broken and the server disconnects without affecting the reading of the data. DataSet DataSet = new DataSet (); SqlDataAdapter adapter = new SqlDataAdapter (cmd); Adapter. Fill (DataSet); SqlDataAdapter is a bridge between a dataset and a database. The dataset DataSet contains several tables datatable,datatable contains several rows of DataRow. foreach (DataRow row in DataSet. Tables[0]. Rows) row["Name"].
The value types (int, Guid, bool, and so on) in C # are not nullable, and int i=null is wrong, so these types, such as int, BOOL, cannot represent "Null" in the database. C # therefore provides a "nullable type" syntax, as long as the type after the addition of a nullable data type, such as int, bool, such as int? I=null can do it. Resolves an issue in which int in a database can be null, whereas int cannot be NULL in C #. Type conversions: Non-nullable types are assigned to nullable types without explicit conversions (which must be successful), and nullable types are assigned to non-nullable types that require explicit conversions (not necessarily successful).
Ado. NET Foundation