问题
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?
回答1:
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.
回答2:
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.
回答3:
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(e.g 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?
来源:https://stackoverflow.com/questions/40843573/connecting-to-sql-server-database-using-a-hosted-web-application