简体   繁体   中英

Service based database visual studio c#

I need help with my project please. I am using a service based database in visual studio, When I am adding a row using dataset and sqldataAdapter, the row is added and then I load the content of the database using another method, it shows me the old data of the database and the new row I inserted. However when the application is closed, the database don't update, thank you for your replies:

This the code of inserting a row in the database

string sConnectionString;
            sConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TrainedFacesDB.mdf;Integrated Security=True;User Instance=True";

            SqlConnection objConn = new SqlConnection(sConnectionString);


            SqlDataAdapter daDistances = new SqlDataAdapter("Select * From TrainedFacesDistances", objConn);

            DataSet dsDistances = new DataSet();


            daDistances.FillSchema(dsDistances, SchemaType.Source, "TrainedFacesDistances");
            daDistances.Fill(dsDistances,"TrainedFacesDistances");


            DataRow newDistance = dsDistances.Tables["TrainedFacesDistances"].NewRow();

            newDistance["Name"] = "Unknown";
            newDistance["EyesDistance"] = ed;
            newDistance["EyesCornerDistance"] = ecd;
            newDistance["NoseWidth"] = nw;


            dsDistances.Tables["TrainedFacesDistances"].Rows.Add(newDistance);


            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(daDistances);

            daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand();
            daDistances.InsertCommand = cmdBuilder.GetInsertCommand();

            daDistances.Update(dsDistances, "TrainedFacesDistances");



            objConn.Close();

Try changing this to

daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand();
daDistances.InsertCommand = cmdBuilder.GetInsertCommand();

to this

daDistances.UpdateCommand = cmdBuilder.GetUpdateCommand(true);
daDistances.InsertCommand = cmdBuilder.GetInsertCommand(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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