user2582175
user2582175

Reputation: 1

How to connect my hosting website with asp.net to SQL Server Database on my local computer

I am a new developer and recently I want to develop a database website that used asp.net using c#. I tried to connect my website to SQL Server Database on the hosting domain that stored my web pages and I could access that database properly. However, if I want to access to SQL Server Database on my local computer that already connected to Internet. How can I do that? Please help to guide me step by step of how to do this.

Here is the connection string that I used to connect to SQL Server Database on the hosting domain that stored my web pages.

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="workstation id=MyDocuments.mssql.somee.com;packet size=4096;user id=xxxxxxx;pwd=xxxxxxxxxxxxxxx;data source=MyDocuments.mssql.somee.com;persist security info=False;initial catalog=MyDocuments"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>

Thanks you in advance for helps.

Upvotes: 0

Views: 5215

Answers (2)

bitfiddler
bitfiddler

Reputation: 2115

In order for the hosting service to access your database, your computer would need a public IP address and the hosting service would need routing back to your computer. That's probably not possible, but if it is then you would set the connection string to use your IP address as the host name something like this in place of the zeros:

connectionString="Provider=SQLOLEDB;Data Source=000.000.000.000;Initial Catalog=yourDBName;UID=******;PWD=******"

Google "SQL Server connection string examples" for other examples if you don't use OLE DB

If what you really want is a way to develop using your SQL database offline instead of your hosting service, you could run IIS on your PC that has SQL Server and then connect to it using a string like above with the hostname localhost.

Upvotes: 1

user2575950
user2575950

Reputation:

Try this

using (SqlConnection conn = new SqlConnection(...))
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText = "...";
conn.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
    // do the sutff
}
}

See this link for more details

Upvotes: 0

Related Questions