简体   繁体   中英

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. 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.

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:

<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.

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)
  • 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

Please refer this answer for more details How do I use Web.Config transform on my connection strings?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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