ADO.NET - Adding Records to a database

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi,

I am trying to insert and update records into a database if have created in
access. I am unsure of the most efficient way of doing this, and have been
trying to work it our for ages.

Here is what i have so far, any help would be appreciated.

OleDbConnection DtlsConnection = new OleDbConnection() ;
OleDbCommand DtlsCommand = new OleDbCommand() ;
OleDbDataAdapter DtlsAdapter = new OleDbDataAdapter() ;
DataSet DtlsDs = new DataSet() ;

DtlsConnection.ConnectionString = StrConnectionPath ;
DtlsConnection.Open() ;

DtlsCommand.CommandText = "Select * From tbl" ;
DtlsCommand.Connection = SecurityDtlsConnection ;

DtlsAdapter.SelectCommand = DtlsCommand ;
DtlsAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ;
DtlsAdapter.Fill(DtlsDs, "tbl") ;

I need to add a record to the database table "tbl" which has two fields
'Code' and 'Name'. 'Code' is a unique number (primary key) with a length of
7 and 'Name' is text with a Length of 50.

Is this the code that i need to implement to add a new record?....

DataRow tblRow ;
tblRow = DtlsDs.Tables["tbl"].NewRow() ;
tblRow["Code"] = 1234567 ;
tblRow["Name"] = "Test Name" ;
DtlsDs.Tables["tbl"].Rows.Add(tblRow) ;

How do i update an existing record in the database?... is this the way...

DataRow tblRow ;
tblRow = SecurityDtlsDs.Tables["tbl"].Rows.Find(1234567) ;
tblRow["Name"] = "New Name" ;
SecurityDtlsDs.Tables["tbl"].AcceptChanges() ;

Any help would be appreciated.

Kind Regards
Darryn Ross
 
Darryn,
It generally looks right, but where does the dataset DtlsDs become
SecurityDtlsDs ?
 
Back
Top