简体   繁体   English

从本地Sql Compact数据库更新强类型数据集

[英]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. 我使用Visual Studio 2010 Express创建了一个带有几个表和关系的Sql Compact数据库(sdf文件)。 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: 如果我使用SqlCeReader读取SqlCeCommand的结果,则读取器将返回以下行:

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. 编辑:我看到在我的数据集中创建了一个名为“表”的新数据表,具有与我请求的列相同的列(progstate),但该数据表的rowCount为0。

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 :- 或者您不能使用DataAdapter这样执行此操作:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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