使用DataSet進行工作
使用Database類的ExecuteDataSet方法擷取DataSet對象,在DataSet對象中,預設的表名稱依次為Table,Table1,Table2.。。。。。。。。。。。。
如果你想要將資料載入到一個已經存在的DataSet對象中,可以使用LoadDataSet方法。
DataSet productDataSet;
// Using a SQL statement and a parameter array.
productDataSet = db.ExecuteDataSet(CommandType.Text, "GetProductsByCategory",
new Object[] { "%bike%" });
// Using a stored procedure and a parameter array.
productDataSet = db.ExecuteDataSet("GetProductsByCategory",
new Object[] { "%bike%" });
// Using a stored procedure and a named parameter.
DbCommand cmd = db.GetStoredProcCommand("GetProductsByCategory");
db.AddInParameter(cmd, "CategoryID", DbType.Int32, 7);
productDataSet = db.ExecuteDataSet(cmd);
將DataSet中的資料更新回資料庫
如果要更新資料庫,可以使用Database的UpdateDataSet方法,返回受影響的行數總和。
有一個參數是UpdateBehavior,決定了更新目標資料庫表的方式。包括下面的幾個值:
Standard:如果更新失敗,停止更新。
Continue:如果更新失敗,將繼續其他的更新。
Transactional:如果更新失敗,所有的操作都將復原。
另外你還可以為UpdateDataSet方法的UpdateBatchSize參數提供一個值,設定更新方式為批次更新而不是一個一個的發送到資料庫。這麼做更高效,但是傳回值就是最後一次批量發送的影響行數了,而不是所有的更新總數。
載入資料到一個已經存在的DataSet
string selectSQL = "SELECT Id, Name, Description FROM Products WHERE Id > 90";
// Fill a DataSet from the Products table using the simple approach
DataSet simpleDS = defaultDB.ExecuteDataSet(CommandType.Text, selectSQL);
DisplayTableNames(simpleDS, "ExecuteDataSet");
// Fill a DataSet from the Products table using the LoadDataSet method
// This allows you to specify the name(s) for the table(s) in the DataSet
DataSet loadedDS = new DataSet("ProductsDataSet");
defaultDB.LoadDataSet(CommandType.Text, selectSQL, loadedDS,
new string[] { "Products" });
DisplayTableNames(loadedDS, "LoadDataSet");
更新資料庫UpdateDataSet
string addSQL = "INSERT INTO Products (Name, Description) VALUES (@name,
@description);";
string updateSQL = "UPDATE Products SET Name = @name, Description = @description
WHERE Id = @id";
string deleteSQL = "DELETE FROM Products WHERE Id = @id";
// Create the commands to update the original table in the database
DbCommand insertCommand = defaultDB.GetSqlStringCommand(addSQL);
defaultDB.AddInParameter(insertCommand, "name", DbType.String, "Name",
DataRowVersion.Current);
defaultDB.AddInParameter(insertCommand, "description", DbType.String,
"Description", DataRowVersion.Current);
DbCommand updateCommand = defaultDB.GetSqlStringCommand(updateSQL);
defaultDB.AddInParameter(updateCommand, "name", DbType.String, "Name",
DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand, "description", DbType.String,
"Description", DataRowVersion.Current);
defaultDB.AddInParameter(updateCommand, "id", DbType.String, "Id",
DataRowVersion.Original);
DbCommand deleteCommand = defaultDB.GetSqlStringCommand(deleteSQL);
defaultDB.AddInParameter(deleteCommand, "id", DbType.Int32, "Id",
DataRowVersion.Original);
Finally, you can apply the changes by calling the UpdateDataSet method, as shown here.
// Apply the updates in the DataSet to the original table in the database
int rowsAffected = defaultDB.UpdateDataSet(loadedDS, "Products",
insertCommand, updateCommand, deleteCommand,
UpdateBehavior.Standard);
Console.WriteLine("Updated a total of {0} rows in the database.", rowsAffected);
以資料庫連接為基礎的事務操作
就是操作一個資料庫,以一個資料庫連接為基礎。
一個常用的資料庫解決方案,都會包括一些事務的處理,例如銀行的轉賬。
事務要滿足ACID原則:
Atomicity,原子性,所有的操作要麼都成功,要麼都失敗。
Consistency,一致性,資料在事務之前和之後保持資料的一致性。
Isolation,隔離性,在事務操作的過程中,其他動作不能訪問和查看資料。
Durability,一旦事務成功,在系統中產生的變化將是永久的。
詳細介紹參看:http://www.360doc.com/content/08/0426/18/61497_1217187.shtml
using (DbConnection conn = db.CreateConnection())
{
conn.Open();
DbTransaction trans = conn.BeginTransaction();
try
{
// execute commands, passing in the current transaction to each one
db.ExecuteNonQuery(cmdA, trans);
db.ExecuteNonQuery(cmdB, trans);
trans.Commit(); // commit the transaction
}
catch
{
trans.Rollback(); // rollback the transaction
}
}
分布式的事務
如果你的事務包含了訪問不同的資料庫,或者是包括了訊息佇列的訪問,你必須使用分布式的事務。例如windows的Distributed transaction coordinator(DTC)服務。
使用TransactionScope類
using (TransactionScope scope
= new TransactionScope(TransactionScopeOption.RequiresNew))
{
// perform data access here
}
擴充模組使用其他類型的資料庫
企業庫預設支援SQL Server、Oracle和SQL CE。但是你可以擴充來支援更多的資料庫。自訂一個provider或者是引入第三方的provider。
如果是自訂一個provider,你可以繼承Database類,然後重寫裡面的一些方法。在方法中處理一些細節,例如傳回值,參數的首碼(@),資料類型轉換,和其他的一些相關因素。