Search code examples
c#sqldatasetdataadapter

how a dataset is disconnected?


i am really confused with concept of dataset in ado.net that it is disconnected.

if let say if we issue an update sql statement using data adapter and dataset in that

string stUpdate=  "update customer set name = 'Faizan' where ID = 5"
SqlDataAdapter da= new SQLDataAdapter(stUpdate, SQLCONNECTIONOBJECT)
DataSet ds = new Dataset
da.Fill(ds, "tablename")

the underlying Customer Table in my database is actually get updated. So in what way DATASET is disconnected. An Update Action perform and customer Table updated


Solution

  • The term disconnected depicts more of an architectural pattern. If you get down to implementation details everything looks/is connected.

    The term disconnected is used to convey its design property that it doesn't need a live database connection. You Fill your Dataset, make changes to it, Save the dataset to disk, stop your application, a day later start your application, Read the dataset from disk and write the changes to you made yesterday to a datastore using an http://msdn.microsoft.com/en-us/library/system.data.common.dbdataadapter(v=vs.110).aspx.

    From the excellent John Pappa this excerpt from his msdn article series Data Points

    Your situation will dictate when and where you'll use a DataSet versus a DataReader. For example, the DataSet's disconnected nature allows it to be transformed into XML and sent over the wire via HTTP if appropriate. This makes it ideal as the return vehicle from business-tier objects and Web services. A DataReader cannot be serialized and thus cannot be passed between physical-tier boundaries where only string (XML) data can go.

    Although it looks 'connected' to your database I can expand your example a little bit to make clear that 'connected' is relative. It is better to talk about dependency amd the DataSet only depends on a DataAdapter.

    StorageEnum store = Storage.Oracle;
    string stUpdate=  "update customer set name = 'Faizan' where ID = 5";
    DbDataAdapter da = null;
    swith (store)
    {
        case StorageEnum.SqlServer:
           da= new SQLDataAdapter(stUpdate, SQLCONNECTIONOBJECT)
        break;
        case StorageEnum.Oracle:
           da= new OracleDataAdapter(stUpdate, ORACLECONNECTIONOBJECT)
        break;
        default:
           da= new OleDBDataAdapter(stUpdate, OLEDBCONNECTIONOBJECT)
        break;
    };
    DataSet ds = new Dataset();
    da.Fill(ds, "tablename")
    

    As you can see the only change needed to support multiple databases for Update/Insert/Delete and Select only required changing the used DataAdapter.