Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Updating DataSet with Multiple Tables
Message
De
16/11/2004 16:50:51
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows XP
Database:
MS SQL Server
Divers
Thread ID:
00961815
Message ID:
00961950
Vues:
5
This message has been marked as the solution to the initial question of the thread.
I have modified the book's sample code for updating multiple tables, my code is in Vb.net, i pass this parameters to the function.
ds: The dataset
ClavePrim: The primary key field
TablaPrimaria: The master table
Autoinc: Indicates if the primary key is autoincemental
Esnuevo: Indicates if i am inserting a new record in the master table.

I hope it will be useful to you, it may have any errors, i am new like you in .Net.


Public Overrides Function GuardaTablas(ByVal ds As DataSet, ByVal ClavePrim As String, _
ByVal TablaPrimaria As String, ByVal EsAutoinc As Boolean, ByVal EsNuevo As Boolean) As Integer
Dim Connection As IDbConnection = OpenConnection()
Dim tabla As New DataTable
Dim MyTrans As SqlTransaction
Dim RowsUpdated As Integer
MyTrans = Connection.BeginTransaction()
Try
For Each tabla In ds.Tables
Dim myDataAdapter As New SqlDataAdapter
Dim lctabla As String = tabla.TableName
Dim strsql As String
strsql = "select * from " + lctabla + " where 1=2"
myDataAdapter.SelectCommand = New SqlCommand(strsql, Connection, MyTrans)
Dim CommandBuilder As New SqlCommandBuilder(myDataAdapter)
myDataAdapter.DeleteCommand = CommandBuilder.GetDeleteCommand()
myDataAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand()
myDataAdapter.InsertCommand = CommandBuilder.GetInsertCommand()
myDataAdapter.DeleteCommand.Transaction = MyTrans
myDataAdapter.UpdateCommand.Transaction = MyTrans
myDataAdapter.InsertCommand.Transaction = MyTrans
' Update the data in the DataSet
RowsUpdated = myDataAdapter.Update(ds, tabla.ToString)
If tabla.TableName = TablaPrimaria Then
If EsNuevo = True Then
If EsAutoinc = True Then
Dim idclave As Integer
Dim cmd As SqlCommand = New SqlCommand("select @@identity", _
Connection, MyTrans)
idclave = cmd.ExecuteScalar()
Dim mytable As New DataTable
Dim myrow As DataRow
For Each mytable In ds.Tables
If mytable.TableName <> TablaPrimaria Then
For Each myrow In mytable.Rows
myrow(ClavePrim) = idclave
Next
End If
Next
End If
End If
End If
Next
MyTrans.Commit()
Catch excep As SqlException
MyTrans.Rollback()
Throw New Exception("Error:" & excep.Message & excep.LineNumber.ToString)
Finally
Connection.Close()
End Try
Return RowsUpdated

End Function
End Class 'DataAccessBase
>Hi Bonnie,
>
>Here is the code for SaveDataSet():
>
>
>public override int SaveDataSet(DataSet ds)
>{
>	SqlDataAdapter DataAdapter;
>	
>	if (ds is CDataSet)
>	{
>		/// If this is a custom DataSet, get a reference to the
>		/// DataSet's data adapter and store it in the DataAdapter variable
>		CDataSet dsSql = (CDataSet)ds;
>		DataAdapter = (SqlDataAdapter)dsSql.DataAdapter;
>	}
>	else
>	{
>		/// If this is NOT a Custom DataSet, create a new Data Adapter
>		DataAdapter = new SqlDataAdapter();
>	}
>	SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataAdapter);
>	CommandBuilder.QuotePrefix = "[";
>	CommandBuilder.QuoteSuffix = "]";
>
>	DataAdapter.DeleteCommand = CommandBuilder.GetDeleteCommand();
>	DataAdapter.UpdateCommand = CommandBuilder.GetUpdateCommand();
>	DataAdapter.InsertCommand = CommandBuilder.GetInsertCommand();
>
>
>	// Update the data in the DataSet
>	int RowsUpdated = DataAdapter.Update(ds, ds.Tables[0].ToString());
>			
>	return RowsUpdated;
>}
>
>
>>Mike,
>>
>>I have Kevin's book, but I haven't downloaded his sample code. How is the current SaveDataSet() method currently coded? Does it not include a way to work with a multi-table DataSet? I've not written any "generic" data access class to do this sort of thing (all my data access classes are specific to what's being updated), so looking at the code for the sample SaveDataSet() method that you're already using may give me some ideas of how to "generic-ize" it. <g>
>>
>>~~Bonnie
>>
>>
>>
>>>I am trying to enhance the WebForms example application from Chapter 10 of Kevin McNeish's book '.Net for Visual FoxPro Developers'.
>>>
>>>On the OrderEdit form, I added Edit buttons to the grid grdOrderItems. This worked ok. Then, I tried to join the Products table with the OrderItems table, in the DataSet, to show the ProductName rather than the ProductID.
>>>
>>>The ProductName shows ok, but when I try to do any edits, I get an error that the CommandBuilder can't build commands for DataSets with multiple tables; you have to define your own commands manually. However, I was trying to use Kevin's business objects, from Chapter 8, where there is a generic SaveDataSet() method in the DataAccessSql data object in the Data.cs file, where commands are built on-the-fly for the DataSet passed in.
>>>
>>>Is there any way to keep using business objects with DataSets with multiple tables? How would I need to code my DataAdapter DeleteCommand, etc. to be generic (to work with any DataSet passed in)?
>>>
>>>Thanks
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform