简体   繁体   中英

make permanent changes for database

I am working on a windows desktop application using C# and MS SQL 2012. I am using Visual Studio 2013 Ultimate. I am using the connection string from configuration manager.

My Connection String : Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\ABC_database.mdf;Initial Catalog=ABCDProject;Integrated Security=True

I have stored some data to the database now and created the setup file. During debugging, the changes which i made to the data are not reflected to the database as i view from the server explorer.

When I searched, many suggestions stated that it could be due to the copy to output directory property. Hence I changed it as : "copy if newer"

Also if i would like to store the data permanently to the database. In case if i want to reinstall my application, all the data will be permanently saved to the database and when i reinstall the application, it will still contain all the data.

Please suggest me the proper , feasible solution.

I am using the following code :

private void saveUser() 
{ 
     using (SqlConnection con = new SqlConnection(conn))
     using (SqlCommand cmd = new SqlCommand("INSERT INTO USERS (id, password, date, status) VALUES (@id, @password, @date,'NEW USER')", con))  
      {
           cmd.Parameters.AddWithValue("id", id);
           cmd.Parameters.AddWithValue("password", password);
           cmd.Parameters.AddWithValue("date", date);
           con.Open();
           cmd.ExecuteNonQuery();
           con.Close(); 
      }
 } 

Try SqlDataAdapter() to update the record. Attach your SqlCommand to the SqlAdapter.InsertCommand property, then execute it, then call Update on the SqlAdapter object giving table as parameter. That should do the magic. Regards.

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