Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 20th November 2011
Guesuper Guesuper is offline
Ceriwiser
 
Join Date: Nov 2011
Posts: 408
Rep Power: 14
Guesuper mempunyai hidup yang Normal
Default Microsoft Enterprise Library, Shortcut Untuk Microsoft Developer !!!

Microsoft Enterprise Library merupakan kumpulan blok kode yang dapat digunakan untuk aplikasi yang dirancang guna membantu programmer mendevelope RAD aplikasi.



Di dalam Library meliputi: Caching Application Block, Cryptography Application Block, Data Access Application Block, Exception Handling Application Block, Logging Application Block, Policy Injection Application Block, Security Application Block, Validation Application Block dan Unity Application Block.



Download Enterprise Library disini



Rilis utama dari Enterprise Library ini berisi berbagai fitur baru yang menarik dan update yang akan membuat programmer lebih produktif bahkan pemula sekalipun alias newbie.



Block yang disediakan :



* Caching Application Block.

* Cryptography Application Block.

* Data Access Application Block.

* Exception Handling Application Block.

* Logging Application Block.

* Policy Injection Application Block.

* Security Application Block.

* Unity Application Block.

* Validation Application Block.



Sample penggunaan :



Misal kita ingin membuat Multiple Updates dalam sebuah Transaction :



Kita buat dulu Store Procedurenya :




Code:

CREATE PROCEDURE credit
@AccountNo CHAR(20),
@Amount SMALLMONEY
AS
INSERT Credits
VALUES (@AccountNo, @Amount)
GO

CREATE PROCEDURE debit
@AccountNo CHAR(20),
@Amount SMALLMONEY
AS
INSERT Debits
VALUES (@AccountNo, @Amount)
GO

Langkahnya :



1. Konfigurasi koneksi ke database.

2. Buat database (jika tidak menggunakan pendekatan Unity Integration). Code berikut untuk memmakai factory untuk membuat sebuah object Database yang sudah mempunyai default configuration.



VB




Code:

Dim db As Database = DatabaseFactory.CreateDatabase()

C#




Code:

Database db = DatabaseFactory.CreateDatabase();

3. Membuat Dbcommand yang dipakai untuk execute stored procedures (CreditAccount and DebitAccount).



VB




Code:

Dim creditCommand As DbCommand = db.GetStoredProcCommand("CreditAccount")

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

Dim debitCommand As DbCommand = db.GetStoredProcCommand("DebitAccount")

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

C#




Code:

DbCommand creditCommand = db.GetStoredProcCommand("CreditAccount");

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

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

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

4. Lakukan update ke banyak table dalam sebuah Transaction.



VB




Code:

Using connection As DbConnection = db.CreateConnection()
connection.Open()
Dim transaction As DbTransaction = connection.BeginTransaction()

Try

db.ExecuteNonQuery(creditCommand, transaction)
db.ExecuteNonQuery(debitCommand, transaction)
transaction.Commit()

result = True
Catch
transaction.Rollback()
End Try

connection.Close()
Return result
End Using

C#




Code:

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

try
{
db.ExecuteNonQuery(creditCommand, transaction);
db.ExecuteNonQuery(debitCommand, transaction);

transaction.Commit();

result = true;
}
catch
{
transaction.Rollback();
}
connection.Close();

return result;
}





Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 12:47 AM.


no new posts