Best Windows Hosting

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Sunday, 15 April 2012

ADO.NET: A quick revision

Posted on 20:15 by Unknown
It is used for data manipulation from datasource. Data manipulation is done using XML internally.

ADO.NET Namespaces:

1. System.Data (For defining ADO.NET objects )
2. System.Data.Common (Contains common classes for System.Data.OleDb and System.Data.SqlClient namespaces)
3. System.Data.OleDb (For connecting to oracle ODBC)
4. System.Data.SqlClient (For connecting to sql server)
5. System.Data.SqlTypes (Contains SQL Server types)

ADO.NET Objects

1. Objects that manage and store data (DataSet, DataTable, DataRow, DataColumn, DataView, DataReader, DataAdapter).

2. Objects that connect with specific DataSource (Connection, Command).

1.  Objects that manage and store data (DataSet, DataTable, DataRow, DataColumn, DataView, DataReader, DataAdapter)

DATASET: DataSets use disconnected access to DataSource using DataAdapters. Theses contain DataTable, DataRow, DataColumn and DataRelation. We can use ReadXML, WriteXML, Merge, Clear, AcceptChanges, GetChanges, RejectChanges commands on DataSets. But there can be concurrency problems with datasets if more than two users are updating the same record at the same time in disconnected mode. When we use DataSets, we never fire update, delete and insert query directly on database, instead we post updated database.

Retrieve logic: DataSource -> Connection Object -> Command Object -> DataAdapter -> DataSet -> UI
Write Logic:  DataSource <- Connection Object <- Command Object <- DataAdapter <- DataSet <- UI

DATAREADER: DataReaders use connected and direct access to DataSource and no DataAdapter is required. These can be SqlDataReader and OleDbDataReader.

Retrieve logic: DataSource -> Connection Object -> Command Object -> DataReader -> UI
Write Logic:  DataSource <- Connection Object <- Command Object <- DataReader <- UI

DATAADAPTER: DataAdapters are the bridges between DataSet and Command Object. These can be SqlDataAdapter and OleDbDataAdapter. We can use Fill, FillSchema and Update commands on DataAdapers.

2. Objects that connect with specific DataSource (Connection, Command)

CONNECTION: These can be SqlConnection and OleDbConnection. ConnectionString is its basic property.

COMMAND: These can be SqlCommand and OleDbCommand. We can use ExecuteNonQuery, ExecuteReader, ExecuteScaler on it.

Code snippet using DataReader (Connected Access)

Making Connection:

myConnectionString = '';
sqlConnection myConnection = new sqlConnection();
myConnection.ConnectionString = myConnectionString;

or

myConnectionString = '';
sqlConnection myConnection = new sqlConnection(myConnectionString);

myConnection.Open();
myConnection.Close();

Making Command:

sqlCommand myCommand = new sqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = myQuery;

or

sqlCommand myCommand = new sqlCommand(myQuery, myConnection);

Making DataReader:

myConnection.Open();
sqlDataReader myReader = new sqlDataReader;
myReader = myCommand.ExecuteReader();  //For reading
myReader.Read(); //Read first row. For reading all rows, iterate it with while loop.
myReader.Close();
myConnection.Close();

anyInteger = myCommand.ExecuteNonQuery(); //For inserting, updating, deleting
anyInteger = myCommand.ExecuteScaler();

Code using DataSet(Disconnected Access)

Making Connection:

myConnectionString = '';
sqlConnection myConnection = new sqlConnection(myConnectionString);
myConnection.Open();
myConnection.Close();

Making Command:

sqlCommand myCommand = new sqlCommand(myQuery, myConnection);

Making DataAdapter:

DataSet myDataSet = new DataSet();
sqlDataAdapter myAdapter = new sqlDataAdapter(myCommand);
myAdapter.Fill(myDataSet, 'myTable');  //Reading

Updating DataSet and Posting:

DELETE A ROW: myDataSet.Tables['myTable'].Rows[2].Delete
ADD A ROW: myDataSet.Tables['myTable'].Rows.Add(myDataRow);

sqlCommandBuilder cb = new sqlCommandBuilder(myAdapter);
myNewAdapter = cb.DataAdapter();
anyIntegerRowAffected = myNewAdapter.Update(myDataSet, 'myTable');

How to create a new DataRow and adding it to DataTable

DataRow dr = new DataRow();
dr = myDataSet.Tables['myTable'].NewRow();
dr['ColumnName1'] = '';
dr['ColumnName2'] = '';
myDataSet.Tables['myTable'].Rows.Add(myDataRow);
Email ThisBlogThis!Share to XShare to Facebook
Posted in DOTNET | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • 13 Things to keep in mind before using DLL in Delphi
    Keep in mind the following tips when writing your DLL: 1. Make sure you use the proper calling convention (C or stdcall). 2. Know the correc...
  • How to use TADOTable in Delphi XE2?
    Following is the code snippet which will show you how to use TADOTable in Delphi XE2? procedure TClass1.GetDataFromADOTable; begin   try    ...
  • How to use FindComponent function in Delphi XE2?
    Following is the code snippet which will show you how to use FindComponent in Delphi XE2? procedure TClass1.UseFindComponent(FieldName : str...
  • Online Finance Degrees
    There is a great demand for professionals with profound knowledge of finance and accounting in most of the reputed banks and financial insti...
  • How to grab the recruiter’s attention with your resume?
    Did you know that the average recruiter spends about 8 to 10 seconds glancing at your resume before s/he moves on to the next? So, whether y...
  • 5 ways to handle workload at your workplace
    With bigger workloads, tighter deadlines and more pressure, the temptation to pack in as many tasks as possible is hard to resist. But juggl...
  • Online Marketing Degrees
    Because global competition has become so intense, it should come as no surprise that companies invest heavily in their marketing and promoti...
  • Frameset, Frame and IFrame Elements in HTML
    Frame Element With frames, you can display more than one HTML document in the same browser window. Each HTML document is called a frame, and...
  • Oracle Streams: An Overview
    Oracle Streams enables information sharing. Each unit of shared information is called a message. The stream can propagate information within...
  • Phonegap: An amazing combination of HTML5, CSS3 and Javascript
    Phonegap (Cordova) = HTML5 + CSS3 + Javascript What a great combination!! How easy is Phonegap to learn!!! A great enhancement in mobile tec...

Categories

  • AJAX
  • C++
  • CSS
  • Delphi
  • DOTNET
  • HTML
  • Javascript
  • jQuery
  • Management
  • Online Degrees
  • Oracle
  • Others
  • Phonegap
  • PHP
  • Unix
  • XML

Blog Archive

  • ▼  2012 (155)
    • ►  September (64)
    • ►  August (11)
    • ►  July (4)
    • ►  June (3)
    • ►  May (25)
    • ▼  April (48)
      • DOM Events: Mouse Events, Keyboard Events, Form Ev...
      • What is DOM (Document Object Model): Tree and Node...
      • DocType: Strict, Transitional and Frameset
      • How the ASP.NET authentication process works?
      • Response.Redirect vs Server.Transfer: What to use ...
      • GET vs POST: Which one is better? A 10 point compa...
      • HTTP vs HTTPS: Similarities and Differences
      • How IIS processes ASP.NET request?
      • AutoEventWireup in ASP.NET: Why my ASP.NET events ...
      • Web Farms in ASP.NET: Advantages and Issues
      • Cautions while dropping a tablespace
      • 12 Point Comparision between FTP and HTTP Protocol...
      • Dataset, Dataview, Datatable and common operations...
      • Client Side State Management in ASP.NET
      • Difference between page_init, page_load and page_p...
      • Database FLASHBACK mode: Overview
      • Database ARCHIVELOG mode: Overview
      • Hash Collision Attacks in .NET
      • ADO.NET: A quick revision
      • What is SQL Injection?
      • What is ASP.NET AJAX?
      • What is DLL HELL?
      • DIV vs TABLE tag: Which one to use?
      • Exception Handling in ASP.NET
      • Business Intelligence (BI): Data Warehouse, Data M...
      • UDDI: Universal Description, Discovery and Integra...
      • Web Services: Exposing and Consuming
      • Web Service Description Language
      • Simple Object Access Protocol
      • Caching in ASP.NET
      • Partial Classes in ASP.NET
      • Difference between DLL and EXE Files
      • What is an ASP.NET User Control?
      • Page Directive in ASP.NET
      • DOTNET Framework: CLR, CTS and CLS
      • What is Tethering and Hotspot?
      • Preventing Caching in AJAX URLs
      • What is web.config file? What is the significance ...
      • HTML vs XHTML vs DHTML
      • AJAX - A quick revision
      • Web Server vs Application Server vs Database Server
      • Difference Between ASP.NET Server Controls and HTM...
      • web.config vs app.config vs machine.config
      • Assemblies in .NET Framework
      • Silver Bullets for Testing
      • Unix Commands which should be on tips of each deve...
      • Basics of IBM Websphere MQ (Part 1)
      • What is Garbage Collector? How and when does it run?
Powered by Blogger.

About Me

Unknown
View my complete profile