基于服务的数据库visual studio c#

时间:2014-05-29 11:06:42

标签: c# sql visual-studio-2010

我需要帮助我的项目。我在visual studio中使用基于服务的数据库,当我使用dataset和sqldataAdapter添加一行时,添加了行,然后我使用另一种方法加载数据库的内容,它显示了数据库的旧数据和我插入了新行。但是,当应用程序关闭时,数据库不会更新,感谢您的回复:

这是在数据库中插入行的代码

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

1 个答案:

答案 0 :(得分:0)

尝试将此更改为

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

到这个

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