As Sam has pointed out, there's not a whole lot out there on this guy (see nothing). I'm going to get pretty in depth with it tomorrow if the audience is interested. If not, oh well. Anyway, this is a totally different beast then what you're used to with Data Access. Why? Well, because you kinda need to leave the connection open to get much use out of it. Normally, this makes me cringe because, well, lots of folks don't like following the instructions on stuff and then bitch up a storm when it blows up. Most of the time they'll find a scapegoat like Microsoft because admitting "I'm professionally irresponsible and my arrogance caused this disaster" doesn't usually bode well for employment status. But I digress. Ok, back to the issue at hand. You need to leave the connection open if you want to do anything with it but you get a lot of cool stuff. First, it's fast as hell. You'll have to check out my webcast if you want numbers but trust me on it, it's faster than what you're used to. Second, you can walk it provided you set the appropriate property. Next you can bind it to stuff and update from it directly. This means that you can INSERT, DELETE and Modify data with it directly as opposed to SqlCeDataReader scenarios where you are in read only world and need another query to send anything back to the DB. A few nuances:
There are 10 types of rows, those that can read binary and those that can't. Seriously though, there are two main ones you'll be concerned with: 1) SqlCeRecord 2) SqlCeUpdatableRecord. Presently there's only one supported (SqlCeUpdatableRecord) but I have overheard that SqlCeRecord will be supported at game time. Since you probably don't give a rat's a33 about stuff that 'may' be out there, there's really only one type of record to worry about here... the SqlCeUpdatableRecord. To use one though, you need to use the ResultSetOptions.Updatable flag when you call ExecuteResultSet (you'll probably want to set the ResultSetOptions.Scrollable flag as well but for a different reason). Assuming you have a valid record in the hopper, here's the basic code to get it working (Remember, you've got to have an open connection):
| private void btnNew_Click(object sender, EventArgs e) { SqlCeUpdatableRecord newRec = rs.CreateRecord(); newRec.SetValue(0,3335); newRec.SetValue(1, tbFirstName.Text); newRec.SetValue(2, tbLastName.Text); try { rs.Insert(newRec); rs.Update(); } catch (InvalidOperationException exc) { Debug.Assert(false, exc.ToString()); } catch (SqlCeException ex) { StringBuilder errMessage = new StringBuilder(); foreach (SqlCeError er in ex.Errors) { errMessage.Append(er.Message + "\r\n"); } Debug.Assert(false, errMessage.ToString()); } } |
| private void SetValues() { nmID.Value = rs.GetInt32(0); tbFirstName.Text = rs.GetString(1); tbLastName.Text = rs.GetString(2); } |
| private void btnNext_Click(object sender, EventArgs e) { if (rs.Read()) { SetValues(); btnPrev.Enabled = true; } else { btnNext.Enabled = false; } } private void btnPrev_Click(object sender, EventArgs e) { if (rs.ReadPrevious()) { btnNext.Enabled = true; SetValues(); } else { btnPrev.Enabled = false; } } private void btnLast_Click(object sender, EventArgs e) { if (rs.ReadLast()) { SetValues(); } } private void btnFirst_Click(object sender, EventArgs e) { if (rs.ReadFirst()) { SetValues(); } } |
| int SomeInt = 112;//Random number pulled out of the air rs.ReadRelative(SomeInt); rs.ReadAbsolute(SomeInt); |
| rs.Delete(); |
| rs = cmd.ExecuteResultSet(ResultSetOptionsScrollable|ResultSetOptions.Updatable); |