SQL Server Express – 维基百科,自由的百科全书
Microsoft SQL Server Express是微软公司所開發的关系数据库產品SQL Server的免費下載版本,可自由下載、分發(需經註冊)及使用。這個版本的設計是專門為嵌入式系统或較小型的程式而設計。本產品可遡源至過往隨同SQL Server 2000軟件分發的Microsoft Database Engine (MSDE),用以讓程式開發者在其測試其程式時建立一個系統需求較低的測試環境。自SQL Server 2005開始,本產品改用現時的名稱。
它繼承了多數的SQL Server功能與特性,像是Transact-SQL、SQL CLR等,相當適合使用在小型的網站,或者是小型的桌面型應用程式,它也可以和 SQL Server 整合,作為資料庫複製(Replication)的訂閱端。
功能与限制
[
编辑
]
SQL Server Express提供了付費的完整版本的多項功能[3],然而其技術限制使之無法適用於大規模的部署。這兩個版本的差異在於:
- 資料庫的大小限制:对SQL Server 2008 R2 Express、SQL Server 2012 Express、SQL Server 2014 Express、SQL Server 2016 Express、SQL Server 2019 Express单个資料庫的大小限制最大為 10 GB[4];而在較早期的SQL Server 2005 Express 和SQL Server 2008 Express 上,单个資料庫的大小限制最大為4 GB。即使如此,亦已比最初MSDE版本的2 GB要多。而且這個大小的限制只有在資料檔案上,交易記錄檔則不受此限。
- 沒有 SQL Agent服务[5][6]:若要做排程服務必須自己寫程式。
- 硬體方面的人為限制:
- 允許在多核心的電腦上使用,但只會使用一個CPU核心來運算[7]。這在具有多核心CPU的電腦上會造成浪費。MSDE 最高可以支援二顆 CPU核心。
- 可使用的記憶體量最高只有 1GB。而全功能标准版至少使用4GB内存。
MSDE有一个并发工作负债监控器并限制在少量用户并发访问,SQL Server Express没有这个并发限制[8]。
SQL Server Express包括了几个数据库管理的图形用户界面工具:
MSDE 沒有管理介面
SQL Server Express不包括全功能标准版的几个工具:
SQL Server Express的各版本是可以远程连接的,但需要用户做一定的配置。
過往版本
[
编辑
]
Microsoft SQL Server Express Version History
版本號碼
發行日期
主流支援完結日期
延伸支援完結日期
支援作業系統
SQL Server 2005 Express Edition
2005-11-07[10]
2011-04-12[11]
2016-04-12[11]
Windows 2000 Service Pack 4, Windows XP Service Pack 2, Windows Server 2003 Service Pack, Windows 7 Service Pack 1 (only SQL Server 2005 Express Edition SP4)[12]
SQL Server 2008 Express
2009-02-08[13]
2014-07-08[14]
2019-07-09[14]
Windows XP Service Pack 2, Windows XP Service Pack 3, Windows Vista, Windows Vista Service Pack 1, Windows Server 2003 Service Pack 2, Windows Server 2008[15]
SQL Server 2008 R2 Express
2010-04-16[16]
2014-07-08[17]
2019-07-09[17]
Windows XP, Windows Vista, Windows 7, Windows 8, Windows Server 2003, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2[18]
SQL Server 2012 Express
2012-05-14[19]
2017-07-11[20]
2022-07-12[20]
Windows Vista Service Pack 2, Windows 7, Windows 7 Service Pack 1, Windows 8, Windows 8.1, Windows Server 2008,[21] Windows Server 2008 R2, Windows Server 2008 R2 SP1, Windows Server 2012, Windows Server 2012 R2[22]
SQL Server 2014 Express
2014-04-01[23]
2019-07-09[24]
2024-07-09[24]
Windows 7 Service Pack 1, Windows 8, Windows 8.1, Windows 10, Windows Server 2008 SP2,[25] Windows Server 2008 R2 SP1, Windows Server 2012, Windows Server 2012 R2[26]
SQL Server 2016 Express
2016-06-01[27]
2021-07-13[28]
2026-07-14[28]
Windows 8, Windows 8.1, Windows 10, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016
SQL Server 2017 Express
2017-09-29[29]
2022-10-11[30]
2027-10-12[30]
Windows 8, Windows 8.1, Windows 10, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016*,
Red Hat Enterprise Linux 7.3 or 7.4, SUSE Enterprise Linux Server v12 SP2, Ubuntu 16.04LTS,
Docker Engine 1.8+ (on Windows, Mac, or Linux)
- 並不包括 Windows Server 2016 “Essentials” Edition
安裝版本
[
编辑
]
SQL Server Express 2017 或 2019,SQL Server Express有三款安裝版本:
- 標準的SQL Server Express,或称Express Core: SQL Server Engine only
- 高級版本(with Advanced Services),或称Express Advanced:还包括有Reporting services及全文檢索(Full Text Service),在舊版本均包括在標準安裝版本內。
- LocalDB:比標準版本更輕便的數據庫。具有全部的编程特性,但在用户模式下运行。只能在本地连接数据库。从SQL Server 2012版开始有了LocalDB。在Visual Studio 2019安装中,带有SQL Server Express 2016 LocalDB
資料庫管理工具
[
编辑
]
到服务器的连接
[
编辑
]
SQL Server包括Express版本,有3种到数据库服务器的连接(connection)方式:
- 共享内存:仅限于本地访问
- 命名管道:
- TCP/IP通信:
其中后两种需要在SQL Server Configuration Manager软件中,SQL Server Network Configuration下的“Protocols for SQLEXPRESS”,把Nameed Pipes与TCP/IP手工置为Enable。对于第三种,还需要在上述的配置时,在TCP/IP Properties的IP Addresses中把IPALL的“TCP Port”设为1433.
工具
[
编辑
]
- SQL Server Management Studio Express,可用來管理 SQL Server Express 實體。
- SQL Server Reporting Service Express,簡易版的 SQL Server Reporting Service。
- SQL Server Configuration Manager,可用來管理 SQL Server Express 的連線組態與服務。
- Business Intelligence Development Studio Express,可用來開發 Reporting Service Express 的報表。
- 命令行工具sqlcmd,如下:
$sqlcmd -S thisisaserver.com -U username Password: 1> SELECT name FROM SYSOBJECTS WHERE xtype = 'U' 2> GO name -------- EquitySecMstr (1 rows affected) 1>
LocalDB
[
编辑
]
LocalDB的全称是Microsoft SQL Server Express LocalDB。
安装
[
编辑
]
从Microsoft下载的SqlLocalDB.msi约54MB。也可以使用Microsoft SQL Server Express的安装程序,在第一页选择第3项“Download Media”,随后在下一页选择第3项“localDB(54MB)”即可下载。
使用
[
编辑
]
命令行工具SqlLocalDB,用于创建和管理LocalDB的实例[32]。
C
:\>
SqlLocalDb
info
#输出默认实例名字:MSSQLLocalDB
#创建一个实例(相当于一个server):
C
:\>
SqlLocalDb
create
"MyInstance"
#输出:LocalDB instance "MyInstance" created with version 15.0.2000.5.
C
:\>
sqllocaldb
info
"MyInstance"
#输出:
#Name: MyInstance
#Version: 15.0.2000.5
#Shared name:
#Owner: MyDomainName\MyUserName
#Auto-create: No
#State: Stopped
#Last start time: 2022/3/9 19:04:41
#Instance pipe name:
C
:\>
sqllocaldb
start
"MyInstance"
#输出: LocalDB instance "MyInstance" started.
#类似的,可以有:
#C:\> SqlLocalDb stop "MyInstance"
#C:\> SqlLocalDb delete "MyInstance"
系统数据库文件存储于本地AppData路径下,通常为C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\
。
LocalDB 是在Windows当前用户的用户进程中运行。而SQL Server Express是作为Windows Service运行。所以,LocalDB的数据存储是Windows用户私有的。在Visual Studio启动IIS Express也是当前用户的用户进程执行,所以可以访问LocalDB。[33]
把私有的数据存储变为共享实例。例如,原存储为“(localdb)\v11.0”:
sqllocaldb share v11.0 IIS_DB
现在为公共名字IIS_DB。该电脑所有用户均可以用“(localdb)\.\IIS_DB”访问该实例。注意,名字中的前缀“\.”表示这是共享实例名字。即其保存的文件的路径不会放在特定用户的documents目录中。连接字符串可为:
Data Source=(localdb)\.\IIS_DB;Initial Catalog=OldFashionedDB;Integrated Security=True
在Visual Studio中通过 SQL Server Object Explorer 连接上述localDB实例,在query窗口执行下述脚本可以授予“”账号数据实例的管理员权限:
create login [IIS APPPOOL\ASP.NET v4.0] from windows; exec sp_addsrvrolemember N'IIS APPPOOL\ASP.NET v4.0', sysadmin
SQLCMD使用的连接字符串
[
编辑
]
C:\>sqlcmd -S "(localdb)\MyInstance"
就可以在交互环境下执行查询:
SELECT
@@
VERSION
;
GO
CREATE
DATABASE
blat
;
GO
USE
blat
;
GO
CREATE
TABLE
dbo
.
splunge
(
Mort
int
);
GO
INSERT
dbo
.
Splune
(
Mort
)
VALUES
(
1
);
SELECT
*
FROM
dbo
.
splunge
;
GO
Microsoft SQL Server Management Studio连接LocalDB
[
编辑
]
Server Name使用(localdb)\MyInstance
编程接口
[
编辑
]
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Data
;
using
System.Data.SqlClient
;
namespace
Connection
{
class
Program
{
static
void
Main
(
string
[]
args
)
{
//构造连接字符串
SqlConnectionStringBuilder
connStr
=
new
SqlConnectionStringBuilder
();
connStr
.
DataSource
=
@".\SQLEXPRESS"
;
connStr
.
InitialCatalog
=
"master"
;
connStr
.
IntegratedSecurity
=
true
;
SqlConnection
conn
=
new
SqlConnection
();
//创建连接对象
conn
.
ConnectionString
=
connStr
.
ConnectionString
;
//设置连接字符串
conn
.
Open
();
//打开连接
if
(
conn
.
State
==
ConnectionState
.
Open
)
{
Console
.
WriteLine
(
"Database is linked."
);
Console
.
WriteLine
(
"\nDataSource:{0}"
,
conn
.
DataSource
);
Console
.
WriteLine
(
"Database:{0}"
,
conn
.
Database
);
Console
.
WriteLine
(
"ConnectionTimeOut:{0}"
,
conn
.
ConnectionTimeout
);
}
conn
.
Close
();
//关闭连接
conn
.
Dispose
();
//释放资源
if
(
conn
.
State
==
ConnectionState
.
Closed
)
{
Console
.
WriteLine
(
"\nDatabase is closed."
);
}
Console
.
Read
();
}
}
}
对于较高版本,如SQL Server Express大于版本2010的,可以通过在Visual Studio,依次点击“视图”(菜单栏)->“服务器资源管理器(Server Explorer)”,进入“服务器资源管理器”窗口。右键点击“数据连接(Data Connection)”,在弹出的上下文菜单中选择“添加连接(Add Connection)”,在弹出的窗口中将Data Source选择为Microsoft SQL Server,“服务器名”设置为(LocalDb)\MSSQLLocalDB,附加服务器文件为具体的mdf文件并点击确定。此时,通过Microsoft SQL Server(SQL Client)方式访问指定的mdf文件。
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Data
;
using
System.Data.SqlClient
;
namespace
ConsoleApp
{
class
Program
{
static
void
Main
(
string
[]
args
)
{
System
.
Data
.
SqlClient
.
SqlConnection
connection
=
new
System
.
Data
.
SqlClient
.
SqlConnection
();
connection
.
ConnectionString
=
@"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;"
;
//@"Data Source=(localdb)\MyInstance;InitialCatalog =blat;Integrated Security = True;Connect Timeout = 30;";
connection
.
Open
();
if
(
connection
.
State
==
ConnectionState
.
Open
)
{
Console
.
WriteLine
(
"Database is linked."
);
Console
.
WriteLine
(
"DataSource:{0}"
,
connection
.
DataSource
);
Console
.
WriteLine
(
"Database:{0}"
,
connection
.
Database
);
Console
.
WriteLine
(
"ConnectionTimeOut:{0}"
,
connection
.
ConnectionTimeout
);
}
SqlDataAdapter
adapter
=
new
SqlDataAdapter
(
"SELECT * FROM Employees"
,
connection
);
DataTable
table
=
new
DataTable
();
adapter
.
Fill
(
table
);
var
result
=
(
from
row
in
table
.
Rows
.
Cast
<
DataRow
>()
select
new
{
ID
=
(
int
)
row
[
0
],
LastName
=
(
string
)
row
[
1
],
FirstName
=
(
string
)
row
[
2
]
});
result
.
ToList
().
ForEach
(
x
=>
Console
.
WriteLine
(
$"{x.ID},{x.LastName},{x.FirstName}"
));
}
}
}
对于较低版本,如SQL Server Express 2005的mdf文件,可以直接用如下connection string打开访问(可能需要administrator权限):
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Data
;
using
System.Data.SqlClient
;
namespace
ConsoleApp
{
class
Program
{
static
void
Main
(
string
[]
args
)
{
System
.
Data
.
SqlClient
.
SqlConnection
connection
=
new
System
.
Data
.
SqlClient
.
SqlConnection
();
connection
.
ConnectionString
=
@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\apps\Documents\NORTHWND.mdf;Integrated Security = True;Connect Timeout = 30;"
;
connection
.
Open
();
if
(
connection
.
State
==
ConnectionState
.
Open
)
{
Console
.
WriteLine
(
"Database is linked."
);
Console
.
WriteLine
(
"DataSource:{0}"
,
connection
.
DataSource
);
Console
.
WriteLine
(
"Database:{0}"
,
connection
.
Database
);
Console
.
WriteLine
(
"ConnectionTimeOut:{0}"
,
connection
.
ConnectionTimeout
);
}
}
}
}
Python
[
编辑
]
需要安装pymssql包
from
sqlalchemy
import
create_engine
import
pandas
as
pd
user
=
'username'
pwd
=
'password'
url
=
'thisisaserver.com'
port
=
1433
db
=
'database'
engine
=
create_engine
(
f
'mssql+pymssql://
{
user
}
:
{
pwd
}
@
{
url
}
:
{
port
}
/
{
db
}
'
)
sql_stmt
=
"SELECT name FROM SYSOBJECTS WHERE xtype = 'U'"
pd
.
read_sql_query
(
sql_stmt
,
engine
)
參考資料
[
编辑
]
參看
[
编辑
]
外部連結
[
编辑
]