Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem updating Inserted row in my DataSet
Message
From
24/10/2007 22:38:09
 
 
To
24/10/2007 12:04:30
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01262924
Message ID:
01263419
Views:
22
This message has been marked as the solution to the initial question of the thread.
Sergio,

You have a few options when updating the database.

First, you can use the Update method of the DataAdapter. In order for this to work, your DataSet must have a PrimaryKey defined.

You can do it using the CommandBuilder, which will generate update commands for you (note: if you use a Stored Proc, the CommandBuilder only generates the proper insert/update/delete commands for the first table retreived from the Stored Proc):
public void UpdateMyData(DataSet ds)
{
	// I wouldn't actually hard-code the connection string here, it should be in your config settings
	string TestConnection = "server=(local);database=MyDataBase;uid=sa;pwd=MyPassword";

	SqlDataAdapter da = new SqlDataAdapter("select * from bob", TestConnection);
	SqlCommandBuilder sb = new SqlCommandBuilder(da);

	da.Update(ds);
}
Or you can create the various update commands yourself instead of using the CommandBuilder:
public void UpdateMyData(DataSet ds)
{

	// I wouldn't actually hard-code the connection string here, it should be in your config settings
	string TestConnection = "server=(local);database=MyDataBase;uid=sa;pwd=MyPassword";

	SqlCommand sc = new SqlCommand();
	sc.Connection = new SqlConnection(TestConnection);
	da = new SqlDataAdapter(sc);

	da.InsertCommand = new SqlCommand("Insert into bob (xyz, abc) VALUES ( @xyz, @abc )", sc.Connection);
	da.InsertCommand.Parameters.Add("@xyz", SqlDbType.Int, 8, "xyz");
	da.InsertCommand.Parameters.Add("@abc", SqlDbType.VarChar, 50, "abc");

	// do the same for da.DeleteCommand & da.UpdateCommand

	da.Update(ds);
}
Or, you can take total control, not use the da.Update() and do it all yourself (this is basically the same code that gets done behind the scenes by the da.Update() method:
public void UpdateMyData(DataSet ds)
{
	// I wouldn't actually hard-code the connection string here, it should be in your config settings
	string TestConnection = "server=(local);database=MyDataBase;uid=sa;pwd=MyPassword";

	SqlCommand sc = new SqlCommand();
	sc.Connection = new SqlConnection(TestConnection);
	sc.Connection.Open();
	foreach (DataRow Row in ds.Tables[0].Rows)
	{
		switch (Row.RowState)
		{
			case DataRowState.Added :
				sc.CommandText = "Insert into bob (xyz, abc) VALUES ( @xyz, @abc )";
				sc.Parameters.Clear();
				sc.Parameters.Add("@xyz", Row["xyz"]);
				sc.Parameters.Add("@abc", Row["abc"]);
				sc.ExecuteNonQuery();
				break;
				
			// Do the same for DataRowState Deleted and Modified
			case DataRowState.Deleted :
				break;
			case DataRowState.Modified :
				break;
		}
	}
	sc.Connection.Close();
}
~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform