简体   繁体   English

WCF:远程SQL Server 2005数据库连接

[英]WCF : remote SQL Server 2005 database connection

I'd like to know how to link my WCF application with a remote SQL Server database. 我想知道如何将WCF应用程序与远程SQL Server数据库链接。 By remote, I mean that is on the same network than me but not on the same computer/project. 远程是指与我位于同一网络上,但不在同一计算机/项目上。 I've the controll of the computer where the database is stored on. 我控制着存储数据库的计算机。

What I've done so far : create my WCF application and try to add an ADO.NET connection. 到目前为止,我已经完成了什么:创建WCF应用程序,并尝试添加ADO.NET连接。 My issue : where to find the name of the server ? 我的问题:在哪里可以找到服务器的名称? (and also : is it the good way to proceed ?). (并且:这是进行下一步的好方法吗?)。

Thanks ! 谢谢 !

where to find the name of the server? 在哪里可以找到服务器的名称?

Three options: 三种选择:

  • whoever "owns" the database server tells you the details, and you put them in a configuration file (or some other configuration system) 拥有数据库服务器的人告诉您详细信息,然后将它们放在配置文件(或其他配置系统)中
  • whoever "owns" the database server tells some key user the details, and the user puts them into a screen / api in the application 谁“拥有”数据库服务器的人告诉一些关键用户详细信息,然后用户将其放入应用程序的屏幕/ api中
  • something like the above, but you try to discover sql servers at runtime via SqlDataSourceEnumerator (not a fan of this option, to be honest) 类似于上面的内容,但是您尝试在运行时通过SqlDataSourceEnumerator发现sql服务器(说实话,不是此选项的支持者)

Conntion string should look like 连接字符串应如下所示

 Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;

In Place of myServerName you can use IPAdress of machine 可以使用机器的IPAdress代替myServerName

I suggest you add connectionString in the Web.config file of the application 我建议您在应用程序的Web.config文件中添加connectionString

<add name="connectionString"
      connectionString="Data Source=ServerName/PC-Name;Initial Catalog=DatabaseName;User ID=userid;Password=pass"
      providerName="System.Data.SqlClient" />

Use the connection string in your code/Logic 在代码/逻辑中使用connection string

string conn = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;

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

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