如何使用ASP.NET MVC链接到远程数据库?

时间:2022-09-21 08:22:54

I have an ASP.NET MVC application up and running, using a SQL Sever express 2005 database instance that is running on my development machine. When the app gets deployed to production, however, the IIS instance and the SQL Server instance will be on different logical machines. I dont know if they're different physical boxes, but i doubt that matters.

我使用在我的开发机器上运行的SQL Sever express 2005数据库实例启动并运行了一个ASP.NET MVC应用程序。但是,当应用程序部署到生产环境时,IIS实例和SQL Server实例将位于不同的逻辑计算机上。我不知道他们是不是有不同的物理盒子,但我怀疑这很重要。

The production DB server is currently in place, with a populated DB on it. Since I have read access to that instance, I'd like to point the project on my dev machine to it to make sure the remote DB Connection still works. The schema is the same, obviously, just the location and contents are different.

生产数据库服务器当前已就位,其上有一个填充的数据库。由于我对该实例具有读访问权限,因此我想将项目指向我的开发机器,以确保远程数据库连接仍然有效。架构是一样的,显然,只是位置和内容不同。

I currently have the project linked up to the .mdf file located on my development machine. What changes to I need to make to switch it over to the DB instance on the remote server?

我目前将项目链接到我的开发机器上的.mdf文件。我需要做什么更改才能将其切换到远程服务器上的数据库实例?

1 个解决方案

#1


The easy answer: change the connection string for your data layer (Linq to SQL?) in the web.config to point to your out-of-process database instance.

答案很简单:在web.config中更改数据层的连接字符串(Linq to SQL?),指向进程外数据库实例。

In more detail, here are the steps I took to split the database out of my site on my development box. By doing the following, you can test the two-server configuration locally before deploying it to production.

更详细地说,以下是我在开发框中将数据库从我的站点拆分的步骤。通过执行以下操作,您可以在将其部署到生产环境之前在本地测试双服务器配置。

  1. Install SQL Server Management Studio
  2. 安装SQL Server Management Studio

  3. Open Management Studio and connect to your local SQL Server Express instence (typicaly [boxname]/SQLEXPRESS)
  4. 打开Management Studio并连接到本地SQL Server Express instence(典型[boxname] / SQLEXPRESS)

  5. Right-click on the Databases folder in Management Server and select Attach...
  6. 右键单击Management Server中的Databases文件夹,然后选择Attach ...

  7. Locate your .mdf file in you project and open it.
  8. 在项目中找到.mdf文件并将其打开。

  9. Rename the newly attached database to what you want it called.
  10. 将新连接的数据库重命名为您想要的名称。

  11. Give whatever service account your IIS application pool runs under permissions to the database. First create an account under the top level Security/Logins folder, and then add that account to the new database's Security/Users folder. For my configuration, I am using NETWORK SERVICE.
  12. 根据数据库的权限,为IIS应用程序池运行任何服务帐户。首先在*Security / Logins文件夹下创建一个帐户,然后将该帐户添加到新数据库的Security / Users文件夹中。对于我的配置,我正在使用NETWORK SERVICE。

Now that your database is mounted as a stand-alone database in SQL Server Express, you can connect to the database using a normal SQL Server connection string.

现在您的数据库已作为SQL Server Express中的独立数据库挂载,您可以使用普通的SQL Server连接字符串连接到数据库。

At this point, your development environment is simulating the production environment where the database and the IIS process are split. You only have to change your connection string in production to point to the production database.

此时,您的开发环境正在模拟分割数据库和IIS进程的生产环境。您只需将生产中的连接字符串更改为指向生产数据库。

#1


The easy answer: change the connection string for your data layer (Linq to SQL?) in the web.config to point to your out-of-process database instance.

答案很简单:在web.config中更改数据层的连接字符串(Linq to SQL?),指向进程外数据库实例。

In more detail, here are the steps I took to split the database out of my site on my development box. By doing the following, you can test the two-server configuration locally before deploying it to production.

更详细地说,以下是我在开发框中将数据库从我的站点拆分的步骤。通过执行以下操作,您可以在将其部署到生产环境之前在本地测试双服务器配置。

  1. Install SQL Server Management Studio
  2. 安装SQL Server Management Studio

  3. Open Management Studio and connect to your local SQL Server Express instence (typicaly [boxname]/SQLEXPRESS)
  4. 打开Management Studio并连接到本地SQL Server Express instence(典型[boxname] / SQLEXPRESS)

  5. Right-click on the Databases folder in Management Server and select Attach...
  6. 右键单击Management Server中的Databases文件夹,然后选择Attach ...

  7. Locate your .mdf file in you project and open it.
  8. 在项目中找到.mdf文件并将其打开。

  9. Rename the newly attached database to what you want it called.
  10. 将新连接的数据库重命名为您想要的名称。

  11. Give whatever service account your IIS application pool runs under permissions to the database. First create an account under the top level Security/Logins folder, and then add that account to the new database's Security/Users folder. For my configuration, I am using NETWORK SERVICE.
  12. 根据数据库的权限,为IIS应用程序池运行任何服务帐户。首先在*Security / Logins文件夹下创建一个帐户,然后将该帐户添加到新数据库的Security / Users文件夹中。对于我的配置,我正在使用NETWORK SERVICE。

Now that your database is mounted as a stand-alone database in SQL Server Express, you can connect to the database using a normal SQL Server connection string.

现在您的数据库已作为SQL Server Express中的独立数据库挂载,您可以使用普通的SQL Server连接字符串连接到数据库。

At this point, your development environment is simulating the production environment where the database and the IIS process are split. You only have to change your connection string in production to point to the production database.

此时,您的开发环境正在模拟分割数据库和IIS进程的生产环境。您只需将生产中的连接字符串更改为指向生产数据库。