简体   繁体   English

在Amazon RDS中托管的SQL Server中自动创建数据库

[英]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应用程序,或者甚至是一个存储过程,一旦将其制作成模板,该存储过程便会在脚本上进行查找和替换。

  1. Create your template customer database with all the objects you want in it and then script out the database (all database objects). 使用所需的所有对象创建模板客户数据库,然后脚本出数据库(所有数据库对象)。
  2. 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.

相关问题 亚马逊 AWS RDS sql 服务器连接字符串 - Amazon AWS RDS sql server connection string 实体框架不会更新Amazon RDS SQL数据库 - Entity Framework do not update Amazon RDS SQL database Amazon RDS 与本地数据库 - Amazon RDS vs local database 从本地数据库更改为 SQL 服务器托管在服务器上 - Changing from local database to SQL Server hosted on a server 如何使用Windows Phone 8连接到服务器上已托管的SQL Server数据库 - How to connect to a SQL Server database already hosted on server with windows phone 8 Amazon AWS RDS-如何在C#中以编程方式创建MS SQL数据库? - Amazon AWS RDS - How to Programmatically Create a MS SQL Database in C#? 将我的第一个SQLCLR存储过程部署到Amazon RDS SQL Server实例 - Deploying my first SQLCLR stored procedure to Amazon RDS SQL Server instance 使用托管的Web应用程序连接到SQL Server数据库 - Connecting to SQL Server Database using a Hosted Web Application 数据未插入Azure上托管的SQL Server数据库 - Data is not being inserted into SQL Server Database hosted on Azure 网络共享上托管的SQL Server Express数据库 - 可能吗? - SQL Server Express Database hosted on Network Share - Is it Possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM