Lecture 10 - ADO - NET (Part 1)
Lecture 10 - ADO - NET (Part 1)
6 Framework
// Methods
void Commit ();
void Rollback ();
}
}
The Role of the IDCommand Interface
namespace System.Data
{
public interface IDbCommand : IDisposable
{
// Properties
string CommandText {get; set;}
int CommandTimeout {get; set;}
CommandType CommandType {get; set;}
IDbConnection Connection {get; set;}
IDataParameterCollection Parameters {get; }
IDbTransaction Transaction {get; set;}
UpdateRowSource UpdatedRowSource {get; set;}
// Methods
void Cancel ();
IDbDataParameter CreateParameter ();
int ExecuteNonQuery ();
IDataReader ExecuteReader ();
IDataReader ExecuteReader (CommandBehavior behavior);
object ExecuteScalar ();
void Prepare ();
}
}
The Role of the IDbDataParameter and
IDataParameter Interfaces
namespace System.Data
{
public interface IDbDataParameter : IDataParameter
{
// Properties
byte Precision {get; set;}
These allow you to represent parameters
byte Scale {get; set;} within a SQL command (including stored
int Size {get; set;} procedures) through specific ADO.NET
}
}
parameter objects, rather than through
namespace System.Data hard-coded string literals.
{
public interface IDataParameter
{
// Properties
DbType DbType { get; set;}
ParameterDirection Direction { get; set;}
bool IsNullable { get; }
string ParameterName { get; set;}
string SourceColumn { get; set;}
DataRowVersion SourceVersion { get; set;}
object Value { get; set;}
}
}
The Role of the IDbDataAdapter and
IDataAdapter Interfaces
namespace System.Data
{
public interface IDbDataAdapter : IDataAdapter
{
// Properties
IDbCommand DeleteCommand { get; set;}
IDbCommand InsertCommand { get; set;}
You use data adapters to push and pull
IDbCommand SelectCommand { get; set;} DataSets to and from a given data store.
IDbCommand UpdateCommand { get; set;}
}
}
namespace System.Data
{
public interface IDataAdapter
{
// Properties
MissingMappingAction MissingMappingAction { get; set;}
MissingSchemaAction MissingSchemaAction { get; set;}
ITableMappingCollection TableMappings { get; }
// Methods
int Fill (DataSet dataSet);
DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType);
IDataParameter[] GetFillParameters ();
int Update (DataSet dataSet);
}
}
The Role of the IDataReader Interface
public interface IDataReader : IDisposable, IDataRecord
{
int Depth { get; }
bool IsClosed { get; }
int RecordsAffected { get; }
void Close();
DataTable GetSchemaTable();
bool NextResult();
bool Read();
}
The Role of the IDataRecord Interface
public interface IDataRecord
{
int FieldCount { get; }
object this[int i] { get; }
object this[string name] { get; }
string GetName(int i);
string GetDataTypeName(int i);
Type GetFieldType(int i);
object GetValue(int i);
int GetValues(object[] values);
int GetOrdinal(string name);
bool GetBoolean(int i);
byte GetByte(int i);
long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length);
char GetChar(int i);
long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length);
Guid GetGuid(int i);
short GetInt16(int i);
int GetInt32(int i);
long GetInt64(int i);
float GetFloat(int i);
double GetDouble(int i);
string GetString(int i);
Decimal GetDecimal(int i);
DateTime GetDateTime(int i);
IDataReader GetData(int i);
bool IsDBNull(int i);
}
System.Data.Common namespace
• DbCommand:
– The abstract base class for all command classes
• DbConnection:
– The abstract base class for all connection classes
• DbDataAdapter:
– The abstract base class for all data adapter classes
• DbDataReader:
– The abstract base class for all data reader classes
• DbParameter:
– The abstract base class for all parameter classes
• DbTransaction:
– The abstract base class for all transaction classes
The ADO.NET Data Provider Factory
Model
namespace System.Data.Common
{
public abstract class DbProviderFactory
{
// Properties
public virtual bool CanCreateDataSourceEnumerator{get;}
// Constructors
protected DbProviderFactory();
// Methods
public virtual DbCommand CreateCommand();
public virtual DbCommandBuilder CreateCommandBuilder();
public virtual DbConnection CreateConnection();
public virtual DbConnectionStringBuilder CreateConnectionStringBuilder();
public virtual DbDataAdapter CreateDataAdapter();
public virtual DbDataSourceEnumerator CreateDataSourceEnumerator();
public virtual DbParameter CreateParameter();
public virtual CodeAccessPermission CreatePermission(PermissionState state);
}
}
…
// Get the factory for the SQL data provider.
DbProviderFactory sqlFactory =
DbProviderFactories.GetFactory("System.Data.SqlClient");
...
Creating the database AutoLot
create database AutoLot;
use AutoLot;
create table Inventory(
CarId int auto_increment primary key,
Make nvarchar(50),
Color nvarchar(50),
PetName nvarchar(50)
);
insert into Inventory(Make, Color, PetName) values
('VW', 'Black', 'Zippy'),
('Ford', 'Rust', 'Rusty'),
('Saab', 'Black', 'Mel'),
('Yogo', 'Yellow', 'Clunker'),
('BMW', 'Black', 'Bimmer'),
('BMV', 'Green', 'Hank'),
('BMV', 'Pink', 'Pinky');
Creating the database AutoLot
create table Customers(
CustId int auto_increment primary key,
FirstName nvarchar(50),
LastName nvarchar(50)
);
Note:
For this example to run, you need to add the MySql.Data Package.
Summary
References
Troelsen, A. & Japikse, P., 2015. C# 6 and the
.NET 4.6 Framework. 7th ed. Apress.