Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How do you update multiple tables in .NET app?
Message
De
06/09/2006 17:31:19
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01151272
Message ID:
01151519
Vues:
33
Here's how I do it. This is simplified ... some of it, like getting the changed/deleted rows, could be done in the Biz layer or the front end or the Web Service. This is basically the code you need in the DataAccess layer though:
public bool SaveMyDataSet()
{
	bool IsOK = true;
	long MyPK;

	this.oConnection.Open();
	SqlTransaction oTrans = this.oConnection.BeginTransaction();
	SqlCommand sc = new SqlCommand();
	sc.CommandType = CommandType.StoredProcedure;
	sc.Connection = this.oConnection;
	sc.Transaction = oTrans;

	DataSet dsChanged = this.MyDataSet.GetChanges();
	DataSet dsDeleted = this.MyDataSet.GetChanges(DataRowState.Deleted);


	if (IsOK == true)
		IsOK = this.SaveMyData(sc, dsChanged);

	if (IsOK == true)
		IsOK = this.DeleteMyData(sc, dsDeleted);

	// End the open transaction
	if (IsOK == true)
	{
		oTrans.Commit();
		MyPK = this.GetKey(dsChanged, "MyPK");
		DataSet dsKeys = this.GetMyDataSetKeys(ref Message, MyPK);
		// this spins through MyDataSet updating the keys from the dsKeys DataSet
		this.MatchKeys(this.MyDataSet, dsKeys);
	}
	else
	{
		oTrans.Rollback();
		Message = "Transaction rolled back!" + (char)13 + (char)13 +
			      "Changes in Personnel Information were not saved.";
	}
	this.oConnection.Close();
	return IsOK;
}

protected bool SaveMyData(SqlCommand Command, MyDataSet Data)
{
	long MyPK;
			
	if (Data.Personnel.Rows.Count < 1)
		return false;

	try
	{
		// -- First, the main table
		Command.CommandText = "bsp_MyMainTable_Put";

		// The SetBasicParameters method loops through every column in the table, setting the parameters.
		// It will also set the PK parameter's .Direction property to be ParameterDirection.InputOutput

		this.SetBasicParameters(Command, Data.Tables["MyMainTable"].Rows[0], "MyPK");
		Command.ExecuteNonQuery();

		MyPK = (long)Command.Parameters["@MyPK"].Value;
		Data.Tables["MyMainTable"].Rows[0]["MyPK"] = MyPK;
				
		// -- The SetKey method goes through every row in every table, setting the ForeignKey
		//    This assumes that all FK's in every table have the same name "MyPK"
		this.SetKey(Data, MyPK, "MyPK");

		// Now that all the FK's are in place, we can update all the child tables
		for (int i = 0; i < Data.Tables["MyChildTable"].Rows.Count; i++)
		{
			Command.CommandText = "bsp_MyChildTable_Put";
			this.SetBasicParameters(Command, Data.Tables["MyChildTable"].Rows[i], "MyChildPK");
			Command.ExecuteNonQuery();
		}

		for (int i = 0; i < Data.Tables["MySecondChildTable"].Rows.Count; i++)
		{
			Command.CommandText = "bsp_MySecondChildTable_Put";
			this.SetBasicParameters(Command, Data.Tables["MySecondChildTable"].Rows[i], "MySecondChildPK");
			Command.ExecuteNonQuery();
		}
	}
	catch (SqlException ex)
	{
		// Log the error 
		return false;
	}

	return true;
}

protected bool DeleteMyData(SqlCommand Command, MyDataSet Data)
{
	// The Delete stuff is similar
}
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform