Microsoft SQL Server 客户端编程的主页Homepage for client programming to Microsoft SQL Server

欢迎访问与 Microsoft SQL Server 和云中的 Azure SQL 数据库进行交互的客户端编程的主页。Welcome to our homepage about client programming to interact with Microsoft SQL Server, and with Azure SQL Database in the cloud. 本文提供了以下信息:This article provides the following information:

  • 列出并介绍了可用的语言和驱动程序组合。Lists and describes the available language and driver combinations.
    • 介绍了操作系统 Linux(Ubuntu 及其他)、macOS 和 Windows。Information is given for the operating systems of Linux (Ubuntu and others), macOS, and Windows.
  • 收录了每个组合的详细文档的链接。Provides links to the detailed documentation for each combination.
  • 适当时,显示某些语言的分层文档的区域和子区域。Displays the areas and subareas of the hierarchical documentation for certain languages, where appropriate.

Azure SQL DatabaseAzure SQL Database

在任何给定语言中,用于连接到 SQL Server 和 Azure SQL 数据库的代码几乎完全相同。In any given language, the code that connects to SQL Server is almost identical to the code for connecting to Azure SQL Database.

若要详细了解用于连接到 Azure SQL 数据库的连接字符串,请参阅:For details about the connection strings for connecting to Azure SQL Database, see:

“生成应用程序”网页Build-an-app webpages

“生成应用程序” 网页以另一种格式提供代码示例和配置信息。Our Build-an-app webpages present code examples, along with configuration information, in an alternative format. 有关详细信息,请参阅本文稍后将介绍的标记为“‘生成应用程序’网站” 的部分For more information, see later in this article the section labeled Build-an-app website.

用于客户端程序的语言和驱动程序Languages and drivers for client programs

下表中的每个语言图像都是一个链接,可便于详细了解如何结合使用相应语言与 SQL Server。In the following table, each language image is a link to detail about using the language with SQL Server. 每个链接都可跳转到本文稍后将介绍的部分。Each link jumps to a later section in this article.

C# 徽标C# logo

Node.js 徽标Node.js logo

Python 徽标Python logo

Java 徽标Java logo

PHP 徽标PHP logo

下载和安装Downloads and installs

下面的文章专门介绍如何下载和安装各种 SQL 连接驱动程序,以供编程语言使用:The following article is devoted to the download and install various SQL connection drivers, for use by programming languages:

C# 徽标 使用 ADO.NET 的 C#C# using ADO.NET

.NET 托管语言(如 C# 和 Visual Basic)最常使用 ADO.NET。The .NET managed languages, such as C# and Visual Basic, are the most common users of ADO.NET. ADO.NET 是 .NET Framework 类子集的临时名称。ADO.NET is a casual name for a subset of .NET Framework classes.

代码示例Code examples

示例Example 描述Description
使用 ADO.NET 连接到 SQL 的概念证明Proof of concept connecting to SQL using ADO.NET 专注于如何连接和查询 SQL Server 的小型代码示例。A small code example focused on connecting and querying SQL Server.
使用 ADO.NET 实现对 SQL 的弹性连接Connect resiliently to SQL with ADO.NET 代码示例中的重试逻辑,因为连接有时可能会断开。Retry logic in a code example, because connections can occasionally experience moments of connectivity loss.

重试逻辑非常适用于通过 Internet 维持连接到任何云数据库(如 Azure SQL 数据库)的连接。Retry logic applies well to connections maintained through the internet into any cloud database, such as to Azure SQL Database.
Azure SQL 数据库:展示了如何在 Windows/Linux/macOS 上使用 .NET Core 创建 C# 程序并进行连接和查询Azure SQL Database: Demonstration of how to use .NET Core on Windows/Linux/macOS to create a C# program, to connect and query Azure SQL 数据库示例。Azure SQL Database example.
生成应用程序:C#、ADO.NET、WindowsBuild-an-app: C#, ADO.NET, Windows 配置信息和代码示例。Configuration information, along with code examples.
 

文档Documentation

区域Area 说明Description
使用 ADO.NET 的 C#C# using ADO.NET 文档的根目录。Root of our documentation.
命名空间:System.DataNamespace: System.Data 用于 ADO.NET 的类集。A set of classes used for ADO.NET.
命名空间:Microsoft.Data.SqlClientNamespace: Microsoft.Data.SqlClient 用于 Microsoft .NET Data Provider for SQL Server 的类集The set of classes used for Microsoft .NET Data Provider for SQL Server
 

实体框架徽标 使用 C# 的 Entity Framework (EF)Entity Framework (EF) with C#

Entity Framework (EF) 提供对象关系映射 (ORM)。Entity Framework (EF) provides Object-Relational Mapping (ORM). 通过 ORM,面向对象的编程 (OOP) 源代码可以更轻松地操作从关系 SQL 数据库中检索到的数据。ORM makes it easier for your Object-Oriented Programming (OOP) source code to manipulate data that was retrieved from a relational SQL database.

EF 与以下技术有直接或间接关系:EF has direct or indirect relationships with the following technologies:

  • .NET Framework.NET Framework
  • LINQ to SQLLINQ to EntitiesLINQ to SQL, or LINQ to Entities
  • 语言语法增强功能,如 C# 中的 => 运算符。Language syntax enhancements, such as the => operator in C#.
  • 为映射到 SQL 数据库中表的类生成源代码的便捷程序。Handy programs that generate source code for classes, which map to the tables in your SQL database. 例如,EdmGen.exeFor instance, EdmGen.exe.

原始 EF 和新 EFOriginal EF, and new EF

Entity Framework 的入门页介绍了 EF,说明如下所示:The start page for Entity Framework introduces EF with a description similar to this:

  • Entity Framework 是一种对象关系映射程序 (O/RM),可方便 .NET 开发人员使用 .NET 对象处理数据库。Entity Framework is an object-relational mapper (O/RM) that enables .NET developers to work with a database using .NET objects. 开发人员无需再像往常一样编写大部分数据访问源代码。It eliminates the need for most of the data-access source code that developers usually need to write.

Entity Framework 是由两个单独的源代码分支共用的名称。Entity Framework is a name shared by two separate source code branches. 一个 EF 分支较旧,它的源代码现在采用公共维护。One EF branch is older, and its source code can now be maintained by the public. 另一个 EF 分支是新的。The other EF is new. 下面介绍了这两个 EF 分支:The two EFs are described next:

版本Version 说明Description
EF 6.xEF 6.x Microsoft 第一次发布 EF 是在 2008 年 8 月。Microsoft first released EF in August 2008. 2015 年 3 月,Microsoft 宣布了 EF 6.x 是 Microsoft 开发的最终版本。In March 2015, Microsoft announced that EF 6.x was the final version that Microsoft would develop. Microsoft 将源代码发布到了公共域。Microsoft released the source code into the public domain.

EF 最初属于 .NET Framework。Initially EF was part of .NET Framework. 但后来,EF 6.x 从 .NET Framework 中脱离。But EF 6.x was removed from .NET Framework.

GitHub 存储库 aspnet/EntityFramework6 中的 EF 6.x 源代码 EF 6.x source code on GitHub, in repository aspnet/EntityFramework6
EF CoreEF Core 2016 年 6 月,Microsoft 发布了新开发的 EF Core。Microsoft released the newly developed EF Core in June 2016. EF Core 旨在提升灵活性和可移植性。EF Core is designed for better flexibility and portability. 可运行 EF Core 的操作系统不止 Microsoft Windows。EF Core can run on operating systems beyond just Microsoft Windows. EF Core 可以与之交互的数据库不止 Microsoft SQL Server 和其他关系数据库。And EF Core can interact with databases beyond just Microsoft SQL Server and other relational databases.

C# 代码示例:C# code examples:
Entity Framework Core 入门Getting Started with Entity Framework Core
开始对现有数据库使用 .NET Framework 上的 EF CoreGetting started with EF Core on .NET Framework with an Existing Database
 

EF 及其相关技术非常强大,对于想要掌握整个领域的开发人员来说,有很多东西需要学习。EF and related technologies are powerful, and are a lot to learn for the developer who wants to master the entire area.

 

Java 徽标 Java 和 JDBCJava and JDBC

Microsoft 提供了 Java Database Connectivity (JDBC) 驱动程序,可用于 SQL Server(或 Azure SQL 数据库)。Microsoft provides a Java Database Connectivity (JDBC) driver for use with SQL Server (or with Azure SQL Database). 它是 Type 4 JDBC 驱动程序,通过标准 JDBC 应用程序编程接口 (API) 提供数据库连接。It is a Type 4 JDBC driver, and it provides database connectivity through the standard JDBC application program interfaces (APIs).

代码示例Code examples

示例Example 描述Description
代码示例Code examples 介绍数据类型、结果集和大型数据的代码示例。Code examples that teach about data types, result sets, and large data.
连接 URL 示例Connection URL Sample 介绍了如何使用连接 URL 连接到 SQL Server。Describes how to use a connection URL to connect to SQL Server. 然后介绍了如何使用 SQL 语句来检索数据。Then use it to use an SQL statement to retrieve data.
数据源示例Data Source Sample 介绍了如何使用数据源连接到 SQL Server。Describes how to use a data source to connect to SQL Server. 然后介绍了如何使用存储过程来检索数据。Then use a stored procedure to retrieve data.
使用 Java 查询 Azure SQL 数据库Use Java to query an Azure SQL database Azure SQL 数据库示例。Azure SQL Database example.
创建在 Ubuntu 上使用 SQL Server 的 Java 应用程序Create Java apps using SQL Server on Ubuntu 配置信息和代码示例。Configuration information, along with code examples.
 

文档Documentation

JDBC 文档包括以下主要方面:The JDBC documentation includes the following major areas:

领域Area 说明Description
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC) JDBC 文档的根目录。Root of our JDBC documentation.
参考Reference 接口、类和成员。Interfaces, classes, and members.
JDBC SQL 驱动程序编程指南Programming Guide for JDBC SQL Driver 配置信息和代码示例。Configuration information, along with code examples.
 

Node.js 徽标 Node.jsNode.js

利用 Node.js,可以从 Windows、Linux 或 macOS 连接到 SQL Server。With Node.js you can connect to SQL Server from Windows, Linux, or macOS. Node.js 文档的根目录位于此处The root of our Node.js documentation is here.

用于 SQL Server 的 Node.js 连接驱动程序是用 JavaScript 实现的。The Node.js connection driver for SQL Server is implemented in JavaScript. 此驱动程序使用 TDS 协议,所有新式版 SQL Server 都支持这种协议。The driver uses the TDS protocol, which is supported by all modern versions of SQL Server. 此驱动程序是 GitHub 上的开放源代码项目。The driver is an open-source project, available on GitHub.

代码示例Code examples

示例Example 描述Description
使用 Node.js 连接到 SQL 的概念证明Proof of concept connecting to SQL using Node.js 用于连接到 SQL Server 和执行查询的基本功能源代码。Bare bones source code for connecting to SQL Server, and executing a query.
Azure SQL 数据库:使用 Node.js 进行查询Azure SQL database: Use Node.js to query 云中的 Azure SQL 数据库的示例。Example for Azure SQL Database in the cloud.
创建在 macOS 上使用 SQL Server 的 Node.js 应用程序Create Node.js apps to use SQL Server on macOS 配置信息和代码示例。Configuration information, along with code examples.
 

ODBC for C++ODBC for C++

ODBC 徽标 cpp-big-plus

开放式数据库连接 (ODBC) 是在 20 世纪 90 年代开发的,比 .NET Framework 更早。Open database connectivity (ODBC) was developed in the 1990s, and it predates .NET Framework. ODBC 旨在独立于任何特定数据库系统和操作系统。ODBC is designed to be independent of any particular database system, and independent of operating system.

多年来,Microsoft 内外的团队已经创建和发布了许多 ODBC 驱动程序。Over the years numerous ODBC drivers have been created and released by groups within and outside of Microsoft. 驱动程序的范围涉及几种客户端编程语言。The range of drivers involve several client programming languages. 数据目标列表远不止包含 SQL Server。The list of data targets goes well beyond SQL Server.

其他一些连接驱动程序在内部使用 ODBC。Some other connectivity drivers use ODBC internally.

代码示例Code example

文档大纲Documentation outline

本部分中的 ODBC 内容重点介绍了如何从 C++ 访问 SQL Server 或 Azure SQL 数据库。The ODBC content in this section focuses on accessing either SQL Server or Azure SQL Database, from C++. 下表列出了主要 ODBC 文档的大纲。The following table lists an approximate outline of the major documentation for ODBC.

区域Area 子区域Subarea 说明Description
ODBC for C++ODBC for C++ 文档的根目录。Root of our documentation.
Linux-macOSLinux-macOS   介绍了如何在 Linux 或 macOS 操作系统上使用 ODBC。Information about using ODBC on the Linux or macOS operating systems.
WindowsWindows   介绍了在 Windows 操作系统上使用 ODBC。Information about using ODBC on the Windows operating system.
管理Administration   用于管理 ODBC 数据源的管理工具。The administrative tool for managing ODBC data sources.
MicrosoftMicrosoft   Microsoft 创建和提供的各种 ODBC 驱动程序。Various ODBC drivers that are created and provided by Microsoft.
概念和参考Conceptual and reference   除了传统参考之外,还介绍了 ODBC 接口的概念性信息。Conceptual information about the ODBC interface, in addition to traditional reference.
  "  " 附录Appendixes 状态转换表、ODBC 游标库等。State transition tables, ODBC cursor library, and more.
  "  " 开发应用程序Develop app 函数、句柄等。Functions, handles, and much more.
  "  " 开发驱动程序Develop driver 如何开发你自己的 ODBC 驱动程序(如果有特殊化数据源的话)。How to develop your own ODBC driver, if you have a specialized data source.
  "  " 安装Install ODBC 安装、子项等。ODBC installation, subkeys, and more.
  "  " 语法Syntax 用于安装、安装程序、转换和数据访问的 API。APIs for setup, installer, translation, and data access.
   

PHP 徽标 PHPPHP

可以使用 PHP 与 SQL Server 进行交互。You can use PHP to interact with SQL Server. PHP 文档的根目录位于此处The root of our PHP documentation is here.

代码示例Code examples

示例Example 描述Description
使用 PHP 连接到 SQL 的概念验证Proof of concept connecting to SQL using PHP 专注于如何连接和查询 SQL Server 的小型代码示例。A small code example focused on connecting and querying SQL Server.
使用 PHP 实现对 SQL 的弹性连接Connect resiliently to SQL with PHP 代码示例中的重试逻辑,因为连接是通过 Internet 实现,且云有时可能会断开连接。Retry logic in a code example, because connections through the Internet and the cloud can occasionally experience moments of connectivity loss.
Azure SQL 数据库:使用 PHP 进行查询Azure SQL database: Use PHP to query Azure SQL 数据库示例。Azure SQL Database example.
创建在 RHEL 上使用 SQL Server 的 PHP 应用程序Create PHP apps to use SQL Server on RHEL 配置信息和代码示例。Configuration information, along with code examples.
 

Python 徽标 PythonPython

可以使用 Python 与 SQL Server 进行交互。You can use Python to interact with SQL Server.

代码示例Code examples

示例Example 描述Description
使用 pyodbc Python 连接到 SQL 的概念证明Proof of concept connecting to SQL with Python using pyodbc 专注于如何连接和查询 SQL Server 的小型代码示例。A small code example focused on connecting and querying SQL Server.
Azure SQL 数据库:使用 Python 进行查询Azure SQL database: Use Python to query Azure SQL 数据库示例。Azure SQL Database example.
创建在 SLES 上使用 SQL Server 的 PHP 应用程序Create PHP apps to use SQL Server on SLES 配置信息和代码示例。Configuration information, along with code examples.
 

文档Documentation

区域Area 说明Description
使用 Python 连接到 SQL ServerPython to SQL Server 文档的根目录。Root of our documentation.
pymssql 驱动程序pymssql driver Microsoft 不维护或测试 pymssql 驱动程序。Microsoft does not maintain or test the pymssql driver.

pymssql 连接驱动程序是连接到 SQL 数据库的简单接口,以供在 Python 程序中使用。The pymssql connection driver is a simple interface to SQL databases, for use in Python programs. pymssql 在 FreeTDS 的基础之上构建,以提供连接到 Microsoft SQL Server 的 Python DB-API (PEP-249) 接口。Pymssql builds on top of FreeTDS to provide a Python DB-API (PEP-249) interface to Microsoft SQL Server.
pyodbc 驱动程序pyodbc driver pyodbc 连接驱动程序是开放源代码 Python 模块,它简化了访问 ODBC 数据库的过程。The pyodbc connection driver is an open-source Python module that makes accessing ODBC databases simple. 它虽实现了 DB API 2.0 规范,但提供了更多 Pythonic 便利。It implements the DB API 2.0 specification, but is packed with even more Pythonic convenience.
 

Ruby 徽标 RubyRuby

可以使用 Ruby 与 SQL Server 进行交互。You can use Ruby to interact with SQL Server. Ruby 文档的根目录位于此处The root of our Ruby documentation is here.

代码示例Code examples

示例Example 描述Description
使用 Ruby 连接到 SQL 的概念证明Proof of concept connecting to SQL with Ruby 专注于如何连接和查询 SQL Server 的小型代码示例。A small code example focused on connecting and querying SQL Server.
Azure SQL 数据库:使用 Ruby 进行查询Azure SQL database: Use Ruby to query Azure SQL 数据库示例。Azure SQL Database example.
创建在 macOS 上使用 SQL Server 的 Ruby 应用程序Create Ruby apps to use SQL Server on macOS 配置信息和代码示例。Configuration information, along with code examples.
 

用于 SQL 客户端开发的“生成应用程序”网站Build-an-app website, for SQL client development

“生成应用程序” 网页上,可以从用于连接到 SQL Server 的编程语言长列表中进行选择。On our Build-an-app webpages, you can choose from a long list of programming languages for connecting to SQL Server. 客户端程序可以运行各种操作系统。And your client program can run a variety of operating systems.

“生成应用程序” 着重为刚开始接触的开发人员提供了简单、完整的体验。Build-an-app emphasizes simplicity and completeness for the developer who is just getting started. 这些步骤解释了以下任务:The steps explain the following tasks:

  1. 如何安装 Microsoft SQL ServerHow to install Microsoft SQL Server
  2. 如何下载并安装工具和驱动程序。How to download and install tools and drivers.
  3. 如何根据选定操作系统进行任何必要的配置。How to make any necessary configurations, as appropriate for your chosen operating system.
  4. 如何编译提供的源代码。How to compile the provided source code.
  5. 如何运行该程序。How to run the program.

下面是网站上的详细信息的一些大纲式要点:Next are a couple approximate outlines of the detail provided on the website:

Ubuntu 上的 JavaJava on Ubuntu

  1. 设置你的环境Set up your environment
    • 步骤 1.1:安装 SQL ServerStep 1.1 Install SQL Server
    • 步骤 1.2:安装 JavaStep 1.2 Install Java
    • 步骤 1.3:安装 Java 开发工具包 (JDK)Step 1.3 Install the Java Development Kit (JDK)
    • 步骤 1.4:安装 MavenStep 1.4 Install Maven
  2. 创建使用 SQL Server 的 Java 应用程序Create Java application with SQL Server
    • 步骤 2.1:创建连接到 SQL Server 并执行查询的 Java 应用程序Step 2.1 Create a Java app that connects to SQL Server and executes queries
    • 步骤 2.2:使用热门框架 Hibernate 创建连接到 SQL Server 的 Java 应用程序Step 2.2 Create a Java app that connects to SQL Server using the popular framework Hibernate
  3. 让 Java 应用程序的速度提高 100 倍Make your Java app up to 100x faster
    • 步骤 3.1:创建用于展示列存储索引的 Java 应用程序Step 3.1 Create a Java app to demonstrate Columnstore indexes

Windows 上的 PythonPython on Windows

  1. 设置你的环境Set up your environment
    • 步骤 1.1:安装 SQL ServerStep 1.1 Install SQL Server
    • 步骤 1.2:安装 PythonStep 1.2 Install Python
    • 步骤 1.3:安装用于 SQL Server 的 ODBC 驱动程序和 SQL 命令行实用工具Step 1.3 Install the ODBC Driver and SQL Command Line Utility for SQL Server
  2. 创建使用 SQL Server 的 Python 应用程序Create Python application with SQL Server
    • 步骤 2.1:安装 Python Driver for SQL ServerStep 2.1 Install the Python driver for SQL Server
    • 步骤 2.2:创建应用程序的数据库Step 2.2 Create a database for your application
    • 步骤 2.3:创建连接到 SQL Server 并执行查询的 Python 应用程序Step 2.3 Create a Python app that connects to SQL Server and executes queries
  3. 让 Python 应用程序的速度提高 100 倍Make your Python app up to 100x faster
    • 步骤 3.1:使用 sqlcmd 创建包含 500 万条数据的新表Step 3.1 Create a new table with 5 million using sqlcmd
    • 步骤 3.2:创建查询此表并度量耗时的 Python 应用程序Step 3.2 Create a Python app that queries this table and measures the time taken
    • 步骤 3.3:度量查询运行时长Step 3.3 Measure how long it takes to run the query
    • 步骤 3.4:向表中添加列存储索引Step 3.4 Add a columnstore index to your table
    • 步骤 3.5:度量使用列存储索引运行查询所需的时长Step 3.5 Measure how long it takes to run the query with a columnstore index

下面的屏幕截图展示了 SQL 开发文档网站的外观。The following screenshots give you an idea of what our SQL development documentation website looks like.

选择语言Choose a language

SQL 开发网站 - 开始使用

 

选择操作系统Choose an operating system

SQL 开发网站 - Java Ubuntu

 

其他开发Other development

本部分收录了其他开发选项的链接。This section provides links about other development options. 其中包括通常在 Azure 开发中使用这些相同的语言。These include using these same languages for Azure development in general. 这些信息不仅仅针对 Azure SQL 数据库和 Microsoft SQL Server。The information goes beyond targeting just Azure SQL Database and Microsoft SQL Server.

适用于 Azure 的开发人员中心Developer hub for Azure

其他语言Other languages