Mossos
Mossos

Reputation: 97

Updating strongly typed Dataset from local Sql Compact database

I created with Visual Studio 2010 Express a Sql Compact Database (sdf-File), with several tables and relations. From this database I generated a strongly typed dataset.

Now I want to fill the dataset with the content from the database. So I used following code to connect to the database and fill the dataset.

//My strongly typed dataset
localData = new LokalStorageDataSet();

//SqlCe-Connection
localConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\TempStorage.sdf; Password='xyc';Persist Security Info=True");

localConnection.Open();
SqlCeCommand command = new SqlCeCommand("SELECT * FROM progstates", localConnection);
localDataAdapter = new SqlCeDataAdapter(command);

localDataAdapter.Fill(localData);

The problem is that no rows were added to my dataset, although in the database there are rows. If I use a SqlCeReader to read the results of the SqlCeCommand, the reader return the rows:

SqlCeDataReader dr = command.ExecuteReader();

        while (dr.Read())
        {
          ...
        }

Can you give me the reason why my dataset don't get the rows from the table in the dataset?

EDIT: I saw that in my dataset a new datatable were created named 'Table' with the same columns like the one I requested (progstate) but also this datatable has a rowCount of 0.

Upvotes: 0

Views: 1826

Answers (1)

Derek
Derek

Reputation: 8640

EDITED *****

Try This :-

//My strongly typed dataset
localData = new LokalStorageDataSet();

//SqlCe-Connection
localConnection = new SqlCeConnection(@"Data Source=|DataDirectory|\TempStorage.sdf; Password='xyc';Persist Security Info=True");


SqlCeCommand command = new SqlCeCommand("SELECT * FROM progstates", localConnection);

localConnection.Open();

var dataReader = command.ExecuteReader();

localData.Load(dataReader,LoadOption.Upsert,localData.Tables[0]);

localConnection.Close();

Or You cant do this with DataAdapter like this :-

localDataAdapter = new SqlCeDataAdapter();

SqlCeCommand command = new SqlCeCommand("SELECT * FROM progstates", localConnection);

localDataAdapter.SelectCommand = command;

localConnection.Open();

localDataAdapter.SelectCommand.ExecuteNonQuery();
localDataAdapter.Fill(localData.Tables[0]);

localConnection.Close();

Upvotes: 1

Related Questions