Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Close connection or leave open
Message
De
02/09/2004 00:39:41
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00938620
Message ID:
00938649
Vues:
28
Einar,

Unfortunately, I don't use the .Update() method of the DataAdapter (we're using Stored Procs for all our DataAccess and so we've rolled our own updates) ... however, I'm betting that the problem is because you are explicitly opening the connection rather than letting the .Fill() handle it. Try this:
string dbDataPath = "c:\\it\\TestData\\TestDB.add";
string dbConStr = "data source=" + dbDataPath ;
DbConnect = new AdsConnection(dbConStr);
// -- Comment out the Open
// DbConnect.Open();

oDS = new DataSet();
oAdapter = new AdsDataAdapter();
oAdapter.SelectCommand = new AdsCommand(selectString,DbConnect);
oCB = new AdsCommandBuilder(oAdapter);
oAdapter.Fill(oDS, "TimeStamp");

// -- Comment out the Close
// DbConnect.Close();

// bind oDS to some controls and changes some values

int iRowsAffected = oAdapter.Update(oDS,"TimeStamp"); 
~~Bonnie



>Bonnie,
>
>Thanks for your reply. Below is the relevant code that is used. I connect to Advantage Data Server database and Iuse their .NEt data provider (that is why all the references to ADS in the code below)
>
>
>string dbDataPath = "c:\\it\\TestData\\TestDB.add";
>string dbConStr = "data source=" + dbDataPath ;
>DbConnect = new AdsConnection(dbConStr);
>DbConnect.Open();
>
>oDS = new DataSet();
>oAdapter = new AdsDataAdapter();
>oAdapter.SelectCommand = new AdsCommand(selectString,DbConnect);
>oCB = new AdsCommandBuilder(oAdapter);
>oAdapter.Fill(oDS, "TimeStamp");
>
>DbConnect.Close(); // if I comment this line the adapter.update works
>
>// bind oDS to some controls and changes some values
>
>int iRowsAffected = oAdapter.Update(oDS,"TimeStamp"); // this line throws an exception 
                                                      // stating that the connection must be open >
>
>
>As stated as inline comments in the code above, if I close the connection before I make the Adapter.Update it throws an exception. If I comment the DbConnect.Close() line above the code runs just fine.
>
>Please let me know if you need more information to help me. Thanks for taking the time to help me.
>
>Sincerely,
>Einar
>
>>Einar,
>>
>>You don't want to keep the connection open and you don't have to ... in fact, the DataAdapter.Fill() method will open the connection, Fill the DataSet and automatically close the connection (unless the connection is already open prior to executing the .Fill() method, in which case it leaves it open).
>>
>>Are you running into a specific problem?
>>
>>~~Bonnie
>>
>>
>>>I was under the impression that .NET wants you to work in a disconnected state i.e. connect get the data then do whatever you need to do with the data and then connect again and update the tables. But it seems that I need to keep the connection open if I want to use the Adapter.Update(), i.e. connect and get the data and then bind the dataset to my control(s) then NOT close the connection make my changes and the issue an Adapter.Update. Is this correct? I would like to close the connection after I have retreived my data and then connect again before I need to issue Adapter.Update().
>>>
>>>Any information or clarification regarding this would be great.
>>>
>>>Thanks,
>>>Einar
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform