简体   繁体   中英

Connect to a SQL Server database on another network via asp.net and c#

I have a customer database that is kept on a SQL Server on our local network. I would like to create a customer portal that will be on our website that is hosted through another company. How would I connect to that SQL Server database?

Give the website host access rights to the sql server. Assuming Sql Server 2008; go to your management studio and right click the server (root) in the object explorer window and go to properties. You can manage permissions from there. Also, it will show you the "server" to use in your connection string (something like [server]\\SQLEXPRESS , which can be used locally and remotely).

Create a proper connection string in the website, preferably in web.config, to use for all of your connections to the database. You can then get this connection string from, say, your data layer via

ConfigurationManager.ConnectionStrings["ConnString_Name"].ConnectionString;

Aside from the correct connection string, you will also need to ensure that the website can communicate with your SQL Server. If you have firewalls, you'll need to configure ports if they are blocked.

The alternative is to create a web service that is hosted on a DMZ zone that will communicate with your sql server internally. The website (hosted by the third party) would communicate via this web service to get the data (you can setup authentication so only those with rights can use this web service). By going this route, you're not exposing your internal sql server directly.

This answer is based on some assumptions because question does not provide all the required information.

For this you need to set ConnectionString property for your connection object. For example

Data Source=yourIP;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

Here is MSDN link connectionStrings

This is a example of SQLExpress connectionstring in Web.Config

<connectionStrings>
   <add 
      name="LocalSqlServer" 
      connectionString="data source=.\SQLEXPRESS;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;" 
      providerName="System.Data.SqlClient"
   />
</connectionStrings>

There is a Beginners guide on Code Project which is voted 5, it will give you all you need to get started.

But before you start working with the code, I suggest that you first test the connection with SQL Server management studio. make sure that you can connect and query some data, otherwise you may face some more confusion while trying to pull this off with code only at the first time.

To connect to SQL Server from C#.NET, you need to create a connection string such as below:

private SqlConnection connection; private string connectionString = @"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123"; connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:

SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods: ExecuteReader - to execute SELECT queries ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database. For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-NK-Research-Embedding-SQL-in-C-Sharp-Java.html ) Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.

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