Now that we have some tables, you naturally want to put some data into them. As you might have guessed from my last post, you perform data manipulation (insert, update, delete) just like you do when creating the table and use the SqlCeCommand object. Only this time there’s a twist.
First, because I wanted to load several rows I created a method to load a single row and pass in the parameters to my “CoolPeople” table. Here’s the small bit of code that handles it:
LoadARow(“Carl”, “Franklin”, @”http:\\www.dnrtv.com”);
LoadARow(“Richard”, “Campbell”, @”http:\\www.dotnetrocks.com”);
LoadARow(“Leo”, “Laporte”, @”http:\\www.twit.tv”);
LoadARow(“Steve”, “Gibson”, @”http:\\www.grc.com”);
LoadARow(“Arcane”, “Code”, @”http:\\arcanecode.wordpress.com”);
I then wrote a routine that would take the passed in data and insert it into the database. As with my create table example yesterday, I’m using the command object. This time though, I am adding parameters to the command.
If you look in the SQL, you see I have three parameters, noted by the @ sign. @first, @last, and @url. When SSCE creates the insert statement for the database, it will then look for three parameters and replace these three @ placeholders with the values you put into the parameters.
Sure, you could concatenate it all together in a string, but then you have to worry about things like the “O’Malley Issue” and SQL Injection attacks. (See Bill Vaughn’s book “Hitchhiker’s Guide to Visual Studio and SQL Server, 7th edition for a complete discussion on these topics, or browse the web. There’s lots of info so I won’t take up further space now.)
Here’s the entire LoadARow routine. Note that my choosing to name the method parameters the same as the SSCE command parameters is entirely a coincidence, it simply makes it self documenting and is not a requirement.
private void LoadARow(string first, string last, string url)
{
SqlCeConnection cn = new SqlCeConnection(ConnectString());
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlCeCommand cmd;
string sql = “insert into CoolPeople “
+ “(LastName, FirstName, URL) “
+ “values (@lastname, @firstname, @url)”;
try
{
cmd = new SqlCeCommand(sql, cn);
cmd.Parameters.AddWithValue(“@lastname”, first);
cmd.Parameters.AddWithValue(“@firstname”, last);
cmd.Parameters.AddWithValue(“@url”, url);
cmd.ExecuteNonQuery();
lblResults.Text = “Row Added.”;
}
catch (SqlCeException sqlexception)
{
MessageBox.Show(sqlexception.Message, “Oh Crap.”
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “Oh Crap.”
, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cn.Close();
}
}
After creating the SqlCeCommand object by passing in the sql string and the connection object, I can then add the parameters with a single line for each. By using the Parameters object of the command object, I can call the AddWithValue method, and simply pass in the parameter name as a string and the value for that parameter. Once you add all the parameters, simply call the ExecuteNonQuery method and the data is inserted!
This method can be a basis for all your future work with SSCE. Everything you need to do revolves around the command object and sending SQL commands to the database. Need to delete a record? Just change the SQL from an insert to a delete, pass the correct parameters and you are in business. Update? Same thing.
Using the code I’ve shown in this series you can create your own SSCE applications, and store data locally. Lest you think this is the wrap up, I have one more cool method to show you, but that’ll be for tomorrow!


March 8, 2008 at 10:33 pm
I linked a combobox to a mobile database in a visual studio app.. bt i cant enter any data into that table. how to do it??
September 16, 2008 at 4:48 am
hi i m facing the problem to insert data into database in mobile appliction i have witten the code to insert data in c# and its working fine excutenonquery returns one row but when i select data directly from the database in dont have data.and when i insert data directy intodatabase then data is inserted and show in appliction.
plz help me i can not understand that how to resolve it
July 1, 2009 at 7:14 am
Hey If you have got answer to your problem can you email me on chintan_shah8@yahoo.com cos I am facing the same prob.
Regards,
Chintan
September 19, 2008 at 1:46 pm
Thanks a lot!!!
I had problems inserting dates and your code solved my problem.
October 21, 2008 at 12:24 am
@Ashu :
Even i hv the same problem if u get ne solution for it then plz do mail me too on my id NayalShruti@ymail.com plz as soon as you get do reply me plz plz !
October 21, 2008 at 12:25 am
@Ashu :
Even i hv the same problem if u get ne solution for it then plz do mail me too on my id NayakShruti@ymail.com plz as soon as you get do reply me plz plz !
November 10, 2008 at 10:51 pm
Hi,
I am facing problem in the connection string. Please give me the syntax of connection string.
Thanks,
Umesh
November 11, 2008 at 2:56 pm
See my article, http://arcanecode.wordpress.com/2007/01/25/create-a-sql-server-compact-edition-database-with-c/ for details on the connection string. Also be sure to click on the Arcane Lessons link at the top of this page and scroll down to review all the lessons around SSCE.
November 16, 2009 at 7:57 am
hi,
Thanks a lot
this code is very useful 2 me
December 22, 2009 at 2:16 pm
The records are not added even though the query returns 1 may be due to you forget to close the connection at the end.
This solved my problem.
Dont forget the finally block. You may close the connection outside the finally block but the important point is to close the connection for insertion to be reflected in the database.
Please notify me through mail if this solution works for you.