Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DataAdapter.Fill() method
Message
De
15/02/2006 12:47:54
 
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Versions des environnements
Environment:
C# 2.0
Database:
MS SQL Server
Divers
Thread ID:
01096461
Message ID:
01096536
Vues:
16
By the way,

Since you're always asking for better ways, and I'm trying to test my knowledge, the preferred way to run the logic you're showing is within a using block.
Ex:
using (SqlConnection cn = new SqlConnection(connectionString))
{
    using (SqlCommand cm = new SqlCommand(commandString, cn))
    {
        cn.Open();
        cm.ExecuteNonQuery();
    }
}
There are 2 reasons for this. 1) Using implicitly generates a try/catch around the code. 2) Sqlcommand and SqlConnection use some unmanaged resources, so you must ensure to dispose of the objects when you're done, otherwise you could get a memory leak. "Using" calls dispose for you.

>Again something works differently than how I expected it to work.
>I have a table with 3 fields (field1, field2 and field3). I created a typed dataset from that table, but I removed field3 so the dataset only has 2 fields (field1 and field2).
>I run the following code:
>
>DataSet1 ds = new DataSet1();
>MessageBox.Show("Coulumns.Count: " + ds.Table2.Columns.Count.ToString(), "before");
>
>SqlDataAdapter adapter = new SqlDataAdapter();
>adapter.SelectCommand = new SqlCommand("SELECT field1, field2, field3 FROM Table2", new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI;"));
>adapter.Fill(ds, ds.Table2.TableName);
>
>MessageBox.Show("Coulumns.Count: " + ds.Table2.Columns.Count.ToString(), "after");
>
>
>The first msgbox (with title before) displays a column count of 2 which is what I expect, but the 2nd msgbox (with title after) displays a column count of 3 which is not really what I expect.
>
>I understand why field3 is in my dataset after the fill (because it is included in the SELECT statement), but I am hoping that there is a way to prevent the structure of the dataset to be changed as a result of the Fill().
>
>Einar

(On an infant's shirt): Already smarter than Bush
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform