为什么DataSet不在数据库中创建新表?

时间:2015-05-27 18:00:26

标签: c# database

在数据库中创建新表时遇到一个小问题。我在DataSet对象中创建了一个新表,但是这个表不是在物理数据库中创建的。该表仅在缓存中创建。为什么呢?

我的代码:

SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Projects\ConsoleApplication6\ConsoleApplication6\Data.sdf");
SqlCeDataAdapter sCEdata = new SqlCeDataAdapter("select * from [Cats]", con);
DataSet ds = new DataSet();
sCEdata.Fill(ds);

DataColumn ID = new DataColumn("ID", typeof(int));
ID.AllowDBNull = false;
ID.AutoIncrementSeed = 0;
ID.AutoIncrement = true;
ID.AutoIncrementStep = 1;
ID.ReadOnly = true;
ID.Unique = true;
DataColumn Name = new DataColumn("Name", typeof(string));
DataColumn Owner = new DataColumn("Owner", typeof(string));
DataColumn Note = new DataColumn("Note", typeof(string));

DataTable Cats2 = new DataTable("Cats2");
Cats2.Columns.AddRange(new DataColumn[]{ID, Name, Hozain, Note});

DataRow dr1 = Cats2.NewRow();
DataRow dr2 = Cats2.NewRow();
DataRow dr3 = Cats2.NewRow();
dr1["Name"] = "Pavel"; dr1["Owner"] = "Sergey"; dr1["Note"] = "Starii";
dr2["Name"] = "Gleb"; dr2["Owner"] = "Inga"; dr2["Note"] = "Tupaya";
dr3["Name"] = "Dusia"; dr3["Owner"] = "Olga"; dr3["Note"] = "Zlaya";

Cats2.Rows.Add(dr1); 
Cats2.Rows.Add(dr2); 
Cats2.Rows.Add(dr3);

dr2["Note"] = "Guzelle"; 

ds.Tables.Add(Cats2);

SqlCeCommandBuilder build = new SqlCeCommandBuilder(sCEdata); 

sCEdata.Update(ds);

1 个答案:

答案 0 :(得分:1)

数据库只知道在使用SQL命令时必须更改某些内容。您可以根据需要创建任意数量的DataSet,删除和插入数据,但数据库保持不变,直到您通过SQL命令指示它进行更改。