Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • SqlDataAdapter in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 66
    Comment on it

    While working with disconnected approach we works with storing and retrieving data from buffers to select operation.

    Here Adapter acts like a buffer where the data is contained. But for reading that data we need to store it either in DataTable or in DataSet.

    For this purpose we use SqlDataAdapter class that uses the disconnected mode for fetching data from the database.

     

     

    DataAdapter can be used with two ways :

    • OleDbDataAdapter
    • SqlDataAdapter

     

    For interacting with MsAccess or other data we can use this OleDb type adapter.

     

    using OleDbDataAdapter;
    
            private void OleDbDataAdapter_Click(object sender, System.Event Args e)
            {
    
            //Create a connection object
            string ConnectionString = @"provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source= C:/northwind.mdb";
            string SQL = "SELECT * FROM Orders";
            OleDbConnection conn = new OleDbConnection(ConnectionString);
    
            // open the connection
            conn.Open( );
    
            // Create an OleDbDataAdapter object
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = new OleDbCommand(SQL, conn);
    
            // Create Data Set object
            DataSet ds = new DataSet("orders");
            // Call DataAdapter's Fill method to fill data from the
            // DataAdapter to the DataSet
            adapter.Fill(ds);
    
            // Bind dataset to a DataGrid control
            dataGrid1.DataSource = ds.DefaultViewManager;
            }

     

    And to interact with SqlServer database we will use SqlDataAdapter for it.

     

    using SqlDataAdapter;
    
            private void SqlDataAdapter_Click(object sender, System.EventArgs e)
            {
            string ConnectionString = "Integrated Security = SSPI;" +
            "Initial catalog = Northwind;" + " Data Source =MAIN-SERVER; ";
            string SQL = "SELECT CustomerID, CompanyName FROM Customers";
            SqlConnection conn = new SqlConnection(ConnectionString);
    
            // open the connection
            conn.Open( );
    
            //Create a SqlDataAdapter object
            SqlDataAdapter adapter = new SqlDataAdapter(SQL, conn);
    
            // Call DataAdapter's Fill method to fill data from the
            // Data Adapter to the DataSet
            DataSet ds = new DataSet("Customers");
            adapter.Fill(ds);
            // Bind data set to a DataGrid control
            dataGrid1.DataSource = ds.DefaultViewManager;
            }

     

    For this we will use disconnected approach where there is no need of opening and closing the connection.

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: