[英]Automating the creation of a database in SQL Server hosted in Amazon RDS
I have been doing some research with SQL Server hosted in Amazon RDS. 我一直在对Amazon RDS中托管的SQL Server进行一些研究。 I am planning on having an application that requires multi tenancy solution - one database per customer with a core database for login etc. 我正计划有一个需要多租户解决方案的应用程序-每个客户一个数据库,带有用于登录的核心数据库等。
So Im playing with RDS/SQL Server but there are come problems I need to overcome. 因此,我正在玩RDS / SQL Server,但是有一些我需要克服的问题。 Firstly how do automate the creation of a database? 首先,如何自动创建数据库? I RDS I cannot just restore from a bak file and I want to create database schema from template. 我的RDS不能仅从bak文件中恢复,我想从模板创建数据库架构。 Does anybody have any experience with this and if so can you guide me with some tips? 有人对此有任何经验吗?如果可以,可以给我一些提示吗?
Thanks, 谢谢,
kseudo kseudo
Generate Scripts is a good starting point for this but if you truly want to automate the database creation you'll want to convert the script that is output from SQL Server's Generate Script into a template. 生成脚本是这样做的一个很好的起点,但是如果您确实要自动化数据库创建,则需要将SQL Server的“生成脚本”输出的脚本转换为模板。
A good way to do this would be to create a .NET application or even just a stored procedure that will do a find and replace on the script once you've made it into a template. 做到这一点的一种好方法是创建一个.NET应用程序,或者甚至是一个存储过程,一旦将其制作成模板,该存储过程便会在脚本上进行查找和替换。
In C# or in SQL build a dynamic query by including a variable in every object name: 在C#或SQL中,通过在每个对象名称中包含一个变量来构建动态查询:
CREATE DATABASE CustomerDB_1 ... "CREATE DATABASE CustomerDB_{0}" //Formatted String in C# 'CREATE DATABASE CustomerDB_' + @vchCustomerID //Dynamic SQL
If every object name has the customer ID in it and every database has a unique customer ID, then you can simply run the query after replacing the variables with the appropriate values and you're all set. 如果每个对象名称中都有客户ID,并且每个数据库都有唯一的客户ID,则只需将变量替换为适当的值即可简单地运行查询,并已完成设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.