简体   繁体   English

对数据库进行永久更改

[英]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. 我正在使用C#和MS SQL 2012在Windows桌面应用程序上工作。我正在使用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 我的连接字符串:数据源=(LocalDB)\\ v11.0; AttachDbFilename = | DataDirectory | \\ ABC_database.mdf;初始目录= ABCDProject;集成安全性= 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. 尝试使用SqlDataAdapter()更新记录。 Attach your SqlCommand to the SqlAdapter.InsertCommand property, then execute it, then call Update on the SqlAdapter object giving table as parameter. 将您的SqlCommand附加到SqlAdapter.InsertCommand属性,然后执行它,然后在SqlAdapter对象上调用Update,并将表作为参数。 That should do the magic. 那应该做魔术。 Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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