When I try to add an item into the database, after running the app the data won't be in the database for long time memory. I don't know why, the connection is good and I already checked it many times.
当我尝试将项目添加到数据库中时,在运行应用程序后,数据将不会在数据库中长时间存储。我不知道为什么,连接很好,我已经多次检查过了。
private void InsertIncomeBtn_Click(object sender, RoutedEventArgs e)
{
float lf = (float)Convert.ToDouble(IncomeText.Text);
MessageBox.Show(lf.ToString());
DateTime date = (DateTime)MovDatePick.SelectedDate;
Movments mv1 = new Movments();
mv1.Sum = lf;
mv1.Date = date.Date;
db.TotalAll.FirstOrDefault().CurrentSum += lf;
db.Movments.Add(mv1);
db.SaveChanges();
IncomeWin.Close();
MainWindow m = new MainWindow();
m.Show();
}
I am using VS 2017 Community and writing a WPF app.
我正在使用VS 2017社区并编写WPF应用程序。
Thank for helping.
谢谢你的帮助。
1 个解决方案
#1
2
I'm assuming (from a lot of previous questions) that you have a SQL Server database, and your connection string most likely contains an AttachDbFileName=.....
entry - correct?
我假设(从很多以前的问题)你有一个SQL Server数据库,你的连接字符串很可能包含AttachDbFileName = .....条目 - 对吗?
If so: the whole AttachDbFileName= approach is flawed - at best! 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!
如果是这样:整个AttachDbFileName =方法是有缺陷的 - 充其量!在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
我认为真正的解决方案是
-
install SQL Server Express (and you've already done that anyway)
安装SQL Server Express(你已经完成了)
-
install SQL Server Management Studio Express
安装SQL Server Management Studio Express
-
create your database in SSMS Express, give it a logical name (e.g.
YourDatabase
)在SSMS Express中创建数据库,为其指定一个逻辑名称(例如YourDatabase)
-
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=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...
其他一切都和以前完全一样......
Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.
另请参阅Aaron Bertrand的优秀博客文章Bad bad to kick:使用AttachDbFileName获取更多背景信息。
#1
2
I'm assuming (from a lot of previous questions) that you have a SQL Server database, and your connection string most likely contains an AttachDbFileName=.....
entry - correct?
我假设(从很多以前的问题)你有一个SQL Server数据库,你的连接字符串很可能包含AttachDbFileName = .....条目 - 对吗?
If so: the whole AttachDbFileName= approach is flawed - at best! 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!
如果是这样:整个AttachDbFileName =方法是有缺陷的 - 充其量!在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
我认为真正的解决方案是
-
install SQL Server Express (and you've already done that anyway)
安装SQL Server Express(你已经完成了)
-
install SQL Server Management Studio Express
安装SQL Server Management Studio Express
-
create your database in SSMS Express, give it a logical name (e.g.
YourDatabase
)在SSMS Express中创建数据库,为其指定一个逻辑名称(例如YourDatabase)
-
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=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...
其他一切都和以前完全一样......
Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.
另请参阅Aaron Bertrand的优秀博客文章Bad bad to kick:使用AttachDbFileName获取更多背景信息。