📄 usingdataaccess.htm
字号:
' ...
</pre></code>
<br>
<b class="SmallHdr">Reading lists of records</b>.<br>
To view which employees we've already stored in the database, we can select them from the database and
view them on a form. We define a form where the user clicks a button and all the employee records are
shown in a datagrid control on the form. The code below does all that magic for you, and is called from
the click event handler delegate of the button:
<code><pre>
/// <summary>
/// Purpose: refreshes the list of employees in the grid from the list
/// stored in the database
/// <summary>
private void RefreshEmployeeList()
{
clsEmployees oEmployees = new clsEmployees();
try
{
DataTable dtEmployees = oEmployees.SelectAll();
dgEmployees.DataSource = dtEmployees;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
</pre></code>
It can't get any simpler than this, folks!
<br><br>
<b class="SmallHdr">Deleting rows</b><br>
We want to delete a row from the Department table, because a Department is closed. This will take
two actions: first we have to remove all rows with the department in question from the DepartmentEmployees
table, and then the row for the department from the Departments table. The following code does all that:
<code><pre>
/// <summary>
/// Purpose: Deletes a department from the database.
/// <param name="iDepartmentIDToDelete">The ID of the department which should be deleted.</param>
/// <summary>
private void DeleteDepartment(int iDepartmentIDToDelete)
{
clsDepartments oDepartments = new clsDepartments();
clsDepartmentEmployees oDepartmentEmployees = new clsDepartmentEmployees();
bool bResult = false;
try
{
// first delete the rows from the DepartmentEmployees table
oDepartmentEmployees.DepartmentID = iDepartmentIDToDelete;
bResult = oDepartmentEmployees.DeleteAllWDepartmentIDLogic();
// then delete the row from the Department table
oDepartments.DepartmentID = iDepartmentIDToDelete;
bResult = oDepartments.Delete();
// done!
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
</pre></code>
This will remove the given Department from the database without violations of FK constraints
and illustrates the power of the generated code. Production code should use COM+ transactions
for this code, since it will hurt database integrity if the first call succeeds, but the
second one fails. To avoid this inconsistency, we can use COM+ transactions, but we can also
use the new ConnectionProvider object functionality of LLBLGen to make the two calls run inside
one transaction which is rolled back when something goes wrong.
<br><br>
<b class="SmallHdr">Using the ConnectionProvider class</b><br>
Below is a codesnippet which does the same as the codesnippet above, but now it uses the
ConnectionProvider object to share a SqlConnection object and transaction among the two
data-access objects so both calls of the two methods are ran in one transaction (using ADO.NET
transaction functionality). To let LLBLGen generate ADO.NET transaction support and the ConnectionProvider
class, check <i>Include Connection Provider Object support</i> on the
<a href="gui_netcode.htm">.NET code tab</a>.
<code><pre>
/// <summary>
/// Purpose: Deletes a department from the database. Now using the new
/// ConnectionProvider class functionality to make the code more reliable.
/// <param name="iDepartmentIDToDelete">The ID of the department which should be deleted.</param>
/// <summary>
private void DeleteDepartment(int iDepartmentIDToDelete)
{
bool bResult = false;
// Create the objects
clsDepartments oDepartments = new clsDepartments();
clsConnectionProvider oConnectionProvider = new clsConnectionProvider();
clsDepartmentEmployees oDepartmentEmployees = new clsDepartmentEmployees();
// Pass the created ConnectionProvider object to the data-access objects.
oDepartments.cpMainConnectionProvider = oConnectionProvider;
oDepartmentEmployees.cpMainConnectionProvider = oConnectionProvider;
// initialize the data-access objects with the key for the department to
// delete.
oDepartments.iDepartmentID = iDepartmentIDToDelete;
oDepartmentEmployees.iDepartmentID = iDepartmentIDToDelete;
try
{
// open the connection provider
bResult = oConnectionProvider.OpenConnection();
// Start a transaction and give it a name so we can reference it later.
oConnectionProvider.BeginTransaction("transDelDepartment");
// do the actual deletion.
try
{
// First the Foreign Key rows.
bResult = oDepartmentEmployees.DeleteWDepartmentIDLogic();
// Then the Primary Key row.
bResult = oDepartments.Delete();
// Done, commit transaction
bResult = oConnectionProvider.CommitTransaction();
}
catch(Exception ex)
{
// Something went wrong using the deletes, we can safely roll back.
// Use the name to reference the transaction.
bResult = oConnectionProvider.RollbackTransaction("transDelDepartment");
// Bubble error.
throw ex;
}
// Done. clean up is done in finally block
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// Close the connection, and don't commit pending transactions.
oConnectionProvider.CloseConnection(false);
}
}
</pre></code>
The code is a little longer than the routine without the ConnectionProvider object,
but it clearly shows the ease of use of this new class, generated by LLBLGen. For
business logic layers which are not part of a COM+ enabled application, this is the way
to go to make use of the generated data-access tier in a robust, reliable way, when transactions
are needed. Transactions are not always needed, f.e. when you're simply selecting a row from
the database, you don't have to use transactions.
</p>
<div align="right" class="SmallFontTOC">
<hr size="1" width="60%" align="right">
LLBLGen v1.21 documentation. © 2002-2003 <a href="http://www.sd.nl/" target="_blank">Solutions Design</a>
</div>
<br><br>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -