Represents an open connection to a data source.
Object
Component
DBConnection
ADOConnection
[Visual Basic] Public Class ADOConnection Inherits DBConnection [C#] public class ADOConnection : DBConnection [C++] public __gc class ADOConnection : public DBConnection [JScript] public class ADOConnection extends DBConnection
An ADOConnection object represents a unique session with a data source. In the case of a client/server database system, it may be equivalent to a network connection to the server. Depending on the functionality supported by the provider, some collections, methods, or properties of an ADOConnection object may not be available.
When an instance of ADOConnection is created, the read/write properties are set to initial values. For a list of these values, see the ADOConnection constructor.
Namespace: System.Data.ADO
Assembly: System.Data.dll
The following example uses the ADOCommand, along ADODataSetCommand and ADOConnection, to select records from an Access data source. The DataSet is then used to insert new records in a SQL Server data source. This example can be modified to migrate an Access database to a SQL Server database.
[C#]
private DataSet InitializeMyDataSet() { // creates the table and its columns for the DataSet DataSet newData = new DataSet(); DataTable newTable = newData.Tables.Add("Catagories"); // add Columns to table newTable.Columns.Add("CategoryID",typeof(Int32)); newTable.Columns.Add("CategoryName", typeof(String)); newTable.Columns.Add("Description", typeof(String)); newTable.Columns.Add("Picture",typeof(Byte[])); return newData; } public void AccessToSQLServer() { // set Access connection and select strings const string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NWIND_RW.MDB"; const string strAccessSelect = "SELECT * FROM Categories ORDER BY CategoryID"; // set SQL Server connection and insert strings const string strSQLServerConn = "Data Source=VBsql7;Initial Catalog=Northwind;User ID=sa;Password=;"; const string strSQLServerInsert = "INSERT INTO Categories(CategoryName, Description, Picture) Values(@CategoryName,@Description,@Picture)"; // create my DataSet DataSet myDataSet = InitializeMyDataSet(); // create my Access objects ADOConnection myAccessConn = new ADOConnection(strAccessConn); ADODataSetCommand myAccessDataSetCmd = new ADODataSetCommand(); myAccessDataSetCmd.SelectCommand = new ADOCommand(strAccessSelect,myAccessConn); // create my SQL Server objects SQLConnection mySQLConn = new SQLConnection(strSQLServerConn); SQLDataSetCommand mySQLDataSetCmd = new SQLDataSetCommand(); mySQLDataSetCmd.InsertCommand = new SQLCommand(strSQLServerInsert,mySQLConn); // fill myDataSet with data from Access myAccessConn.Open(); try { myAccessDataSetCmd.FillDataSet(myDataSet,"table"); } finally { myAccessConn.Close(); } // do ugly work to turn original rows into new rows // update SQL Server with data from the DataSet mySQLConn.Open(); try { mySQLDataSetCmd.InsertCommand.Parameters.Add("CategoryName", typeof(String)); mySQLDataSetCmd.InsertCommand.Parameters.Add("Description", typeof(String)); mySQLDataSetCmd.InsertCommand.Parameters.Add("Picture", typeof(Byte[])); mySQLDataSetCmd.InsertCommand.Parameters[0].SourceColumn = "CategoryName"; mySQLDataSetCmd.InsertCommand.Parameters[1].SourceColumn = "Description"; mySQLDataSetCmd.InsertCommand.Parameters[2].SourceColumn = "Picture"; mySQLDataSetCmd.Update(myDataSet,"table"); } finally { mySQLConn.Close(); } }
ADOConnection Members | System.Data.ADO Namespace | ADODataSetCommand | ADOCommand