0

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?

0

5 Answers 5

3

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Is it just that simple? Will the connection string be identical the ones I use when accessing it locally? How do I give my website host access rights? Am I doing it in sql server or is it something I do with my host?
Depends if you're using localhost in your conn. string. Check my update.
1

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.

1 Comment

I like this route. I'm not real familiar with creating web services. I'll look for some tutorials. Do you know any good ones you can direct me too?
0

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>

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.