|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Transactions with generated DataAdaptersI have generated a DataSet with Visual Studio 2005 by selecting Data -> Add New Data Source ... -> Database etc. from the menu and selected 2 objects (tables) in the Data Source Configuration Wizard. As a result I get an .XSD DataSet file that automatically generated a DataAdapter. I have added an insert statement to both tables. Question 1: One table has an autonumber id field that automatically generates a primary key when inserting a new row into the database. How do I get that automatically primary key which I need for the insert statement in the second table? Question 2: Both insert statements should be performed in one transaction. How do I set up a business method for inserting values into both tables that executes both insert statements in one transaction, i.e. public void insertValues() { MyFirstDataSet.MyFirstDataAdapter a1 = new MyFirstDataSet.MyFirstDataAdapter (); MySecondDataSet.MySecondDataAdapter a2 = new MySecondDataSet.MySecondDataAdapter (); // start transaction a1.InsertValues("mike", "miller"); // a value is inserted into a table with autogenerated primary key int key = ... a2.InsertValues(key, "test"); // end transaction or rollback } Any help would be much appreciated. Steffen Q1:Probobly you need in Sql server specify identity seed yes on primary key
or in code make procedure that automatic insert value in primary key column. Q2:You can use try and catch or begin transaction,roolback transaction? <Steffen.Schlach***@gmail.com> wrote in message Show quote news:1146670056.261900.43690@i39g2000cwa.googlegroups.com... > Hello, > > I have generated a DataSet with Visual Studio 2005 by selecting Data -> > Add New Data Source ... -> Database etc. from the menu and selected 2 > objects (tables) in the Data Source Configuration Wizard. As a result I > get an .XSD DataSet file that automatically generated a DataAdapter. > > I have added an insert statement to both tables. > > Question 1: One table has an autonumber id field that automatically > generates a primary key when inserting a new row into the database. How > do I get that automatically primary key which I need for the insert > statement in the second table? > > Question 2: Both insert statements should be performed in one > transaction. How do I set up a business method for inserting values > into both tables that executes both insert statements in one > transaction, i.e. > > public void insertValues() { > > MyFirstDataSet.MyFirstDataAdapter a1 = new > MyFirstDataSet.MyFirstDataAdapter (); > MySecondDataSet.MySecondDataAdapter a2 = new > MySecondDataSet.MySecondDataAdapter (); > > // start transaction > a1.InsertValues("mike", "miller"); > // a value is inserted into a table with autogenerated primary key > int key = ... > a2.InsertValues(key, "test"); > // end transaction or rollback > } > > Any help would be much appreciated. > > Steffen > |
|||||||||||||||||||||||