2
votes

I'm running some experiments to grasp the concept behind managing databases in C# an Visual Studio. What I did was creating a simple sdf database with just one table "Books", consisting of two collumns - auto-incrementing key "ID" and "Author" field. I tried to create a DataSet object to load, modify and store changes. The program writes two numbers to console - the count of rows in the DataSet right after loading data from database, and one after modifying it but before updating it to the sdf.

What happens, and has me at a total loss, is this:
First run, I get exactly what I expected to see, that is 0 1 - empty table was successfully loaded, and an item was added to the dataset.

When I run the code again I get 1 2. Then 2 3, 3 4 and so forth, as expected. Untill I refresh the database and try to see it's contents via Server Explorer - all that I see is empty table. Also when I run the program once again afterwards, I get 0 1 response like in the beginning.

Here's the code I'm using:

namespace DatasetTesting{
class Program
{
    static void Main(string[] args)
    {
        string conString = "Data Source=Books.sdf;Persist Security Info=False;Password=pass";
        SqlCeConnection objConn = new SqlCeConnection(conString);
        objConn.Open();

        SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from Books", objConn);
        BooksDataSet booksSet = new BooksDataSet();
        da.FillSchema(booksSet,SchemaType.Source, "Books");
        da.Fill(booksSet, "Books");

        Console.Out.WriteLine(booksSet.Books.Rows.Count);

        BooksDataSet.BooksRow book = booksSet.Books.NewBooksRow();
        book.Author = "Tolkien";

        booksSet.Books.AddBooksRow(book);

        Console.Out.WriteLine(booksSet.Books.Rows.Count);

        SqlCeCommandBuilder comBuilder = new SqlCeCommandBuilder(da);
        da.Update(booksSet, "Books");

        Console.In.ReadLine();
    }
}

I must admit that at this point I have no clue about what might be going wrong - I'd be grateful for any advice.

2
Close call...you almost just broadcast your password to the whole wide world. - KingCronus
Not really, no - I don't use my real passwords anywhere other than real aplications. I've only edited out the one I used in those test just so people wouldn't think I used my real pass xP - Mornar
I've heard of SQL gremlins - thought they were only mythical. Bits of data are a delicacy. Did you copy the .sdf to your project? It seems to be a common mistake to copy the .sdf to your project which is where the data is written - and then your SQL studio manager is looking at a different database. A mythical +1 for adding Tolkien to your library. - IAbstract
Oh God, I can't belive I didn't notice it =.= lAbstract, you were correct, this is exactly what caused the problem. Thanks a lot. - Mornar
Edited comment and entered as answer. - IAbstract

2 Answers

2
votes

Did you copy the .sdf to your project?

It seems to be a common mistake to copy the .sdf to your project which is where the data is written - and then your SQL studio manager is looking at a different database.

0
votes

It's been a long time since I've done a DataAdapter, but looking at the MSDN documentation, I think you have to call

da.UpdateCommand = comBuilder.GetUpdateCommand();

Before you update.