如何使用 SQL Server 身份验证进行连接

如题所述

SQL Server 身份验证只能在无法进行 Windows 身份验证时使用。有关使用 Windows 身份验证进行连接的信息,请参阅如何使用 Windows 身份验证进行连接。 在使用 SQL Server 身份验证连接到 SQL Server 时必须考虑以下几点: 必须对服务器启用 SQL Server 混合模式身份验证。
在尝试建立连接时必须设置 UID 和PWD 连接属性。UID 和PWD 必须映射到有效的 SQL Server 用户和密码。
注意: 包含右大括号 (}) 的密码必须使用另一个右大括号进行转义。例如,如果 SQL Server 密码为“pass}word”,则 PWD 连接属性的值必须设置为“pass}}word”。
在使用 SQL Server 身份验证连接到 SQL Server 时应采取以下预防措施: 保护(加密)通过网络从 Web 服务器传递到数据库的凭据。默认情况下,SQL Server 2005 和 SQL Server 2008 将对凭据进行加密。为了提高安全性,请将“Encrypt”连接属性设置为“on”,以便对发送至服务器的所有数据进行加密。
注意: 将“Encrypt”连接属性设置为“on”可能导致性能降低,因为数据加密可能是一项计算密集型操作。
在PHP 脚本的纯文本部分中不要包含 UID 和PWD 连接属性的值。这些值应存储在具有相应受限权限的特定应用程序目录中。
避免使用 sa 帐户。将应用程序映射到拥有所需权限的数据库用户,并使用强密码。
注意: 在建立连接时可以设置除 UID 和PWD 之外的连接属性。有关支持的连接属性的完整列表,请参阅 sqlsrv_connect。
示例 下面的示例使用 SQL Server 身份验证连接到 SQL Server 的本地实例。所需的 UID 和PWD 连接属性的值是从 C:\AppData 目录中特定应用程序的文本文件 uid.txt 和pwd.txt 中提取的。建立连接之后,将查询服务器以验证用户登录名。 此示例假定本地计算机上已安装了 SQL Server 和 AdventureWorks 数据库。当从浏览器运行此示例时,所有的输出都将写入该浏览器。 <?php /* Specify the server and connection string attributes. */ $serverName = "(local)"; /* Get UID and PWD from application-specific files. */ $uid = file_get_contents("C:\AppData\uid.txt"); $pwd = file_get_contents("C:\AppData\pwd.txt"); $connectionInfo = array( "UID"=>$uid, "PWD"=>$pwd, "Database"=>"AdventureWorks"); /* Connect using SQL Server Authentication. */ $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "Unable to connect.</br>"; die( print_r( sqlsrv_errors(), true)); } /* Query SQL Server for the login of the user accessing the database. */ $tsql = "SELECT CONVERT(varchar(32), SUSER_SNAME())"; $stmt = sqlsrv_query( $conn, $tsql); if( $stmt === false ) { echo "Error in executing query.</br>"; die( print_r( sqlsrv_errors(), true)); } /* Retrieve and display the results of the query. */ $row = sqlsrv_fetch_array($stmt); echo "User login: ".$row[0]."</br>"; /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-22
SQL
Server
身份验证只能在无法进行
Windows
身份验证时使用。有关使用
Windows
身份验证进行连接的信息,请参阅如何使用
Windows
身份验证进行连接。
在使用
SQL
Server
身份验证连接到
SQL
Server
时必须考虑以下几点:
必须对服务器启用
SQL
Server
混合模式身份验证。
在尝试建立连接时必须设置
UID
和PWD
连接属性。UID
和PWD
必须映射到有效的
SQL
Server
用户和密码。
注意:
包含右大括号
(})
的密码必须使用另一个右大括号进行转义。例如,如果
SQL
Server
密码为“pass}word”,则
PWD
连接属性的值必须设置为“pass}}word”。
在使用
SQL
Server
身份验证连接到
SQL
Server
时应采取以下预防措施:
保护(加密)通过网络从
Web
服务器传递到数据库的凭据。默认情况下,SQL
Server
2005

SQL
Server
2008
将对凭据进行加密。为了提高安全性,请将“Encrypt”连接属性设置为“on”,以便对发送至服务器的所有数据进行加密。
注意:
将“Encrypt”连接属性设置为“on”可能导致性能降低,因为数据加密可能是一项计算密集型操作。
在PHP
脚本的纯文本部分中不要包含
UID
和PWD
连接属性的值。这些值应存储在具有相应受限权限的特定应用程序目录中。
避免使用
sa
帐户。将应用程序映射到拥有所需权限的数据库用户,并使用强密码。
注意:
在建立连接时可以设置除
UID
和PWD
之外的连接属性。有关支持的连接属性的完整列表,请参阅
sqlsrv_connect。
示例
下面的示例使用
SQL
Server
身份验证连接到
SQL
Server
的本地实例。所需的
UID
和PWD
连接属性的值是从
C:\AppData
目录中特定应用程序的文本文件
uid.txt
和pwd.txt
中提取的。建立连接之后,将查询服务器以验证用户登录名。
此示例假定本地计算机上已安装了
SQL
Server

AdventureWorks
数据库。当从浏览器运行此示例时,所有的输出都将写入该浏览器。
<?php
/*
Specify
the
server
and
connection
string
attributes.
*/
$serverName
=
"(local)";
/*
Get
UID
and
PWD
from
application-specific
files.
*/
$uid
=
file_get_contents("C:\AppData\uid.txt");
$pwd
=
file_get_contents("C:\AppData\pwd.txt");
$connectionInfo
=
array(
"UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"AdventureWorks");
/*
Connect
using
SQL
Server
Authentication.
*/
$conn
=
sqlsrv_connect(
$serverName,
$connectionInfo);
if(
$conn
===
false
)
{
echo
"Unable
to
connect.</br>";
die(
print_r(
sqlsrv_errors(),
true));
}
/*
Query
SQL
Server
for
the
login
of
the
user
accessing
the
database.
*/
$tsql
=
"SELECT
CONVERT(varchar(32),
SUSER_SNAME())";
$stmt
=
sqlsrv_query(
$conn,
$tsql);
if(
$stmt
===
false
)
{
echo
"Error
in
executing
query.</br>";
die(
print_r(
sqlsrv_errors(),
true));
}
/*
Retrieve
and
display
the
results
of
the
query.
*/
$row
=
sqlsrv_fetch_array($stmt);
echo
"User
login:
".$row[0]."</br>";
/*
Free
statement
and
connection
resources.
*/
sqlsrv_free_stmt(
$stmt);
sqlsrv_close(
$conn);
?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网