简体   繁体   English

数据库文件不会更新

[英]Database file doesn't update

I have created a Windows Forms application that is supposed to add a new record into a database. 我创建了一个Windows Forms应用程序,它应该将新记录添加到数据库中。 Now, it adds it successfully and the new data can be used... but when I close the application and start it again, the application acts as if nothing has changed. 现在,它成功地添加它并且可以使用新数据......但是当我关闭应用程序并再次启动时,应用程序就好像什么都没有改变一样。

The database was created in SQL Server and the application uses the .mdf file it generated. 数据库是在SQL Server中创建的,应用程序使用它生成的.mdf文件。

Here is the method: 这是方法:

private void btnUnos_Click(object sender, EventArgs e)
{
            //create an instance of the row to be inserted
            PIScarinaDataSet.OsobaRow novaOsoba;
            novaOsoba = pIScarinaDataSet.Osoba.NewOsobaRow();

            //fill the attributes
            novaOsoba.Ime = txtImeOsobe.Text;
            novaOsoba.Drzavnost = dobijDrzavu();
            novaOsoba.Predstavlja = dobijPredstavnika();

            //insert into the database
            this.Validate();
            this.pIScarinaDataSet.Osoba.Rows.Add(novaOsoba);
            this.osobaTableAdapter1.Update(this.pIScarinaDataSet.Osoba);
            this.osobaBindingSource1.EndEdit();
}

Since this is what helped you, I will make it an answer: 既然这对你有帮助,我会回答:

[S]ince you're using the .MDF created by SQL Server, you need to make sure the "Copy to Output Directory" property on the file is set to "Copy if newer" in Visual Studio. 如果您正在使用SQL Server创建的.MDF,则需要确保文件中的“Copy to Output Directory”属性在Visual Studio中设置为“Copy if newer”。 (Or, "Do not copy" if you're going to manually put it in the correct directory.) That is, if you're actually saving the data and this is the issue. (或者,如果您要手动将其放在正确的目录中,请“复制”。)也就是说,如果您实际上是在保存数据,那就是问题所在。

You didn't show us the crucial part - the connection string of your application - but I'm guessing that it will contain something like AttachDbFileName=yourdatabase.mdf somewhere in there. 你没有向我们展示关键部分 - 你的应用程序的连接字符串 - 但我猜它会包含像AttachDbFileName=yourdatabase.mdf这样的东西。

The whole User Instance and AttachDbFileName= approach is flawed - at best! 整个User Instance和AttachDbFileName =方法存在缺陷 - 充其量! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\\bin\\debug - where you app runs) and most likely , your INSERT works just fine - but you're just looking at the wrong .mdf file in the end! 在Visual Studio中运行应用程序时,它将复制.mdf文件(从App_Data目录到输出目录 - 通常是.\\bin\\debug - 应用程序运行的地方)并且很可能 ,你的INSERT运行正常 - 但是你最后只是看错了.mdf文件

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there. 如果你想坚持这种方法,那么尝试在myConnection.Close()调用上设置一个断点 - 然后用SQL Server Mgmt Studio Express检查.mdf文件 - 我几乎可以肯定你的数据在那里。

The real solution in my opinion would be to 我认为真正的解决方案

  1. install SQL Server Express (and you've already done that anyway) 安装SQL Server Express(你已经完成了)

  2. install SQL Server Management Studio Express 安装SQL Server Management Studio Express

  3. create your database in SSMS Express , give it a logical name (eg MyDatabase ) SSMS Express中创建数据库,给它一个逻辑名称(例如MyDatabase

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. 使用其逻辑数据库名称 (在服务器上创建时给定)连接到它 - 并且不要乱用物理数据库文件和用户实例。 In that case, your connection string would be something like: 在这种情况下,您的连接字符串将类似于:

     Data Source=.\\\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True 

    and everything else is exactly the same as before... 其他一切都和以前完全一样......

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

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