Ado.net : How to insert a row without filling the dataset ?

C

Cybertof

Hello,

Is it necessary to initially fill a dataset to be able to add a new row
to the dataset and then update the undercover database ?
(In the case of a very large database, this would mean that all the
database content should be loaded before beeing able to add a new row,
and i don't want to load anything...)

Is it possible in the following code to remove the
"// CAN THIS BE REMOVED ?"
line ?


Sample Code :
**************

public static void Main() {

SqlConnection myConn = new SqlConnection("...");
SqlCommand myCommand = new SqlCommand( "Select * from Company", myConn);
OleDbDataAdapter myDA = new OleDbDataAdapter(myCommand);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "Companies"); // CAN THIS BE REMOVED ?

DataRow myRow = myDS.Tables["Companies"].NewRow();
myRow["CompanyName"] = "Widget Corp.";
myRow["StockSymb"] = "WID";
myDS.Tables["Companies"].Rows.Add(myRow);
OleDbCommandBuilder mBuild = new OleDbCommandBuilder(myDA);
myDA.Update(myDS, "Companies");

}
 
M

Miha Markic

Is it necessary to initially fill a dataset to be able to add a new row
to the dataset and then update the undercover database ?
(In the case of a very large database, this would mean that all the
database content should be loaded before beeing able to add a new row,
and i don't want to load anything...)

Yes, it is possible.
Is it possible in the following code to remove the
"// CAN THIS BE REMOVED ?"
line ?

No because your dataset schema is not defined.
You might manullay add columns to the dataset's table.
 
C

Cybertof

How to get a dataset schema from a dataadapter without filling the
dataset with records using the .Fill method from the dataAdapter object
?

I would like to do it without doing the FILL instruction, so the
response time to add a new row does not depend on the size of the
database...


Thanks,
Cybertof.
 
E

Empire City

Is it necessary to initially fill a dataset to be able to add a new row
SqlConnection myConn = new SqlConnection("...");
SqlCommand myCommand = new SqlCommand( "Select * from Company", myConn);
OleDbDataAdapter myDA = new OleDbDataAdapter(myCommand);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "Companies"); // CAN THIS BE REMOVED ?

myDA.FillSchema( myDS, SchemaType.Source, "Companies");
myDT= myDS.Tables[ "Companies" ];
DataRow myRow = myDS.Tables["Companies"].NewRow();
myRow["CompanyName"] = "Widget Corp.";
myRow["StockSymb"] = "WID";
myDS.Tables["Companies"].Rows.Add(myRow);
OleDbCommandBuilder mBuild = new OleDbCommandBuilder(myDA);
myDA.Update(myDS, "Companies");

As the other reply to your post pointed out you could also add the columns
as in this excerpt from the Duwamish sample:

DataTable table = new DataTable( CUSTOMERS_TABLE );
DataColumnCollection columns = table.Columns;
DataColumn Column = columns.Add( PKID_FIELD, typeof( System.Int32 ));
Column.AllowDBNull = false;
Column.AutoIncrement = true;
columns.Add( EMAIL_FIELD, typeof( System.String ));
columns.Add( PASSWORD_FIELD, typeof( System.String ));
columns.Add( NAME_FIELD, typeof( System.String ));
columns.Add( ADDRESS_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( COUNTRY_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( PHONE_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( FAX_FIELD, typeof( System.String ));
this.Tables.Add( table );
 
C

Cybertof

Thanks Miha Markic & Empire City.

In think i prefer to extract the schema from the existing database, so
in case of small inner database schema change (type change for example)
, the code would "update itself dynamically" extracting the schema on
the fly from the database.


Regards,
Cybertof.



Is it necessary to initially fill a dataset to be able to add a new row
SqlConnection myConn = new SqlConnection("...");
SqlCommand myCommand = new SqlCommand( "Select * from Company", myConn);
OleDbDataAdapter myDA = new OleDbDataAdapter(myCommand);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "Companies"); // CAN THIS BE REMOVED ?

myDA.FillSchema( myDS, SchemaType.Source, "Companies");
myDT= myDS.Tables[ "Companies" ];
DataRow myRow = myDS.Tables["Companies"].NewRow();
myRow["CompanyName"] = "Widget Corp.";
myRow["StockSymb"] = "WID";
myDS.Tables["Companies"].Rows.Add(myRow);
OleDbCommandBuilder mBuild = new OleDbCommandBuilder(myDA);
myDA.Update(myDS, "Companies");

As the other reply to your post pointed out you could also add the columns
as in this excerpt from the Duwamish sample:

DataTable table = new DataTable( CUSTOMERS_TABLE );
DataColumnCollection columns = table.Columns;
DataColumn Column = columns.Add( PKID_FIELD, typeof( System.Int32 ));
Column.AllowDBNull = false;
Column.AutoIncrement = true;
columns.Add( EMAIL_FIELD, typeof( System.String ));
columns.Add( PASSWORD_FIELD, typeof( System.String ));
columns.Add( NAME_FIELD, typeof( System.String ));
columns.Add( ADDRESS_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( COUNTRY_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( PHONE_FIELD, typeof( System.String )).AllowDBNull= false;
columns.Add( FAX_FIELD, typeof( System.String ));
this.Tables.Add( table );
 
M

Miha Markic

Hi,

In addition to this two ways, there is another one. You might use
strongtyped dataset (created at design time).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top