Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sending Changes From A Dataset To The DB
Message
 
To
31/03/2008 11:01:40
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01306720
Message ID:
01307019
Views:
13
They plan to have users access the system through a VPN, so each user can have direct access
to the DB. I would then create a data access class which will hit the DB.

I'm open to any better ideas.




>No problem, Kevin ... I like answering newbie questions. <g>
>
>Gotta ask though ... why was it decided not to use web services? And how do you plan on hitting the server side stuff?
>
>~~Bonnie
>
>
>
>
>
>>Bonnie & Viv:
>>
>>Thanks for all the help, on this and prior postings.
>>
>>I met with my new client again over the weekend and the scope of the project has changed. There will not be
>>a need for a WS, so my original plan of a Client-Side class to a WS to a Server-Side class can be pared down
>>to simply a Data Access class directly hitting a SQL 2005 DB.
>>
>>Bonnie, what you have posted here seems like a good place to start, so let me parse through all this and I'm sure
>>I'll be back with more questions.
>>
>>Again, thanks for putting up with my newby c# quesstions!
>>
>>Kev
>>
>>
>>
>>
>>
>>>Kevin,
>>>
>>>I don't know if you've seen the stuff I've posted before for DataAccess questions:
>>>
>>>First, you have a base DataAccess class that all of your other DataAccess classes should inherit from, and get the connection in it's constructor:
>>>
>>>
>>>public class MyDataAccess
>>>{
>>>	protected SqlConnection oConnection;
>>>
>>>	public MyDataAccess
>>>	{
>>>		// Note that I wouldn't actually hard-code the connection string like this. It should be in
>>>		// your config settings. All the examples I currently have, use the old 1.1 way of doing this,
>>>		// which is this:
>>>		// string MyConnString = ConfigurationSettings.AppSettings["ConnectionString"];
>>>
>>>		this.oConnection = new SqlConnection("server=(local);database=MyDataBase;uid=sa;pwd=MyPassword");
>>>	}
>>>}
>>>
>>>
>>>Depending on how complicated your application is, you may want to have a separate DataAccess class for different functionality, all sub-classing from the MyDataAccess base class.
>>>
>>>
>>>Retrieving the data is easy. Note that this DataAccess class returns a DataSet:
>>>
>>>
>>>public DataSet GetMyData()
>>>{
>>>	// Note that a Fill does not require a Connection Open/Close. The Fill leaves
>>>	// the connection in the state it was found (leaves it open if it was open,
>>>	// and if it was closed, it opens it, Fills, then closes again).
>>>
>>>	SqlDataAdapter da = new SqlDataAdapter("select * from bob", this.oConnection);
>>>	DataSet ds = new DataSet();
>>>	da.Fill(ds, "MyTable");
>>>
>>>	return ds;
>>>}
>>>
>>>
>>>
>>>There are a few more 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)
>>>{
>>>	// The same applies for the Update. It's not necessary to Open/Close the connection.
>>>	SqlDataAdapter da = new SqlDataAdapter("select * from bob", this.oConnection);
>>>	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)
>>>{
>>>
>>>	SqlCommand sc = new SqlCommand();
>>>	sc.Connection = this.oConnection;
>>>	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)
>>>{
>>>	SqlCommand sc = new SqlCommand();
>>>	sc.Connection = this.oConnection;
>>>	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();
>>>}
>>>
>>>
>>>HTH,
>>>~~Bonnie
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Reply
Map
View

Click here to load this message in the networking platform