简体   繁体   English

如何从WCF Web服务访问SQL Server?

[英]How to access SQL Server from my WCF web service?

VS Express 2012, SQL Server Express 2012, Win 8.1 VS Express 2012,SQL Server Express 2012,Win 8.1

Hello, 你好,

I have a (very) simple WCF hosted as a web service on IIS. 我有一个(非常)简单的WCF作为IIS上的Web服务托管。 I also have a SQL Server instance (with 1 table) installed on the same machine. 我也在同一台计算机上安装了一个SQL Server实例(带有1个表)。

I need a step-by-step guide on how to connect to SQL from the WCF (VB) and retrieve a single record from the table (ie: "SELECT LAST NAME FROM MYTABLE WHERE PK = 1;"). 我需要有关如何从WCF(VB)连接到SQL并从表中检索单个记录的逐步指南(即:“从MYTABLE WHERE PK = 1中选择姓氏”)。 That's it. 而已。 I don't need a 1,200 page manual -- which is all Google keeps throwing at me. 我不需要一本1200页的手册,这是Google一直向我扔的所有手册。

Anyone know of a quick, clean resource? 有人知道快速,干净的资源吗?

Thanks, Jason 谢谢,杰森

There is no difference on using ADO.NET in a WCF service or in a normal application from the point of view of the classes required. 从所需类的角度来看,在WCF服务或普通应用程序中使用ADO.NET并没有区别。

The first thing needed is a connection string that allows your SqlConnection object to find the host and the database that you want to use. 所需的第一件事是允许您的SqlConnection对象找到要使用的主机和数据库的连接字符串。 Here examples on connection strings 这里是关于连接字符串的例子

Then you usually need a SqlCommand that encapsulates the SQL text and parameters needed to retrieve the data (Here you setup your SELECT statement and conditions) 然后,通常需要一个SqlCommand,其中封装了检索数据所需的SQL文本和参数(在此处设置SELECT语句和条件)

Finally you need a SqlDataReader that get the result of the command execution and allows you to loop over the results. 最后,您需要一个SqlDataReader来获取命令执行的结果,并允许您遍历结果。

Here a sample that could get you started. 这里有一个示例,可以帮助您入门。

Keep in mind that this is just a minimal todo-list and there are numerous other ways to work with data. 请记住,这只是一个最小的待办事项清单,还有许多其他处理数据的方法。 Basic objects like SqlDataAdapter, Dataset, DataTable present different ways to load data from a database and use that data. 诸如SqlDataAdapter,Dataset,DataTable之类的基本对象提供了从数据库加载数据和使用该数据的不同方法。 Then on top of these there are technologies like Linq To Sql and Object Relational Mapper tools that abstract the data access and offer high level functionality on top of data. 然后在这些之上,还有诸如Linq To Sql对象关系映射器工具之类的技术,它们抽象化数据访问并在数据之上提供高级功能。

That's probably the reason you get so much informations on data access technologies 这可能就是您获得大量有关数据访问技术信息的原因

The main classes that are involved are SqlConnection and SqlCommand . 涉及的主要类是SqlConnectionSqlCommand The documentation of the classes contains some samples on how to use them. 这些类的文档包含一些有关如何使用它们的示例。 To get you started, here is a small sample: 为了帮助您入门,这里有一个小样本:

Dim connStr = "Data Source=SQLServerName\InstanceName;Initial Catalog=DatabaseName;Integrated Security=SSPI"
Using conn As New SqlConnection(connStr)
    conn.Open()
    Using cmd = conn.CreateCommand()
        cmd.CommandText = "SELECT LAST_NAME FROM MYTABLE WHERE PK = @pk"
        cmd.Parameters.AddWithValue("@pk", 1)
        Dim result = cmd.ExecuteScalar()
        If Typeof result Is DbNull Then
            ' Handle null value
        Else
            ' Otherwise
        End If
    End Using
End Using

This sample assumes that you want to retrieve a single cell as in your statement. 本示例假定您要检索语句中的单个单元格。 If you want to retrieve tabular data, have a look a the SqlDataReader or SqlDataAdapter class. 如果要检索表格数据,请查看SqlDataReaderSqlDataAdapter类。 Please note that - especially in server applications - it is important to dispose of the created instances properly. 请注意,尤其是在服务器应用程序中,正确处理创建的实例非常重要。

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

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