0

I have a small problem with creating a new table in a database. I create a new table in my DataSet object, but this table is not created in the physical database. The table is created only in cache. Why?

My code:

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);
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
  • 4
    Datasets don't do that. – SLaks May 27 '15 at 18:02
  • 1
    how do you expect the database to know what you are creating without any valid Sql command also you are only doing a Select which returns data to an object not a Create ..where is the Create Table command..? I think you need to rethink your approach and write down the steps as to what you want to do.. also look up how to use the CreateTable command in sql – MethodMan May 27 '15 at 18:06
  • 2
    here is a good place to start reading @polyakov_s http://stackoverflow.com/questions/1348712/creating-a-sql-server-table-from-a-c-sharp-datatable – MethodMan May 27 '15 at 18:15
  • I think, DataAdapter updates physically database only using DataSet object. For example, i adding, new rows in tables without using sql commands, and its worked. And sorry me, i am from Russia and bad wrote in english, but understand )) – Polyakov Sergey May 27 '15 at 18:17

1 Answers1

1

The Database only knows that is has to change something when you use a SQL command. You can create as many DataSets as you like, delete and insert data, but the Database stays the same until you tell it to change via a SQL command.