P
PeterB
Hi!
I'm using SQLCE on a pocket pc and I have a database table with an identity field. I use a sqlcedataadapter to fill, insert, delete etc. a dataset datatable. I've read that I need to take some extra steps to retrieve the incremented identity value when performing an insert.
The main problem is that the identity field of the dataset table is also an identity field but "on it's own" as it seems. I.e. when I create a new record it puts it's own increment in the value of the field without notice of the database's increment value. So the question is how should this be resolved in SSCE?
1. Identity field in dataset table is readonly
Since I use the fillschema method to fill the dataset table with it's schema it automatically sets the identity field to an identity field, and hence readonly. Should I explicitly remove the identity property and readonly property after filling the schema? By doing that I could update the identity field with the correct value from the database. What are the consequences of this?
2. Updating the value of the dataset table identity field
2.1 My first try was to add code for this in the RowUpdated event that is fired when I execute the adapter.Update() method. The code I use for this is:
private static void sqlAdapt_RowUpdated(object sender, SqlCeRowUpdatedEventArgs e)
{
// Include a variable and a command to retrieve the identity value from the database.
object obj = null;
int newID = 0;
SqlCeCommand sqlCmd = new SqlCeCommand("SELECT @@IDENTITY", sqlConn);
if (e.StatementType == StatementType.Insert)
{
// Retrieve the identity value and store it in the LOPNR column.
obj = sqlCmd.ExecuteScalar();
//newID = (int)sqlCmd.ExecuteScalar();
System.Data.SqlTypes.SqlDecimal dec;
dec = (System.Data.SqlTypes.SqlDecimal)obj;
newID = Convert.ToInt32(dec.ToString());
e.Row["LOPNR"] = newID;
}
}
This great if I unset the identity and readonly property of the LOPNR column in the dataset table.If I don't unset those properties I get an exception telling me LOPNR is readonly. My SqlCeAdapter Insert commands does not have a parameter for the identity field here.
2.2 My second choice was from a post by David Sceppa on 2003-02-26 10:38:24 PST. He suggests adding a parameter for the identity field and modify the InserCommand as shown below (userid is equal to lopnr in my code):
INSERT INTO [users] ([firstname]) VALUES (@firstname);
SET @userid = @@IDENTITY
insertPrm = insertCmd.Parameters.Add("@userID", SqlDbType.Int, 0, "userid")
insertPrm.Direction = ParameterDirection.Output
insertCmd.UpdatedRowSource = UpdateRowSource.OutputParameters
dataadapter.InsertCommand = insertCmd
Questions:
1. Do I need to unset the dataset table readonly/identity property in 2.2 as well?
2. Will 2.2 work in SSCE?
3. Which method is recommended?
4. Can the update row event cause the wrong identity value to be added into the dataset table?
5. Is there a solution where you don't need to unset the identity/readonly properties in the dataset?
6. Is it even correct that the column is set to identity using FillSchema method? That increment will restart each time a application is restarted while the SSCE identity increment is permanent (until db is deleted). So eventually they will be out of sync won't they?
br,
Peter
I'm using SQLCE on a pocket pc and I have a database table with an identity field. I use a sqlcedataadapter to fill, insert, delete etc. a dataset datatable. I've read that I need to take some extra steps to retrieve the incremented identity value when performing an insert.
The main problem is that the identity field of the dataset table is also an identity field but "on it's own" as it seems. I.e. when I create a new record it puts it's own increment in the value of the field without notice of the database's increment value. So the question is how should this be resolved in SSCE?
1. Identity field in dataset table is readonly
Since I use the fillschema method to fill the dataset table with it's schema it automatically sets the identity field to an identity field, and hence readonly. Should I explicitly remove the identity property and readonly property after filling the schema? By doing that I could update the identity field with the correct value from the database. What are the consequences of this?
2. Updating the value of the dataset table identity field
2.1 My first try was to add code for this in the RowUpdated event that is fired when I execute the adapter.Update() method. The code I use for this is:
private static void sqlAdapt_RowUpdated(object sender, SqlCeRowUpdatedEventArgs e)
{
// Include a variable and a command to retrieve the identity value from the database.
object obj = null;
int newID = 0;
SqlCeCommand sqlCmd = new SqlCeCommand("SELECT @@IDENTITY", sqlConn);
if (e.StatementType == StatementType.Insert)
{
// Retrieve the identity value and store it in the LOPNR column.
obj = sqlCmd.ExecuteScalar();
//newID = (int)sqlCmd.ExecuteScalar();
System.Data.SqlTypes.SqlDecimal dec;
dec = (System.Data.SqlTypes.SqlDecimal)obj;
newID = Convert.ToInt32(dec.ToString());
e.Row["LOPNR"] = newID;
}
}
This great if I unset the identity and readonly property of the LOPNR column in the dataset table.If I don't unset those properties I get an exception telling me LOPNR is readonly. My SqlCeAdapter Insert commands does not have a parameter for the identity field here.
2.2 My second choice was from a post by David Sceppa on 2003-02-26 10:38:24 PST. He suggests adding a parameter for the identity field and modify the InserCommand as shown below (userid is equal to lopnr in my code):
INSERT INTO [users] ([firstname]) VALUES (@firstname);
SET @userid = @@IDENTITY
insertPrm = insertCmd.Parameters.Add("@userID", SqlDbType.Int, 0, "userid")
insertPrm.Direction = ParameterDirection.Output
insertCmd.UpdatedRowSource = UpdateRowSource.OutputParameters
dataadapter.InsertCommand = insertCmd
Questions:
1. Do I need to unset the dataset table readonly/identity property in 2.2 as well?
2. Will 2.2 work in SSCE?
3. Which method is recommended?
4. Can the update row event cause the wrong identity value to be added into the dataset table?
5. Is there a solution where you don't need to unset the identity/readonly properties in the dataset?
6. Is it even correct that the column is set to identity using FillSchema method? That increment will restart each time a application is restarted while the SSCE identity increment is permanent (until db is deleted). So eventually they will be out of sync won't they?
br,
Peter