简体   繁体   English

使用托管的Web应用程序连接到SQL Server数据库

[英]Connecting to SQL Server Database using a Hosted Web Application

I have created a .net web application using c# that inserts, updates, and deletes data from an SQL server database. 我已经使用c#创建了一个.net Web应用程序,该应用程序可以从SQL Server数据库中插入,更新和删除数据。 I have tested it using my localhost and it works fine. 我已经使用本地主机对其进行了测试,并且工作正常。 Now I would like to publish this application on a hosted site. 现在,我想将此应用程序发布在托管站点上。

My question is, does the database need to be on the same server/host that the application is in for it to connect? 我的问题是,数据库是否需要与应用程序位于同一服务器/主机上才能进行连接?
Does anyone have any tips on how to implement this? 有没有人对如何实现这一目标有任何提示?

No you do not have to run the Database on the same server as the application. 不,您不必与应用程序在同一服务器上运行数据库。 For scalability it can be best practice to separate the two, one dedicated server for your DB and one server to host all your websites. 为了实现可伸缩性,最好的做法是将两者分开,一台用于数据库的专用服务器,另一台用于托管所有网站的服务器。

In your application you specify the connection to your Database. 在应用程序中,指定与数据库的连接。 On your Database server you can also then set restrictions on the IP to access this data if you really wanted for further security. 如果确实需要进一步的安全保护,则还可以在数据库服务器上为访问此数据的IP设置限制。

If your Question is 'How to connect database present on another server', then you have to Add IP address of it in the Web.config file as: 如果您的问题是“如何连接另一台服务器上存在的数据库”,则必须在Web.config文件中添加其IP地址,如下所示:

<connectionStrings>
    <add name="myConnectionString" connectionString="data source=myserver\SQLEXPRESS;initial catalog=myDatabase;uid=myUserName;password=myPassword;" />
</connectionStrings>

data source can be IP ADDRESS or PC NAME, where Database is stored. data source可以是存储数据库的IP地址或PC NAME。

does the database need to be on the same server/host that the application is in for it to connect? 数据库是否需要与应用程序位于同一服务器/主机上才能进行连接?

It's upto you to put the Database on the same machine or a different machine.You can choose following scenarios 您可以将数据库放在同一台计算机上或另一台计算机上。可以选择以下方案

  • If it's a simple website and you only have one Machine,then you can install Database on the same server(or if'ts already present) .In this case the connection string may or may not have to change depending on what type of connection string you use(eg if your sql server instance name is different,connection string will change) 如果这是一个简单的网站,并且只有一台计算机,则可以将数据库安装在同一台服务器上(或者如果不存在)。在这种情况下,连接字符串可能必须更改,也可以不必更改,这取决于连接字符串的类型您使用(例如,如果您的sql server实例名称不同,则连接字符串将更改)
  • Your website can get complex or process large amount of data,it makes sense to use a dedicated database server .This case connection string will change 您的网站可能会变得复杂或处理大量数据,因此使用专用的数据库服务器是很有意义的。这种情况下连接字符串将发生变化

Does anyone have any tips on how to implement this? 有没有人对如何实现这一目标有任何提示?

Easiest way to deal with this is using web.config transformation.All you have to do is create a new Web.Release.Config 解决此问题的最简单方法是使用web.config转换。您要做的就是创建一个新的Web.Release.Config。

Please refer this answer for more details How do I use Web.Config transform on my connection strings? 请参考此答案以获取更多详细信息。 如何在连接字符串上使用Web.Config转换?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM