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))
}
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())
}
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
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);
相关文章:
-
2021-06-07
-
2021-07-25
-
2022-12-23
-
2022-02-23
-
2021-07-05
-
2022-12-23
-
2022-02-06
-
2021-08-22