Cannot get data saved to my SQL dbase
-
Hi everyone. I am on the LAST beginner tutorial on MSDN in C#. I have a couple questions. I am adding to my database with the code : this.cd_tableTableAdapter.Insert(cdName, cdDescription, image); this.cd_tableTableAdapter.Fill(this.cdTrackerDataSet.cd_table); This is SUPPOSED to write to my database and of course it shows up while the app is running, but it NEVER writes it to the database. I thought I remember from a previous tutorial that I had to do an actual UPDATE on the database to make sure it got written. Is this correct? Can someone help me permanently write this info to my dbase? Thanks!
-
Hi everyone. I am on the LAST beginner tutorial on MSDN in C#. I have a couple questions. I am adding to my database with the code : this.cd_tableTableAdapter.Insert(cdName, cdDescription, image); this.cd_tableTableAdapter.Fill(this.cdTrackerDataSet.cd_table); This is SUPPOSED to write to my database and of course it shows up while the app is running, but it NEVER writes it to the database. I thought I remember from a previous tutorial that I had to do an actual UPDATE on the database to make sure it got written. Is this correct? Can someone help me permanently write this info to my dbase? Thanks!
There are many ways to write data to the db: With SqlDataAdapter: FETCH DATA: SqlConnection con = new SqlConnection( connectionstring); SqlCommand command = new SqlCommand("select* from table", con); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet set = new DataSet(); adapter.Fill(set); CHANGE DATA: //get row to change DataRow row = set.Select("Columnx = 1"); row("Columnx") = 2; //Write to DB adapter.Update(set); //commit dataset set.AcceptChanges(); INSERT NEW ROW: DataRow newrow = set.Tables[0].NewRow(); newrow("Columnx") = x; newrow("Columny") = y; set.Tables[0].Rows.Add(newrow); adapter.Update(set); set.AcceptChanges(); Connection does not have to be opened nor closed when using adapter. You can also use direct SQL like so.. SqlConnection con = new SqlConnection( connectionstring); SqlCommand command = new SqlCommand("insert into table values(1,2) ", con); con.Open(); command.ExecuteNonQuery(); con.Close();