简体   繁体   English

通过asp.net和c#连接到另一个网络上的SQL Server数据库

[英]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. 我有一个客户数据库,该数据库保存在我们本地网络上的SQL Server上。 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? 我将如何连接到该SQL Server数据库?

Give the website host access rights to the sql server. 授予网站主机对sql服务器的访问权限。 Assuming Sql Server 2008; 假设使用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). 另外,它将显示连接字符串中要使用的“服务器”(类似于[server]\\SQLEXPRESS ,可以在本地和远程使用)。

Create a proper connection string in the website, preferably in web.config, to use for all of your connections to the database. 在网站上(最好在web.config中)创建一个正确的连接字符串 ,以用于与数据库的所有连接。 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. 除了正确的连接字符串外,您还需要确保该网站可以与您的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. 替代方法是创建一个托管在DMZ区域上的Web服务,该区域将在内部与sql Server通信。 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). 该网站(由第三方托管)将通过此Web服务进行通信以获取数据(您可以设置身份验证,以便只有那些有权利的人才能使用此Web服务)。 By going this route, you're not exposing your internal sql server directly. 通过这种方法,您不会直接暴露内部sql服务器。

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. 为此,您需要为连接对象设置ConnectionString属性。 For example 例如

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

Here is MSDN link connectionStrings 这是MSDN链接connectionStrings

This is a example of SQLExpress connectionstring in Web.Config 这是Web.Config中SQLExpress连接字符串的示例

<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. 有一个关于Code Project的初学者指南,被评为5分,它将为您提供入门所需的一切。

But before you start working with the code, I suggest that you first test the connection with SQL Server management studio. 但是,在开始使用代码之前,建议您首先测试与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: 要从C#.NET连接到SQL Server,您需要创建一个如下的连接字符串:

private SqlConnection connection; 私有SqlConnection连接; private string connectionString = @"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123"; 私有字符串connectionString = @“ Server =(local);数据库= Embedding_SQL_Test;用户ID = sa;密码= 123”; connection = new SqlConnection( connectionString ); connection =新的SqlConnection(connectionString);

Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below: 接下来,使用上面创建的SqlConnection对象创建一个“ SqlCommand”,如下所示:

SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection); SqlCommand cmd =新的SqlCommand(“从客户那里选择* *,其中CustomerID = @Cid”,连接);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc. 此处显示的SQL查询可以替换为SELECT,INSERT,UPDATE查询等。

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. 接下来要在数据库中执行SQL查询,您可以使用以下方法:ExecuteReader-执行SELECT查询ExecuteNonQuery-执行INSERT,DELETE,UPDATE和SET语句。

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database. 这是关于如何从C#连接到SQL Server数据库以及如何在数据库中执行SQL查询的简短描述。 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. 有关连接字符串,方法及其参数的详细信息,请检查以下链接:( http://www.shahriarnk.com/Shahriar-NK-Research-Embedding-SQL-in-C-Sharp-Java.html )还将找到有关如何将参数传递给SQL查询以及调用存储过程的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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