Page 97 - DCAP312_WEB_TECHNOLOGIES_II
P. 97

Unit 5: Database Access



                              Data Source=C:/Workers.mdb”;                                        Notes
                          Con.Close();
                     MessageBox.Show( “Database Closed”);
                     Con.Dispose();
                     }
            Run your programme and you should see the two message boxes display. The form will display
            after you click OK on these.
                          Do not calls Close or Dispose on a Connection, a DataReader, or any other
                          managed object in the Finalize method of your class. In a finalizer, you should
                          only release unmanaged resources that your class owns directly. If your class
                          does not own any unmanaged resources, do not include a Finalize method
                          in your class definition.

            5.3 Database Command


            Commands are issued next to databases to receive trial against data stores and to include any
            declaration that can be issued adjacent to a database. You can use the OleDbCommand or the
            SqlCommand classes to get a command to your data store, and OleDbCommand can be precise
            to the data stock up The SqlClient class (to connect to a computer that is running Microsoft SQL
            Server) and the OleDb class (for any database that has an OLE DB or ODBC driver available)
            within ADO.NET. However, the code is generally the same for both.

            With ADO, you can issue commands through the Command, the Connection, or the Recordset
            object.  In  ADO.NET,  only  the  Command  objects  (SqlCommand  or  OleDbCommand)  run
            commands.
            To run a command, follow these steps:

            Follow these steps to create a new console application in Microsoft Visual C# 2005 or in Microsoft
            Visual C# .NET:
               •  Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.

               •  On the File menu, point to New, and then click Project.
               •  In the New Project dialog box, click Visual C# Projects under Project Types, and then click
                 Console Application under Templates.
            Make sure that your project contains a reference to the System.Data namespace, and add a
            reference if it does not.
            Use the using statement on the System and System.Data namespaces so that you do not have to
            qualify declarations in those namespaces later in your code. You can also include System.Data.
            SqlClient or System.Data.OleDb, depending on which one you are using.
                     using System;
                     using System.Data;
                     using System.Data.SqlClient;
            Before you can create a connection to a database, you must have a connection string. Connection
            strings contain all of the information that you need to establish a database connection, including
            the server name, the database name, the user ID, and the password. For example, the following
            connection string points to a local computer that is running SQL Server:





                                             LOVELY PROFESSIONAL UNIVERSITY                                    91
   92   93   94   95   96   97   98   99   100   101   102