What did I learn from the QucikStart about DAAB?
1.DatabaseFactory.CreateDatabase():Method for invoking a default Database object. Reads default settings from the ConnectionSettings.config file.
Database dbSvc = DatabaseFactory.CreateDatabase();
2.Database.GetSqlStringCommand(string query):Create a DbCommand for a SQL query.
Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
"From Customers";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
3.Database.ExecuteReader(DbCommand command):Executes the command and returns an IDataReader through which the result can be read. It is the responsibility of the caller to close the connection and reader when finished.
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
// readerDate,a instance of StringBuilder
readerData.Append(dataReader["Name"]);
readerData.Append(Environment.NewLine);
}
}
4.Database.GetStoredProcCommand(string storedProcedureName):Creates a DbCommand for a stored procedure.
string sqlCommand = "GetProductsByCategory";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
5.Database.AddInParameter(DbCommand command,string name,DbType dtType,Object value):Adds a new In DbParameter object to the given command. With the value.
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category);
6.Database.ExecuteDataSet(DbCommand command):Executes the command and returns the results in a new DataSet.
productsDataSet = db.ExecuteDataSet(dbCommand);
7.Database.AddOutParameter(DbCommand command,string name,DbType dbType,int size):Adds a new Out DbParameter object to the given command.
db.AddOutParameter(dbCommand, "ProductName", DbType.String, 50);
8.Database.ExecuteNonQuery(DbCommand command):Executes the command and returns the number of rows affected.
db.ExecuteNonQuery(dbCommand);
9.Database.ExecuteScalar(DbCommand command):Executes the command and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.
string productName = (string) db.ExecuteScalar(dbCommand);
string sqlCommand = "GetProductName";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, productID);
bool result = false;

// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();

// Two operations, one to credit an account, and one to debit another
// account.
string sqlCommand = "CreditAccount";
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);

sqlCommand = "DebitAccount";
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);

using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();

try
{
// Credit the first account
db.ExecuteNonQuery(creditCommand, transaction);
// Debit the second account
db.ExecuteNonQuery(debitCommand, transaction);

// Commit the transaction
transaction.Commit();
result = true;
}
catch
{
// Rollback transaction
transaction.Rollback();
}
connection.Close();
return result;
}
12.Database.LoadDataSet(DbCommand command,DataSet dataSet,string tableName):Executes the command and adds a new DataTable to the existing DataSet.
Database db = DatabaseFactory.CreateDatabase();

DataSet productsDataSet = new DataSet();

string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);

string productsTable = "Products";

// Retrieve the initial data
db.LoadDataSet(dbCommand, productsDataSet, productsTable);
13.Database.UpdateDataSet(DataSet dataSet,string tableName,DbCommand insertCommand,DbCommand updateCommand,DbCommand deleteCommand,UpdateBehavior updateBehavior):Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet.
string productsTable = "Products";

// Retrieve the initial data
db.LoadDataSet(dbCommand, productsDataSet, productsTable);

// Get the table that will be modified
DataTable table = productsDataSet.Tables[productsTable];

// Add a new product to existing DataSet
DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25});

// Modify an existing product
table.Rows[0]["ProductName"] = "Modified product";

// Establish our Insert, Delete, and Update commands
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);

DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);

DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);

// Submit the DataSet, capturing the number of rows that were affected
int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
deleteCommand, UpdateBehavior.Standard);
14.SqlDatabase.ExecuteXmlReader(DbCommand command):Executes the SqlCommand and returns a new XmlReader.
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products FOR XML AUTO";
DbCommand dbCommand = dbSQL.GetSqlStringCommand(sqlCommand);
XmlReader productsReader = null;
productsReader = dbSQL.ExecuteXmlReader(dbCommand);
Database dbSvc = DatabaseFactory.CreateDatabase();2.Database.GetSqlStringCommand(string query):Create a DbCommand for a SQL query.
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "Select CustomerID, Name, Address, City, Country, PostalCode " +
"From Customers";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);3.Database.ExecuteReader(DbCommand command):Executes the command and returns an IDataReader through which the result can be read. It is the responsibility of the caller to close the connection and reader when finished.
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
// readerDate,a instance of StringBuilder
readerData.Append(dataReader["Name"]);
readerData.Append(Environment.NewLine);
}
}4.Database.GetStoredProcCommand(string storedProcedureName):Creates a DbCommand for a stored procedure.
string sqlCommand = "GetProductsByCategory";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);5.Database.AddInParameter(DbCommand command,string name,DbType dtType,Object value):Adds a new In DbParameter object to the given command. With the value.
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category);6.Database.ExecuteDataSet(DbCommand command):Executes the command and returns the results in a new DataSet.
productsDataSet = db.ExecuteDataSet(dbCommand);7.Database.AddOutParameter(DbCommand command,string name,DbType dbType,int size):Adds a new Out DbParameter object to the given command.
db.AddOutParameter(dbCommand, "ProductName", DbType.String, 50);8.Database.ExecuteNonQuery(DbCommand command):Executes the command and returns the number of rows affected.
db.ExecuteNonQuery(dbCommand);9.Database.ExecuteScalar(DbCommand command):Executes the command and returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.
string productName = (string) db.ExecuteScalar(dbCommand);
10.Database.GetStoredProcCommand(string storeProcedureName,object[] parameterValues):Creates a DbCommand for a stored procedure with the parameter values.
string sqlCommand = "GetProductName";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, productID);11,How to use the Transaction?
bool result = false;
// Create the Database object, using the default database service. The
// default database service is determined through configuration.
Database db = DatabaseFactory.CreateDatabase();
// Two operations, one to credit an account, and one to debit another
// account.
string sqlCommand = "CreditAccount";
DbCommand creditCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(creditCommand, "AccountID", DbType.Int32, sourceAccount);
db.AddInParameter(creditCommand, "Amount", DbType.Int32, transactionAmount);
sqlCommand = "DebitAccount";
DbCommand debitCommand = db.GetStoredProcCommand(sqlCommand);
db.AddInParameter(debitCommand, "AccountID", DbType.Int32, destinationAccount);
db.AddInParameter(debitCommand, "Amount", DbType.Int32, transactionAmount);
using (DbConnection connection = db.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction();
try
{
// Credit the first account
db.ExecuteNonQuery(creditCommand, transaction);
// Debit the second account
db.ExecuteNonQuery(debitCommand, transaction);
// Commit the transaction
transaction.Commit();
result = true;
}
catch
{
// Rollback transaction
transaction.Rollback();
}
connection.Close();
return result;
}12.Database.LoadDataSet(DbCommand command,DataSet dataSet,string tableName):Executes the command and adds a new DataTable to the existing DataSet.
Database db = DatabaseFactory.CreateDatabase();
DataSet productsDataSet = new DataSet();
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products";
DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
string productsTable = "Products";
// Retrieve the initial data
db.LoadDataSet(dbCommand, productsDataSet, productsTable);13.Database.UpdateDataSet(DataSet dataSet,string tableName,DbCommand insertCommand,DbCommand updateCommand,DbCommand deleteCommand,UpdateBehavior updateBehavior):Calls the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet.
string productsTable = "Products";
// Retrieve the initial data
db.LoadDataSet(dbCommand, productsDataSet, productsTable);
// Get the table that will be modified
DataTable table = productsDataSet.Tables[productsTable];
// Add a new product to existing DataSet
DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25});
// Modify an existing product
table.Rows[0]["ProductName"] = "Modified product";
// Establish our Insert, Delete, and Update commands
DbCommand insertCommand = db.GetStoredProcCommand("AddProduct");
db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct");
db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct");
db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current);
db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);
// Submit the DataSet, capturing the number of rows that were affected
int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand,
deleteCommand, UpdateBehavior.Standard);14.SqlDatabase.ExecuteXmlReader(DbCommand command):Executes the SqlCommand and returns a new XmlReader.
string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate " +
"From Products FOR XML AUTO";
DbCommand dbCommand = dbSQL.GetSqlStringCommand(sqlCommand);XmlReader productsReader = null;
productsReader = dbSQL.ExecuteXmlReader(dbCommand);


浙公网安备 33010602011771号