|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionLike ADO, ADO.NET provides the mechanism you can use to get the events raised by the managed providers when certain actions are taking place or have taken place. This is a cool feature that in some cases becomes very handy when you want to change the action based on the rules that database access layer is to follow. E.g. the application changes value of a column in a table. The application may not be aware of all the rules that it is supposed to follow to make a judgment whether the values it is changing to are legal or not. Then you can set up an event notification function for that In this article I will be using the general OLE DB based provider
Look at the reference documentation to get details about these properties. But in short I can tell you that these properties can give you complete description about the error like complete error text, source of error, etc. Here is the example on how you add event handler to your String strConnection = "Provider= SQLOLEDB.1;
Data Source=localhost; uid=sa; pwd=; Initial Catalog=northwind";
m_ADOConnection = new ADOConnection (strConnection);
m_ADOConnection.InfoMessage += new
ADOInfoMessageEventHandler (OnInfoMessageFromConnection);
m_ADOConnection.StateChange +=
new StateChangeEventHandler (OnStateChange);
And then you need to provide the event handler function for the events. E.g. in this case protected static void OnInfoMessageFromConnection
(object sender, ADOInfoMessageEventArgs args)
{
Trace.Write ("Info Message Recieved From Server");
Trace.Write (args.Message);
Trace.Write ("End Of Message From Server");
}
protected static void OnStateChange(object sender, StateChangeEventArgs e)
{
PrintEventArgs(e);
}
After you have established connection, then next step is retrieving the data from the data store and then manipulating it. The information is accessed in
The names of the events are very self-explanatory. So depending on your needs you can event handlers for these events. Here is the example on how you can do it. public static void SetEventHandlers (DataTable table)
{
table.ColumnChanging += new
DataColumnChangeEventHandler (ColumnChangingEvtHandler);
table.RowChanging += new
DataRowChangeEventHandler RowChangingEvtHandler);
table.RowChanged += new
DataRowChangeEventHandler (RowChangedEvtHandler);
table.RowDeleting += new
DataRowChangeEventHandler (RowDeletingEvtHandler);
table.RowDeleted += new
DataRowChangeEventHandler (RowDeletedEvtHandler);
}
public static void RowChangingEvtHandler
(object sender, DataRowChangeEventArgs args)
{
Trace.Write ("Row Changing: " + args.Row.ToString ());
}
public static void RowChangedEvtHandler
(object sender, DataRowChangeEventArgs args)
{
Trace.Write ("Row Changed: " + args.Row.ToString ());
}
public static void RowDeletingEvtHandler
(object sender, DataRowChangeEventArgs args)
{
Trace.Write ("Row Deleting: " + args.Row.ToString ());
}
public static void RowDeletedEvtHandler
(object sender, DataRowChangeEventArgs args)
{
Trace.Write ("Row Deleted: " + args.Row.ToString ());
}
public static void ColumnChangingEvtHandler
(object sender, DataColumnChangeEventArgs args)
{
Trace.Write ("Column Changing: " + args.Column.ColumnName);
}
Most of these events are just providing information except
Similar to the way I added events for
|
|||||||||||||||||||||||||||||||||||||||||||||||||||