ADO.NET ----------Introducing Databases -------------------------Database in simple terms means a collection of structured information.

D atabases can store data in an organized and structured manner to facilitate the retrieval of data as and when required. A collection of records, that is rows, where each column is a field, bec omes a table. In its most common form, a database is just a collection of one or more tables. A simple collection of tables is a certain type of a database. DataSource communication --------------------------------Datasource is a place where we store the information which can be either a file or a database. While developing applications, information has to be stor ed under these data sources. But there is no language available, which can commu nicate with the data source directly, because each data source adopts a differen t protocol for communication. To overcome this problem, Microsoft provides a solution with two differe nt components like DRIVERS and PROVIDERS. These components act as mediators betw een the application and the datasource. Originally, when the need of data source communication was needed, Micro soft provided the solution in the form of DRIVERS to communicate with the databa se. These drivers are of two types 1. JET drivers Expanded as Joint Engine Technology drivers. These are designed for comm unicating with the local database such as Foxpro, dbase etc. 2. ODBC drivers Open Database Connectivity Drivers are designed for communicating with r emote databases, such as Oracle, SQL*Server etc. The major disadvantage with ODBC drivers is that the drivers hav e to be installed on the system, where the application is running and then confi gure with the application. To overcome the above drawback of ODBC drivers, OLEDB providers were int roduced. OLE DB (Object Linking and Embedding Database) providers are also desig ned for communicating with the data sources. OLEDB providers are a part of your application and do not require any separate installations and configurations lik e ODBC drivers. The drawback with drivers and providers is that - as they are designed w ith the native code language, they lead to platform dependency. In the traditional days, the language Visual Basic from Microsoft was no t capable of communicating with the native code drivers and providers. To overco me this problem, Microsoft developed some intermediate components which helps VB language to communicate with drivers and providers. These components are DAO (D ata Access Objects), RDO (Remote Data Objects) and ADO (ActiveX Data Objects). ADO.NET ----------When Microsoft launched .NET, the drivers and providers could not be use d for .NET application development, because they were O.S. dependent. Hence, out of the three components DAO, RDO and ADO, Microsoft picked up ADOs and re-imple mented it for you as ADO.NET(ActiveX Data Objects). ADO.NET is a collection of m anaged providers used for data source communication. .NET Framework Data Providers ---------------------------------------The data providers are used for connecting to a database, executing comm ands and also retrieving results. The data provider in ADO.NET consists of four main objects. 1. Connection

NET framework data provider for OLEDB. Once t he syntax of the connection string is validated. ii) OleDbConnection : designed to provide connections to a wide range of databas es such as Ms-Access and Oracle. a) . Provides access to SQL*Server. Available in the namespace "System. The base class for all the 'DataAdapter' objects is "DbDataAdapter" class.OracleClient". The .NET framework provides two types of command classes namely i) SqlCommand : designed to execute commands against an SQL Server database. Available in the namespace "System. ii) OleDbCommand : designed to execute commands against Ms-Access. The . DataAdapter --------------The 'DataAdapter' object is used to update the 'DataSet' with data from the corresponding data source. using/from which you can read the corresponding rows. d) .Data. Provides access to data soruces exposed by using OLEDB. Command 3.Data.Data.Odbc". b) . Provides access to Oracle data source through Oracle client connectivity software. Oracle etc.NET framework provides two types of connection classes namely i) SqlConnection : designed to connect to Microsoft SQL Server.NET framework data provider for SQL Server. It helps you in executing the INSERT . DataReader -------------The 'DataReader' provides a forward-only and read-only connected results et.NET framework data provider for ODBC. Command -----------The 'Command' object is used to execute a command against a data source and retrieve a 'DataReader' or a 'DataSet'.NET framework data provider for Oracle. The base class for all the C ommand objects is "DbCommand". The base class is "Dat aReader". The base class for all the Connection objects is "DbConnection" class. Available in the namespace "System. DataAdapter Connection -------------The 'Connection' object is used to connect to the data source. Available in the namespace "System.OleDb". UPDATE or DELETE command against the data source. The following are the list of data providers available.2.Data. The syntax of the connection string depends on the data provider. The "DbConnectionSt ringBuilder" is the base class for creating connection strings. DataReader 4. the data source opens the conne ction. c) . Provides access to data sources exposed by using ODBC.S qlCient". You can get or set the connection string using the property "ConnectionSt . Connection Strings ---------------------A connection string contains the initialization information that is pass ed as a parameter from the data provider to the data source.

The v alue must be enclosed within double quotations. c)void Open() Opens the database connection. OleDbConnection object).Jet. To connect to Ms-A ccess. b) State Returns the current state of the connection. Creates an 'OleDbCommand' object. Constructor -------------i) OleDbConnection(). OleDbConnection class ---------------------------The 'OleDbConnection' class helps you to create a connection object. b) Data Source Represents the path of the database file stored in your machine.Data Source=F:\\C#1015\\Catalogue. Properties -----------i) Connection Allows you to specify the connection object to be used. Eg : "Provider=Microsoft. using which you can issue commands against the data source. The syntax for each connection s tring is different for the data sources. The basic syntax of a connection string includes a series of keywords se parated by semi-colons. iii) OleDbCommand(string cmdText. The "=" sign connects each keyword with its value. Methods ---------a) BeginTransaction() Starts a transaction.0.0". ii) CommandText Allows you to specify the SQL command (INSERT. Properties ------------a) ConnectionString Allows you to create a connection string. UPDATE). The connection string consists of two keywords namely a) Provider Specifies the type of database that you want to connect to. OleDbCommand class ------------------------The 'OleDbCommand' class allows you to create a 'Command' object. you can use the provider "Microsoft.Jet. d) OleDbCommand CreateCommand(). ii) OleDbCommand(string cmdText).4. you must first create a connection string. b)void Close() Closes the database connection. DELETE. Working with OLEDB providers ------------------------------------Connection String for Ms-Access ---------------------------------------In order to work out with Ms-Access.4. Methods .OLEDB. Constructors --------------i) OleDbCommand().OLEDB.mdb".ring". ii) OleDbConnection(String connectionString).

4.Open(). The classes include - . To create an 'OleDbDataReader' object use the 'ExecuteRead er()' method of the 'OleDbCommand' object. ii) bool Read() Advances the pointer to the next row in the table. or go backward. ii) int ExecuteNonQuery().Data. iv) byte GetByte(int index). vi) decimal GetDecimal(int index). Oracle etc. xii) string GetString(int index).NET framework data provider for ODBC -------------------------------------------------------------Provides access to data sources exposed by using ODBC. Retrieves the value out of the specified index. vii) double GetDouble(int index).State. Methods ---------i) void Close() Closes the DataReader object. You cannot choose any row you want. Working with the . viii) float GetFloat(int index). Available in the namespace "System. con.ConnectionString = "Provider=Microsoft. you can access a wide range of databases such as Ms-Access.Jet. Returns a resultset by the way of a 'DataReader' object. Executes the SQL statement against the connection and returns the number of row s affected.---------i) Cancel() Tries to cancel the command.mdb". iii) object ExecuteScalar(). private void button1_Click(object sender.ToString()). Properties ------------i) FieldCount Gets the number of columns available in the current row. EventArgs e) { OleDbConnection con = new OleDbConnection().OLEDB. you ca n read rows only one after the other.Show(con. ix) short GetInt16(int index).Data Source =F:\\C#1015\\Catalogue. MessageBox. xi) long GetInt64(int index).Odbc". iii) bool GetBoolean(int index). xiii) object GetValue(int index). x) int GetInt32(int index). iv) OleDbDataReader ExecuteReader(). con. } OleDbDataReader class ----------------------------The 'OleDbDataReader' class gives you a way of reading a forward-only st ream of data rows from a database. v) char GetChar(int index). Returns a single value from a database query.0. Because this stream is "forward-only". Using this namespace. Data readers are low-level objects that give you a direct access to th e data in a database.

S qlCient". SqlDataReader 4. 2.NET framework data provider for SQL Server ----------------------------------------------------------------Provides access to SQL*Server. Working with . 6. OdbcDataAdapter Creating a connection string ----------------------------------The connection string for an ODBC connection takes the following keyword s.Database Engine b) Server Name . iii) PWD Specifies the password for the user. There are 2 types of auth entication i) Windows Authentication In Windows Authentication. To work with S QL Server it provides a 'Development Management Studio' using which you can inte ract with the database.Data. Connecting to SQL Server 2005 --------------------------------------1.NET.1 2. 4. In the authentication dialog specify t he "user name" and "password" and click on the button "Test Connection" to check if the connection works out or not. i) DSN Specifies the Data Source Name created from the ODBC Manager. This provider again provides us with 4 classes namely 1. Available in the namespace "System. 7. c) Authentication . To work out with SQL Server . SqlDataAdapter Working with Microsoft SQL Server 2005 -------------------------------------------------SQL Server is the default database for . Fig. Ultimately click on the button "Ok". Specify a name for the connection using the option "Data Source Name". Select the driver related to Oracle / Ms-Access and click on the button "Fini sh".i. OdbcConnection ii. 5. Start the SQL Server Management Studio. Click on the button "Test Connection". Select Start -> Settings -> Control Panel -> Administrative Tools -> Data Sou rces (ODBC). Then s pecify the user name related with Oracle using the option "User Id". OdbcCommand iii. SqlCommand 3. Procedure to create a DSN --------------------------------1. OdbcDataReader iv. Use the following options a) Server Type . you need not specify the user name and passwo .Specifies the name of the machine. 3. It provides the following authenticat ion screen. It opens the Microsoft ODBC Data Source Administrator. SqlConnection 2. ii) UID Specifies the underlying user name. It displays the list of drivers available. first of all you need to install SQL Server into your machines.Specifies the user authentication. Double click on it. Click on the button "Add".

In the 'Object Explorer' window. CREATING A CONNECTION STRING FOR SQL SERVER ---------------------------------------------------------------The connection string for SQL Server includes the following parameters a) Data Source Represents the name or the network address of the instance of the SQL Server to which you want to connect. 2. click on the button "New Query". you will find the button "Execute" on the toolbar itsel f. you can also create tables using the sql statement 'CREATE TABLE' . cost int. you specify the user name which is "sa" by default and then you specify the password (password set during installation of SQL Server). category varchar(50). On the right hand side it opens a file with the extension . Type your required query in it. ii) SQL Server Authentication In SQL Server Authentication. After specifying the above options. click on the button "Connect". Specify a table name and click on the button 'Save'. It then prompts you for the name of the table.sql. It connect s you to the SQL Server Database. Use the following steps. (or) Alternatively. b) Initial Catalog Represents the name of the database in SQL Server to which you want to connect. where you can spec ify the required column names along with their data-types. 5. 5.rd. 1. 2. 3. copies int). 3. To execute the query. . After specifying the columns close the window. It then invokes the 'Table structure' on the right window. from which select the menu option "New Table". author varchar(50). Select the option 'Tables' and right click on it. 3. book_name varchar(50). For example ---------------create table books (book_id int. 4. Click on the button to execute the query. 4. In the toolbar. expand the tree Databases -> System Database s -> master -> Tables. Creating Tables ------------------1. It displays a context menu.

pwd.Initial Catalog=master.Connection = con. cmd. cmd.Read()) { MessageBox. string uid. if (reader.Read()) { uid = reader.ExecuteReader().Show(con. EventArgs e) { con = new SqlConnection(). SqlCommand cmd.Connection = con. cmd.ConnectionString = "Data Source=PATRICKS.Open(). If the value is 'True'.Text + "'".CommandText = "select * from users where user_name='" + textBox1 . . MessageBox.State. cmd. then the current Window s accounts credentials are used for authentication. public partial class Form1 : Form { SqlConnection con.CommandText = "select * from users". cmd.Dispose().Text + "' and password='" + textBox2. Integrated Security=True".Initial Catalog=master.Show("Valid user with a valid password"). } private void Form1_Load(object sender. EventArgs e) { /* cmd = new SqlCommand(). */ cmd = new SqlCommand(). reader = cmd.Dispose().c) Integrated Security Accepts two values .GetValue(0).ToString().ToString()).Text) { pwd = reader. con.True or False. reader = cmd. } else { MessageBox.Show("Invalid user or Invalid password").ExecuteReader(). } private void button2_Click(object sender. while (reader. SqlDataReader reader.GetValue(1). then you need to s pecify the user id and password. if (uid == textBox1. } reader. The default is 'True'. public Form1() { InitializeComponent(). If the value is 'False'. con.Integrated Security=True". For example : -----------------"Data Source=system-name. int count=0.ToString().

. you can then create a child windo w for the parent window. The best exampl e for an MDI application is Ms-Word. To do so. } else { count = 1. This makes the form an MDI parent wi ndow. break. reader. } cmd.Text) { count = 1.Show("Valid user with a valid password"). } } } if (count == 0) { MessageBox. } } SDI/MDI Applications -------------------------An SDI (Single Document Interface) application is one in which you can o pen only one document at a time.Focus(). MessageBox. first of all add a new form to your applicati on using the menu option Project -> Add Windows Form. In an MDI application. MessageBox. Set the name of the MDI Form to the "Md iParent" property. textBox1.Dispose(). The parent window does not allow t he child window to move out of its boundaries. Once added. Now to make this form an MDI parent window. you have a parent wi ndow in which the child windows are embedded. On the other hand a MDI (Multiple Document Interface) application is one in which a user can open more than one document simultaneously. invoke the 'Properties' dialog box and s et the property "IsMdiContainer" to "True". Creating an MDI parent window --------------------------------------To create an MDI parent window.if (pwd == textBox2. break. This project by default adds a form with the name "Form1".Focus(). first of all open a 'Windows Forms Appli cation' Project. the newly added form can be set as a child to the paren t window using the property "MdiParent". You can add as many forms as you want.Dispose(). Creating an MDI Child window ------------------------------------Once a parent window has been created. The best example for an SDI application is a "N otepad".Show("Invalid password !").Show("Invalid username"). textBox2.

Sign up to vote on this title
UsefulNot useful