My program has an online and offline mode.
SqlCommand
classes..SDF
file using the C# SqlCeCommand
classes.Now I want to do the following which works when in online mode
GetSqlCommand("insert into my_table (col1) values (@c1); SELECT SCOPE_IDENTITY()", conn))
So I insert a record a get back the ID of that new record with SELECT SCOPE_IDENTITY()
.
But when in offline mode using SQL Server CE, it throws an error. Is there a way to get back ID from the insert with SQL Server CE without running a separate query?
SQL Server CE doesn't support multiple queries in single command, try as below
SqlCeCommand cmd = new SqlCeCommand("insert into my_table (col1) values (@c1)", conn);
//set paremeter values
//execute insert
cmd.ExecuteNonQuery();
//now change the sql statment to take identity
cmd.CommandText = "SELECT @@IDENTITY";
int id = Convert.ToInt32(cmd.ExecuteScalar());