简体   繁体   中英

C# SqlCeConnection not updating .sdf file, no data in the file

I have the following code:

string dbfile = "C:\\Users\\Ralph\\Documents\\Visual Studio 2012\\Projects\\WorksTimes\\WorksTimes\\Database.sdf";
        SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);
        connection.Open();

        DataSet data = new DataSet();

        DataTable dt = new DataTable("Table2");
        dt.Columns.Add(new DataColumn("ID", typeof(int)));
        dt.Columns.Add(new DataColumn("Name", typeof(string)));
        data.Tables.Add(dt);

        SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from [Table2]", connection);

        if (data != null && data.Tables.Count > 0)
        {
            System.Diagnostics.Debug.WriteLine("Test");

            DataRow dr = data.Tables["Table2"].NewRow();
            dr["ID"] = 0;
            dr["Name"] = "Jan";
            data.Tables["Table2"].Rows.Add(dr);

            data.Tables["Table2"].Rows.Add(new object[] { 1, "Jan" });
            System.Diagnostics.Debug.WriteLine(data.Tables["Table2"].Rows.Count);//returns 2
            adapter.Fill(data);
            adapter.Update(data);
            connection.Close();
        }

When I check the database file its still empty. Why isn't the code adding rows to my database file?

The Update command will update existing rows - but you seem to try to insert new rows which would be the task of the .Insert method (not the .Update() ) .... – marc_s 16 mins ago

Thanks, it looks like I was using the wrong things to try to do get the rows in my database.

To be honest, I'm not entirely sure why your code does not throw an exception and just silently does nothing.

The following is an example of how I would probably do it. I tested it and the data ends up in the *.sdf.

string dbfile = yourDbFile;
using (var connection = new SqlCeConnection("datasource=" + dbfile))
{
    using (var adapter = new SqlCeDataAdapter("select * from [Table2]", connection))
    {
        using (var builder = new SqlCeCommandBuilder(adapter))
        {
            // The CommandBuilder automatically builds INSERT, UPDATE 
            // and DELETE statements from the given SELECT statement.
            // otherwise, these commands would need to be specified
            // manually.
            builder.RefreshSchema();

            using (var data = new DataSet())
            {
                // Fill() opens the connection if necessary
                adapter.Fill(data); 

                // The "table", i.e. the result of the select statement,
                // does *not* have the name "Table2".
                // To reduce confusion, just work with the "table"
                // at index 0.
                var dr = data.Tables[0].NewRow();
                dr["ID"] = 0;
                dr["Name"] = "Jan";
                data.Tables[0].Rows.Add(dr);

                data.Tables[0].Rows.Add(new object[] { 1, "Jan" });
                adapter.Update(data);
            }
        }
    }
}

Things to note:

  • Use using when dealing with objects that implement IDisposable .
  • Use a CommandBuilder to automatically build INSERT, UPDATE and DELETE statements from the SELECT statement, or create the respective commands manually.
  • The Fill() (and Update() ) method automatically opens the connection if necessary. You don't need to open the connection manually.
  • You should not expect to be able to access the returned data via the tablename. Access it via the index to be sure.

You might want to read up on the Update method for further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM