如何对 SQL Server Express 进行实时更改
我一直在使用 VS studio 开发一个 ASP.NET Web 应用程序。我正在使用 SQL Server Express。 在开发过程中,我一直在我的服务器上测试我的网络应用程序。
每次我需要更新数据库时,我都会简单地删除旧数据库(位于我的服务器上)并上传新数据库。由于我只是测试,并且我的应用程序没有用户,所以这不是问题。
问题:
我的网站上线后,我不知道如何更改我的数据库。显然我无法简单地删除它,因为它包含用户数据。那么人们通常如何更新实时数据库。也就是说,假设我的网站已上线,现在我需要向数据库添加更多表和存储过程。我如何在实时网站上执行此操作?
I've been developing an asp.net web app using VS studio. I'm using SQL Server Express.
During development I have been testing my web app on my server.
Every time that I need to update my database I would simply delete my old database (located on my server) and upload my new DB. Since I'm only testing, and my app has no users, it hasn't been a problem.
Problem:
Once my site goes live I don't know how to make changes to my DB. Obviously I wont be able to simply delete it as it will contain user data. So how do people typically update a live DB. That is, lets say my site is live and now I need to add more tables and stored procedures to my DB. How would i do this on a live site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要对生产数据库进行更改,您可以:
步骤 2 所需的脚本应已在开发和测试/QA 环境中进行测试,以确保生产系统中遇到尽可能少的问题。开发和测试/QA 环境都需要允许您将数据库恢复到应用程序以前版本的备份。
To make changes to a production database, you'd:
The scripts necessary for step 2 should have been tested in Development and Test/QA environments to ensure as few issues as possible are experienced in the Production system. Backups that allow you to restore the database to previous versions of the application are required for both the Development and Test/QA environments.
您需要:
第二个选项涉及为任何新的不可为 NULL 的列提供合理的默认值,确保所有 INSERT 语句都使用列列表(例如 INSERT INTO dbo.MyTable (col1, col2, col3) VALUES (...) )并确保新的存储过程参数具有默认值。这不是一份详尽的清单,但却是一个好的开始。
You either need to:
The second option involves giving any new non-NULLable columns sensible defaults, ensuring that all INSERT statements use column lists (e.g.
INSERT INTO dbo.MyTable (col1, col2, col3) VALUES (...)
) and ensuring that new stored procedure parameters have defaults. This is not an exhaustive list, but a good start.大多数情况下,我们都会创建发布脚本,从而将更改编写成脚本并可重复。因此,您会看到类似的内容
您可以通过以下内容判断哪些对象已更改:
有些人使用 RedGate 软件来比较 2 个不同的数据库,但这样做是有成本的。
Mostly we create release scripts whereby the changes are scripted out and repeatable. So you will see things like
You can tell which objects have changed with stuff like:
Some people use RedGate software to compare the 2 different database, but there is a cost to that.
这取决于您想要对实时数据库进行的更改类型。
在您的问题中,您只讨论添加新表和存储过程。
只要您只添加新内容(表、存储过程,甚至向现有表添加新列),您就不必执行任何特殊操作,因为 SQL Server 可以在数据库使用时执行此操作并且这些更改不会影响您的用户,因为他们的应用程序版本甚至不知道您新添加的内容。
另一方面,如果您更改甚至删除现有内容,事情会变得更加复杂。
这很有可能对您的应用程序来说是一个重大更改,因为当表看起来与预期不同或者它尝试访问不再存在的表/存储过程时,它可能会停止工作。
(即使您只添加新的东西,就像我在开始时所说的那样 - 您可能会想更新您的应用程序,这样它实际上可以使用数据库中的新东西)
所以您可能需要更改数据库并部署您的应用程序的新版本也是如此,两者同时进行。
我不是 ASP.NET 专家,但据我所知,在不踢出所有活跃用户的情况下更新 ASP.NET 应用程序是不可能的。
因此,在这种情况下,您必须执行什么OMG Ponies 已经说过:选择受影响用户数量尽可能少的时间,和/或尽早通知您的用户计划的中断。
It depends on that kinds of changes you want to make to your live database.
In your question you only talk about adding new tables and stored procedures.
As long as you only add new stuff (tables, sprocs, or even new columns to existing tables) you don't have to do anything special, because SQL Server can do this while the database is in use and the changes don't affect your users because their version of your app doesn't even know about your newly added stuff.
On the other hand, it gets way more complicated if you change or even delete existing stuff.
There is a great chance that this will be a breaking change for your app, as it will probably stop working when tables look different than it expects or if it tries to access tables/sprocs that don't exist anymore.
(Even if you only add new stuff like I said in the beginning - you will probably want to update your app anyway, so it can actually USE the new stuff in the database)
So you probably will need to make your database changes AND deploy a new version of your app as well, both at the same time.
I'm no ASP.NET expert, but as far as I know it's not possible to update an ASP.NET app without all active users getting kicked out.
So in this case you would have to do what OMG Ponies already said: choose a time when the least possible number of users is affected, and/or inform your users early enough about the scheduled outage.