SQL SERVER2005发送邮件

 

SQL SERVER2005发送邮件  

2010-04-26 20:44:00|  分类: 编程 Database |  标签: |字号 订阅

key:sql server 2005 邮件
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Database Mail XPs', 1;
GO
RECONFIGURE
GO

一:
http://sqlserver2005.itpub.net/post/22359/210413

早在sql server 2000中可以通过sql mail给指定的邮件地址发邮件。SQL SERVER 2005中我们仍外可以用sql server2000中的SQLMAIl功能

发送邮件,但SQL SERVER2005中又提供一个新数据库邮件功能也可以发送和接收邮件。这里以数据库邮件为例进行说明。


为了安全起见,微软将数 据中的一些可能带来安全隐患的一些功能组件禁用掉了,数据库邮件和SQL MAIL都在被禁用之列,因些第一步是通

过外围配置器将这两个功 能组件启用。启用之后进入SQL SERVER2005管理界面(SQL SERVER 2005 MANGEMENT STUDIO)找到管理节点,就可以找

到数据库邮件子项,在右键菜单中会有一个"配置数据库邮件"菜单,按照步骤先建立配置文件,然后创建 账号,保存退出。在右键菜单的第二

项"发送测试邮件",在弹出的窗口中输入接收的邮件地址,发送就可以。


我们可以在 SQL中通过刚才的配置来收发邮件。在sql server 2000中,通过xp_snedmail以及以xp_开头的存储过程来发送和管理邮件。数据

邮 件用的是在msdb库sp开头的一些存储过程来收发和管理邮件。

sp_send_dbmail发送电子邮件年,用法如下:

sp_send_dbmail [ [ @profile_name = ] 'profile_name' ]
[ , [ @recipients = ] 'recipients [ ; ...n ]' ]
[ , [ @copy_recipients = ] 'copy_recipient [ ; ...n ]' ]
[ , [ @blind_copy_recipients = ] 'blind_copy_recipient [ ; ...n ]' ]
[ , [ @subject = ] 'subject' ]
[ , [ @body = ] 'body' ]
[ , [ @body_format = ] 'body_format' ]
[ , [ @importance = ] 'importance' ]
[ , [ @sensitivity = ] 'sensitivity' ]
[ , [ @file_attachments = ] 'attachment [ ; ...n ]' ]
[ , [ @query = ] 'query' ]
[ , [ @execute_query_database = ] 'execute_query_database' ]
[ , [ @attach_query_result_as_file = ] attach_query_result_as_file ]
[ , [ @query_attachment_filename = ] query_attachment_filename ]
[ , [ @query_result_header = ] query_result_header ]
[ , [ @query_result_width = ] query_result_width ]
[ , [ @query_result_separator = ] 'query_result_separator' ]
[ , [ @exclude_query_output = ] exclude_query_output ]
[ , [ @append_query_error = ] append_query_error ]
[ , [ @query_no_truncate = ] query_no_truncate ]
[ , [ @mailitem_id = ] mailitem_id ] [ OUTPUT ]

sp_send_mail 返回值1表示成功,0表示失败

sp_send_dbmail参数解释

[ @profile_name = ] 'profile_name'
发送邮件的配置文件的名称。profile_name 的类型为 sysname,默认值为 NULL。profile_name 必须是现有数据库邮件配置文件的名称。如果

未指定 profile_name,则 sp_send_dbmail 使用当前用户的默认专用配置文件。如果该用户没有默认专用配置文件,sp_send_dbmail 会使用

msdb 数据库的默认公共配置文件。如果用户没有默认的专用配置文件,而且数据库也没有默认的公共配置文件,则必须指定 @profile_name。

[ @recipients = ] 'recipients'
要向其发送邮件的电子邮件地址列表,以分号分隔。收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定

@recipients、@copy_recipients 或 @blind_copy_recipients 中的一个,否则 sp_send_dbmail 将返回错误。

[ @copy_recipients = ] 'copy_recipients'
要向其抄送邮件的电子邮件地址列表,以分号分隔。抄送件收件人列 表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少指定

@recipients、 @copy_recipients 或 @blind_copy_recipients 中的一个,否则 sp_send_dbmail 将返回错误。

[ @blind_copy_recipients = ] 'blind_copy_recipients'
要向其密件抄送邮件的电子邮件地址列 表,以分号分隔。密件副本收件人列表的类型为 varchar(max)。虽然此参数是可选参数,但是必须至少

指定 @recipients、@copy_recipients 或 @blind_copy_recipients 中的一个,否则 sp_send_dbmail 将返回错误。

[ @subject = ] 'subject'
电子邮件的主题。主题的类型为 nvarchar(255)。如果未指定主题,则默认为 SQL Server Message。

[ @body = ] 'body'
电 子邮件的正文。邮件正文的类型为 nvarchar(max),默认值为 NULL。

[ @body_format = ] 'body_format'
邮件正文的格式。该参数的类型为 varchar(20),默认值为 NULL。如果已指定,则待发邮件的标头设置会指示邮件正文具有指定格式。该参数

可能包含下列值之一(默认为 TEXT):

TEXT

HTML

[ @importance = ] 'importance'
邮件的重要性。该参数的类型为 varchar(6)。该参数可能包含下列值之一(默认值为 Normal):

Low

Normal

High

[ @sensitivity = ] 'sensitivity'
邮件的敏感度。该参数的类型为 varchar(12)。该参数可能包含下列值之一(默认值为 Normal):

Normal

Personal

Private

Confidential

[ @file_attachments = ] 'file_attachments',
电子邮件附件的文件名列表,以分号分隔。必须使用绝对 路径指定列表中的文件。附件列表的类型为 nvarchar(max)。

[ @query = ] 'query'
要执行的查询。 查询结果可以作为文件附加,或包含在电子邮件的正文中。查询的类型为 nvarchar(max),并且可以包含任何有效的

Transact- SQL。请注意,查询在单独的会话中执行,所以调用 sp_send_dbmail 的脚本中的局部变量不可用于查询。
(注意:发邮件的用户必须 有执行@query的权限)

[ @execute_query_database = ] 'execute_query_database'
存储过程在其中运行查询的数据库上下文。该参数的类型为 sysname,默认为当前数据库。只有在指定 @query 时,此参数才适用。

[ @attach_query_result_as_file = ] attach_query_result_as_file
指定查询结果集是 否作为附件返回。attach_query_result_as_file 的数据类型为 bit,默认值为 0。

如果该值为 0,查询结果包含在电子邮件的正文中,在 @body 参数的内容之后。如果该值为 1,结果作为附件返回。只有在指定 @query 时,

此 参数才适用。

[ @query_attachment_filename = ] query_attachment_filename
指 定查询结果集附件使用的文件名。query_attachment_filename 的数据类型为 nvarchar(255),默认值为 NULL。如果 attach_query_result

为 0,则忽略此参数。如果 attach_query_result 为 1 且此参数为 NULL,则数据库邮件会创建任意文件名。

[ @query_result_header = ] query_result_header
指定查询结果是否包含列标题。query_result_header 值的数据类型为 bit。如果该值为 1,则查询结果包含列标题。如果该值为 0,则查询

结果不包含列标题。该参数的默认值为 1。只有在指定 @query 时,此参数才适用。

[ @query_result_width = ] query_result_width
用 于设置查询结果的格式的线条宽度(字符)。query_result_width 的数据类型为 int,默认值为 256。提供的值必须介于 10 到 32767 之

间。只有在指定 @query 时,此参数才适用。

[ @query_result_separator = ] 'query_result_separator'
用于分隔查询输出中的列的字符。分隔符的类型为 char(1)。默认为 ' '(空格)。

[ @exclude_query_output = ] exclude_query_output
指定是否使用电 子邮件返回查询执行的输出。exclude_query_output 的数据类型为 bit,默认值为 0。当此参数为 0 时,sp_send_dbmail 存

储过程的执行将在控制台上打印作为查询执行结果而返回的消息。当此参数为 1 时,sp_send_dbmail 存储过程的执行不会在控制台上打印任

何查询执行消息。

[ @append_query_error = ] append_query_error
指定是否在 @query 参数中指定的查询返回错误时发送电子邮件。exclude_query_output 的数据类型为 bit,默认值为 0。如果该参数的值为

1, 数据库邮件会发送电子邮件,并将查询错误消息包含在电子邮件的正文中。如果该参数的值为 0,数据库邮件不发送电子邮件,

sp_send_dbmail 在结束时会返回代码 1,表示失败。

[ @query_no_truncate = ] query_no_truncate
指 定是否使用可避免截断大型可变长度数据类型(varchar(max)、nvarchar(max)、varbinary(max)、xml、text、 ntext、image 以及用户定义

数据类型)的选项执行查询。设置该选项后,查询结果将不包含列标题。 query_no_truncate 值的数据类型为 bit。当该值为 0 或未指定时,

查询中的列截断为 256 个字符。当该此值为 1 时,不截断查询中的列。该参数的默认值为 0。

注意:
与大量数据一起使用时,query_no_truncate 选项会占用其他资源,并可降低服务器的性能。


[ @mailitem_id = ] mailitem_id [ OUTPUT ]
可 选输出参数将返回消息的 mailitem_id。mailitem_id 的数据类型为 int。


以sp开头的一 些与数据邮件相关的过程存储在msdb数据库中,msdb 数据库中的 DatabaseMailUser 数据库角色的所有成员对sp_send_dbmail

都有执行权限。不过,如果发送邮件的用户不具有使用该请求的配置文件的权 限,sp_send_dbmail 会返回错误且不发送该邮件。

配置数据库邮件

1、在外围配置器中启用数据库邮件
   
  2、在数据库管理节点下配置数据库邮件(先建立配置文件,然后创建账号)

3、建立账号,使他成为msdb中 DatabaseMailUser 数据库角色所成员

4、修改据库管理节点下配置数据库邮件配置设置,使刚建立的账号有权使用刚才建立的 配置文件

  5、以刚建立的用户登陆,测试


 下面是我的测试SQL:


use mdsb

--基本的发送语句
EXEC sp_send_dbmail @profile_name = 'xx_adam',
@recipients = 'xx_adam@263.com',
@subject = '我数据库邮件测试成功了!',
@body = 'hello,world!'

--包含查询的
EXEC sp_send_dbmail @profile_name = 'xx_adam',
@recipients = 'xx_adam@263.com',
@subject = '查询结果',
@query = 'select * from demo.dbo.orders',

--包含附 件
EXEC sp_send_dbmail @profile_name = 'xx_adam',
@recipients = 'xx_adam@263.com',
@subject = '包含附件',
@body = '有附件,请查收',
@file_attachments = 'c:a.txt'

-- 将查询作为附件
EXEC sp_send_dbmail @profile_name = 'xx_adam',
@recipients = 'xx_adam@263.com',
@subject = '查询结果',
@body = '查询结果在附件中',
@query = 'select * from demo.dbo.orders',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'a.txt'



二:http://www.sqlstudy.com/sql_article.php?id=2008072403

SQL Server:在 SQL Server 2005 中配置数据库邮件。

对于真正的 DBA 来说,数据库邮件是必不可少的。 例如,数据库发生了警报(alert), DBA 希望得到邮件通知,以便即时排除故障。 或者是监控数据库作业(SQL Server Job)的运行状况,当检查到失败的作业时, 就发送数据库邮件报告给 DBA。

在 SQL Server 2000 中 配置 “SQL Mail”,需要安装 Outlook,配置过程比较麻烦。 在 SQL Server 2005 中配置 “Database Mail” 就相对容易多了。 主要是理清思路。

SQL Server 并没有内置邮件服务器(Mail Server), 它跟我们发送邮件一样,需要用户名和密码通过 SMTP(Simple Message Transfer Protocol) 去连接邮件服务器。我们想让 SQL Server 来发送邮件,首先要告诉它用户名称,密码, 服务器地址,网络传送协议,邮件服务器的端口。。。等信息。这是通过 SQL Server 系统 存储过程 sysmail_add_account_sp 来实现的。

exec sysmail_add_account_sp

这样,在 SQL Server 2005 中就添加了一个发送邮件的帐户。 道理上讲,有了这个邮件帐户,SQL Server 就可以发送邮件了。 如:

sp_send_dbmail @account_name = 'mail_account'

但是,SQL Server 考虑的更周全。试想:如果这个邮件帐户发生故障 (比如:用户密码过期,或者邮件服务器宕机)那岂不是发送不了邮件了? 为了应对这种情况,SQL Server 2005 引入了 mail profile 这个东东。 一个 profile 中可以包含多个 account (邮件帐户),这样,SQL Server 发邮件的时候会依次尝试 profile 中的多个邮件帐户,如果发送成功,则退出, 否则,利用下一个邮件帐户发送邮件。其中,添加 profile 和 在 account 和 profile 建立映射是通过下面两个系统存储过程实现的:

sysmail_add_profile_sp
sysmail_add_profileaccount_sp

这时候,SQL Server 发送邮件,就采用下面的方法了:

sp_send_dbmail @profile_name = 'profile_name'

下面是具体的配置邮件步骤

在 sa 系统帐户下运行。

1. 启用 SQL Server 2005 邮件功能。

use master
go

exec sp_configure 'show advanced options',1
go

reconfigure
go

exec sp_configure 'Database mail XPs',1
go

reconfigure
go

2. 在 SQL Server 2005 中添加邮件帐户(account)

exec msdb..sysmail_add_account_sp
        @account_name            = 'p.c.w.l'                 -- 邮件帐户名称(SQL Server 使用)
       ,@email_address           = 'webmaster@sqlstudy.com'  -- 发件人邮件地址
       ,@display_name            = null                      -- 发件人姓名
       ,@replyto_address         = null
       ,@description             = null
       ,@mailserver_name         = '58.215.64.159'           -- 邮件服务器地址
       ,@mailserver_type         = 'SMTP'                    -- 邮件协议(SQL 2005 只支持 SMTP)
       ,@port                    = 25                        -- 邮件服务器端口
       ,@username                = 'webmaster@sqlstudy.com'  -- 用户名
       ,@password                = 'xxxxxxxxx'               -- 密码
       ,@use_default_credentials = 0
       ,@enable_ssl              = 0
       ,@account_id              = null

3. 在 SQL Server 2005 中添加 profile

exec msdb..sysmail_add_profile_sp @profile_name = 'dba_profile'      -- profile 名称 
                                 ,@description  = 'dba mail profile' -- profile 描述 
                                 ,@profile_id   = null

4. 在 SQL Server 2005 中映射 account 和 profile

exec msdb..sysmail_add_profileaccount_sp  @profile_name    = 'dba_profile' -- profile 名称 
                                         ,@account_name    = 'p.c.w.l'     -- account 名称 
                                         ,@sequence_number = 1             -- account 在 profile 中顺序 

5. 利用 SQL Server 2005 Database Mail 功能发送邮件。

exec msdb..sp_send_dbmail @profile_name =  'dba_profile'               -- profile 名称 
                         ,@recipients   =  'sqlstudy@163.com'          -- 收件人邮箱 
                         ,@subject      =  'SQL Server 2005 Mail Test' -- 邮件标题 
                         ,@body         =  'Hello Mail!'               -- 邮件内容 
                         ,@body_format  =  'TEXT'                      -- 邮件格式 

6. 查看邮件发送情况:

use msdb
go

select * from sysmail_allitems
select * from sysmail_mailitems
select * from sysmail_event_log

如果不是以 sa 帐户发送邮件,则可能会出现错误:

Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1
EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'.

这是因为,当前 SQL Server 登陆帐户(login),在 msdb 数据库中没有发送数据库邮件的权限, 需要加入 msdb 数据库用户,并通过加入 sp_addrolemember 角色赋予权限。假设该SQL Server 登陆帐户 名字为 “dba”

use msdb
go

create user dba for login dba
go

exec dbo.sp_addrolemember @rolename   = 'DatabaseMailUserRole',
                          @membername = 'dba'
go

此时,再次发送数据库邮件,仍可能有错误:

Msg 14607, Level 16, State 1, Procedure sp_send_dbmail, Line 119
profile name is not valid

虽然,数据库用户 “dba” 已经在 msdb 中拥有发送邮件的权限了, 但这还不够,他还需要有使用 profile:“dba_profile” 的权限。

use msdb
go

exec sysmail_add_principalprofile_sp  @principal_name = 'dba'
                                     ,@profile_name   = 'dba_profile'
                                     ,@is_default     = 1

从上面的参数 @is_default=1 可以看出,一个数据库用户可以在多个 mail profile 拥有发送权限。

现在,可以利用 SQL Server 2005 发送数据库邮件了吧。如仍有问题,请留言。




How to configure SQL 2005 Database Mail,如何配置 SQL

http://kb.cnblogs.com/a/1415082/

What is SQL 2005 Database Mail?

Before Microsoft has introduced SQL Server 2005 to database administrators and database developers, administrators and programmers were suffering in their applications just because of the lack of SMTP email support in their SQL Server databases when a need or a requirement occurs to send emails from database.

The requirement to send email may occur when a job fails, or in the co de you want to send email in your stored procedures or in the sql codes of triggers in certain situations.

Now, Microsoft SQL Server 2005 supports SMTP with a new feature named Database Mail so SQL database administrators do not need to install MS Outlook, or MAPI or any other third party mailing tools to send emails from within the SQL Server database.

Database Mail is just on e of the enhancements we were introduced with the new version of SQL Server, SQL Server 2005.

Now we can continue to configuration steps and processes of SQL Server 2005 Database Mail.


window.google_render_ad();



Configuration Steps of SQL Server 2005 Database Mail

database-mail

 

Right click on the Database Mail in order to display the context menu and select the Configure Database Mail menu item.

 

configure-database-mail

Database Mail Configuration Wizard runs and displays a welcome message.

Database Mail Configuration Wizard is used to enable Database Mail, manage security, and configure Database Mail system parameters

configuration-task

When you click Next

database-mail-feature

TITLE: Microsoft SQL Server Management Studio
------------------------------

The Database Mail feature is not available. Would you like to enable this feature?
 

You can enable the Database Mail feature by pressing the "Yes" button or by running the below sql script

USE Master
GO
sp_configure 'show advanced options', 1
GO
reconfigure with override
GO
sp_configure 'Database Mail XPs', 1
GO
reconfigure
GO
sp_configure 'show advanced options', 0
GO
 

 

Configuration option 'show advanced options' changed from 1 to 1. Run the RECONFIGURE statement to install.
Configuration option 'Database Mail XPs' changed from 0 to 1. Run the RECONFIGURE statement to install.
Configuration option 'show advanced options' changed from 1 to 0. Run the RECONFIGURE statement to install.
 

If you run sp_configure command on the master database, you will see that the run_value for the "Database Mail XPs" is changed from 0 to 1 to enable this feature.

sp_configure

sp_configure

First step is creating a new profile. Enter a profile name and a description for the new profile. Then click the Add... button to complete the task.

database-mail-pro<wbr>file

 

database-mail-account

 

 

smtp-accounts

 

Click Next button

 

Use this page to configure a public profile.

Profiles are either public or private. A private profile is accessible only to specific users or roles. A public profile allows any user or role with access to the mail host database (msdb) to send e-mail using that profile.

A profile may be a default profile. In this case, users or roles can send e-mail using the profile without explicitly specifying the profile. If the user or role sending the e-mail message has a default private profile, Database Mail uses that profile. If the user or role has no default private profile, sp_send_dbmail uses the default public profile for the msdb database. If there is no default private profile for the user or role and no default public profile for the database, sp_send_dbmail returns an error. Only one profile can be marked as the default profile
 

To send Database Mail users must also be a added to the DatabaseMailUsersRole database role in the msdb database using Management Studio or sp_addrolemember.

 

mail-pro<wbr>file-security

Use this page to configure a private profile.

Profiles are either public or private. A private profile is accessible only to specific users or roles. A public profile allows any user or role with access to the mail host database (msdb) to send e-mail using that profile.

A profile may be a default profile. In this case, users or roles can send e-mail using the profile without explicitly specifying the profile. If the user or role sending the e-mail message has a default private profile, Database Mail uses that profile. If the user or role has no default private profile, sp_send_dbmail uses the default public profile for the msdb database. If there is no default private profile for the user or role and no default public profile for the database, sp_send_dbmail returns an error.

Note:
To send Database Mail users must also be a added to the DatabaseMailUsersRole database role in the msdb database using Management Studio or sp_addrolemember.
 

configure-system-parameters

Use this page to specify Database Mail system parameters. View the system parameters and the current value of each parameter. Select a parameter to view a short description in the information pane.

Options
Account Retry Attempts
The number of times that the external mail process attempts to send the e-mail message using each account in the specified profile.

Account Retry Delay (seconds)
The amount of time, in seconds, for the external mail process to wait after it tries to deliver a message using all accounts in the profile before it attempts all accounts again.

Maximum File Size (Bytes)
The maximum size of an attachment, in bytes.

Prohibited Attachment File Extensions
A comma-separated list of extensions which cannot be sent as an attachment to an e-mail message. Click the browse button (...) to add additional extensions.

Database Mail Executable Minimum Lifetime (seconds)
The minimum amount of time, in seconds, that the external mail process remains active. The process remains active as long as there are e-mails in the Database Mail queue. This parameter specifies the time the process remains active if there are no messages to process.

Logging level
Specify which messages are recorded in the Database Mail log. Possible values are:

Normal - logs only errors
Extended - logs errors, warnings, and informational messages
Verbose - logs errors, warnings, informational messages, success messages, and additional internal messages. Use verbose logging for troubleshooting.

Default value is Extended.

Reset All
Select this option to reset the values on the page to the default values.

Additional Information about the Retry Options
The sequence number in the Database Mail profile determines the order in which Database Mail uses accounts in the profile. This is configured by using the Move Up and Move Down buttons when configuring a profile in the Database Mail Configuration Wizard. For a new e-mail message, Database Mail starts with the account that has the lowest sequence number. Should that account fail, Database Mail uses the account with the next highest sequence number, and so on until either Database Mail sends the message successfully, or the account with the highest sequence number fails. If the account with the highest sequence number fails, the Database Mail pauses attempts to send the mail for the amount of time configured in the Account Retry Delay parameter of sysmail_configure_sp, then starts the process of attempting to send the mail again, starting with the lowest sequence number. Use the Account Retry Attempts parameter of sysmail_configure_sp, to configure the number of times that the external mail process attempts to send the e-mail message using each account in the specified profile.

If more than one account exists with the same sequence number, Database Mail will only use one of those accounts for a given e-mail message. In this case, Database Mail makes no guarantees as to which of the accounts is used for that sequence number or that the same account is used from message to message.
 

dbmail-wizard

 

 

process

 

Run the below sql command from a query editor.

EXEC msdb.dbo.sp_send_dbmail @profile_name = 'DBMailProfile', @recipients = 'recipient@mail.com', @body = 'This is a sample email sent via Database Mail from a t-sql code script', @subject = 'Subject: Database Mail'

"Mail queued." response will be returned.

 

database-mail-menu-items

 

 

If you run the sp_send_dbmail stored procedure before enabling the Database Mail feature, you will get a response similar to below.

Msg 15281, Level 16, State 1, Procedure sp_send_dbmail, Line 0
SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.
 

In such a case use sp_configure to set Database Mail XPs properties or enable the Database Mail feature using the Configure Database Mail Wizard.

If you enabled the database mail but getting the below error message, you might be using a wrong profile name in the sp_send_dbmail command.

Msg 14607, Level 16, State 1, Procedure sysmail_verify_profile_sp, Line 42
profile name is not valid

In this case, run the Configure Database Mail Wizard. Then select "Manage Database Mail accounts and profiles" option in the Select Configuration Task

manage-database-mail-accounts-and-profiles

Then select,

view-change-delete-existing-account

In the manage existing account screen, choose from existing accounts to manage from the dropdown list.






MS SQL Server 2005 在建立与服务器的连接时出错。

http://topic.csdn.net/u/20090621/08/40e0864c-5ae7-4c14-94e6-431353b8d724.html
SQL co de
     
     
彻底解决SQL SERVER 2005无法远程连接的问题 一、为 SQL 启用远程连接 1 . 单击“开始”,依次指向“程序”、“Microsoft SQL Server 2005 ”和“配置工具”,然后单击“SQL Server 外围应用配置器”。 2 . 在“SQL Server 2005 外围应用配置器”页上,单击“服务和连接的外围应用配置器”。 3 . 在“服务和连接的外围应用配置器”页上,展开“数据库引擎”,依次单击“远程连接”和“本地连接和远程连接”,单击适用于您的环境的相应协议,然后单击“应用”。 注意:请在接收到以下消息时单击“确定”: 直到重新启动数据库引擎服务后,对连接设置所做的更改才会生效。 4 . 在“服务和连接的外围应用配置器”页上,展开“数据库引擎”,依次单击“服务”和“停止”,等待 MSSQLSERVER 服务停止,然后单击“启动”以重新启动 MSSQLSERVER 服务。 二、启用 SQL Server Browser 服务 1 . 单击“开始”,依次指向“程序”、“Microsoft SQL Server 2005 ”和“配置工具”,然后单击“SQL Server 外围应用配置器”。 2 . 在“SQL Server 2005 外围应用配置器”页上,单击“服务和连接的外围应用配置器”。 3 . 在“服务和连接的外围应用配置器”页上,单击“SQL Server Browser”,在“启动类型”中单击“自动”选项,然后单击“应用”。 注意:在单击“自动”选项后,每次启动 Microsoft Windows 时将自动启动 SQL Server Browser 服务。 4 . 单击“启动”,然后单击“确定”。 三、在 Windows 防火墙中为SQL Server 2005 创建例外 1 . 在 Windows 防火墙中,单击“例外”选项卡,然后单击“添加程序”。 2 . 在“添加程序”窗口中,单击“浏览”。 3 . 单击 C:\Program Files\Microsoft SQL Server\MSSQL. 1 \MSSQL\Binn\sqlservr.exe 可执行程序,单击“打开”,然后单击“确定”。 注意:上述路径可能会根据 SQL Server 2005 的安装位置而不同。 四、在 Windows 防火墙中为 SQL Server Browser 服务创建例外 1 . 在 Windows 防火墙中,单击“例外”选项卡,然后单击“添加程序”。 2 . 在“添加程序”窗口中,单击“浏览”。 3 . 单击 C:\Program Files\Microsoft SQL Server\ 90 \Shared\sqlbrowser.exe 可执行程序,单击“打开”,然后单击“确定”。 注意:上述路径可能会根据 SQL Server 2005 的安装位置而不同。 五、远程连接端口设置(很关键的一步,很多方法只讲了一到四步,却没有讲到第五步) 1 、在服务器上打开SQL Server Configuration Manager。选择SQL Server配置治理器 -> SQL Server 2005网络配置 -> MSSQLSERVER的协议 -> TCP / IP,在弹出对话框中选择IP地址 -> IPALL -> TCP端口,设置为可用端口。(如果默认的1433端口老是连接不上,你就设置为你确认已经打开的端口试试,如21端口等) 2 、在“服务和连接的外围应用配置器”页上,展开“数据库引擎”,依次单击“服务”和“停止”,等待 MSSQLSERVER 服务停止,然后单击“启动”以重新启动 MSSQLSERVER 服务。 六、测试连接 1 、打开SQL2005登陆界面,输入服务器IP和用户名及密码。 如: 服务器名称: 192.168 . 1.200 , 21 用户名:sa 密码:sa 点连接,结果连接成功。

     
     
     
     
SQL co de
         
         
QL Server 2005 远程连接 配置TCP / IP属性. Surface Area Configuration -- > Database Engine --> Remote Connections --->Using TCP/IT SQL Server 外围应用配置器—服务和连接外围配置— database englie—远程连接—启用(远程连接的TCP / IP和named pipes) SQL Server Configuration Manager—SQL2005网络配置—启用TCP / IP和named pipes -- --------------------------------------------------------------------------------------- 一般按如上即可. 其他说明见下: sqlserver2005(Express版),为了便于管理,你还需要去下一个manage管理器,微软的官方网站有 下,安装好manage管理器后,在程序中连接sqlserver2005,下面几点是要注意的。 1 . 开启sql2005远程连接功能,开启办法如下, 配置工具 -> sql server外围应用配置器 -> 服务和连接的外围应用配置器 -> 打开MSSQLSERVER节点下的Database Engine 节点,先择"远程连接",接下建议选择"同时使用TCP / IP和named pipes",确定后,重启数据库服务就可以了. 2 .登陆设置改为,Sql server and windows Authentication方式同时选中,具体设置如下: manage管理器 -> windows Authentication(第一次用windows方式进去), -> 对象资源管理器中选择你的数据服务器 -- 右键>属 > security > Sql server and windows Authentication方式同时选中. 3 :设置一个Sql server方式的用户名和密码,具体设置如下: manage管理器 -> windows Authentication > new query > sp_password null , ' sa123456 ' , ' sa ' 这样就设置了一个用户名为sa ,密码为:sa123456的用户,下次在登陆时,可以用Sql server方式, 用户名为sa ,密码为:sa123456的用户进数据库了. 4 : 做完上面三步后,这样写连接字符串就可以顺利进入数据库了, (server = .\\sqlexpress;uid = sa;pwd = sa123456; database = master"; 其它版本,做如下設置 程序—Microsoft SQL Server 2005 —配置工具—SQL Server 外围应用配置器—服务和连接的外围应用配置器— 1 、MSSQLserver(实例名)— DataBase Engine—远程连接—(设置选项) 在下方选项 2 、SQL Server Browser—服务(停止时只本机访问) 3 、程序—Microsoft SQL Server 2005 —配置工具—SQL Server Configuration Manager—网络连接协议 - 选择实例对应协议—启用(shard memory、Named Pipes、TCP\IP) 注: SQL Server 浏览器程序以服务的形式在服务器上运行。SQL Server 浏览器侦听对 Microsoft SQL Server 资源的传入请求, 并提供计算机上安装的 SQL Server 实例的相关信息。 SQL Server 浏览器可用于执行下列三种操作: 浏览可用服务器 连接到正确的服务器实例 连接到专用管理员连接 (DAC) 端点 SQL Server Browser 服务 (sqlbrowser) 为数据库引擎 和 SSAS 的每个实例提供实例名称和版本号。 SQL Server 浏览器随 Microsoft SQL Server 2005 一起安装,

         
         
         
         
SQL co de
              
              
打开sql server 2000的1433端口和进行远程连接设置. SQL code远程连接sql server 2000服务器的解决方案   一 看ping 服务器IP能否ping通。   这个实际上是看和远程sql server 2000服务器的物理连接是否存在。如果不行,请检查网络,查看配置,当然得确保远程 sql server 2000服务器的IP拼写正确。   二 在Dos或命令行下输入telnet 服务器IP 端口,看能否连通。   如telnet 202.114 . 100.100 1433   通常端口值是1433,因为1433是sql server 2000的对于Tcp / IP的默认侦听端口。如果有问题,通常这一步会出问题。通常的提示 是“……无法打开连接,连接失败"。   如果这一步有问题,应该检查以下选项。    1 检查远程服务器是否启动了sql server 2000服务。如果没有,则启动。    2 检查服务器端有没启用Tcp / IP协议,因为远程连接(通过因特网)需要靠这个协议。检查方法是,在服务器上打开 开始菜单 -> 程序 -> Microsoft SQL Server -> 服务器网络实用工具,看启用的协议里是否有tcp / ip协议,如果没有,则启用它。    3 检查服务器的tcp / ip端口是否配置为1433端口。仍然在服务器网络实用工具里查看启用协议里面的tcp / ip的属性,确保默认 端口为1433,并且隐藏服务器复选框没有勾上。   事实上,如果默认端口被修改,也是可以的,但是在客户端做telnet测试时,写服务器端口号时必须与服务器配置的端口号 保持一致。如果隐藏服务器复选框被勾选,则意味着客户端无法通过枚举服务器来看到这台服务器,起到了保护的作用,但不 影响连接,但是Tcp / ip协议的默认端口将被隐式修改为2433,在客户端连接时必须作相应的改变。    4 如果服务器端操作系统打过sp2补丁,则要对windows防火墙作一定的配置,要对它开放1433端口,通常在测试时可以直接关掉 windows防火墙(其他的防火墙也关掉最好)。    5 检查服务器是否在1433端口侦听。如果服务器没有在tcp连接的1433端口侦听,则是连接不上的。检查方法是在服务器的dos或命令行下面输入   netstat - a - n 或者是netstat - an,在结果列表里看是否有类似 tcp 127.0 . 0.1 1433 listening 的项。如果没有,则通常需要给 sql server 2000打上至少sp3的补丁。其实在服务器端启动查询分析器,输入 select @@version 执行后可以看到版本号,版本号 在8. 0 .2039以下的都需要打补丁。   如果以上都没问题,这时你再做telnet 服务器ip 1433 测试,将会看到屏幕一闪之后光标在左上角不停闪动。恭喜你, 你马上可以开始在企业管理器或查询分析器连接了。   三 检查客户端设置   程序 -> Microsoft SQL Server -> 客户端网络使用工具。像在服务器网络实用工具里一样,确保客户端tcp / ip协议启用,并且 默认端口为1433(或其他端口,与服务器端保持一致就行)。   四 在企业管理器里或查询那分析器连接测试   企业管理器 -> 右键SQlserver组 -> 新建sqlserver注册 -> 下一步 -> 写入远程IP -> 下一步 -> 选Sqlserver登陆 -> 下一步 -> 写入 登陆名与密码(sa,password) -> 下一步 -> 下一步 -> 完成   查询分析器 -> 文件 -> 连接 -> 写入远程IP -> 写入登录名和密码(sa,password) -> 确定   通常建议在查询分析器里做,因为默认情况下,通过企业管理器注册另外一台SQL Server的超时设置是4秒,而查询分析器是 15秒。   修改默认连接超时的方法:   企业管理器 -> 工具 -> 选项 -> 在弹出的"SQL Server企业管理器属性"窗口中,点击"高级"选项卡 -> 连接设置 -> 在 登录超时 (秒) 后面的框里输入一个较大的数字   查询分析器 -> 工具 -> 选项 -> 连接 -> 在 登录超时(秒) 后面的框里输入一个较大的数字   通常就可以连通了,如果提示错误,则进入下一步。   五 错误产生的原因通常是由于SQL Server使用了"仅 Windows"的身份验证方式,因此用户无法使用SQL Server的登录帐户 (如 sa )进行连接。解决方法如下所示:    1 在服务器端使用企业管理器,并且选择"使用 Windows 身份验证"连接上 SQL Server。    2 展开"SQL Server组",鼠标右键点击SQL Server服务器的名称,选择"属性",再选择"安全性"选项卡。    3 在"身份验证"下,选择"SQL Server和 Windows "。    4 重新启动SQL Server服务。(在dos或命令行下面net stop mssqlserver停止服务,net start mssqlserver启动服务,也是一种快捷的方法)。

为该计算机上运行的早期版本的 SQL Server(从 Microsoft SQL Server 7.0 开始)提供此服务。

PS:上面的服务器名称中的,21中的21第五步中第1步中设置的IPALL中的TCP端口
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL Server 中,可以使用 SQL Server Agent 和 Database Mail 功能来发送电子邮件。下面是一个简单的步骤指南: 1. 配置 SQL Server Agent: - 打开 SQL Server Management Studio (SSMS),连接到 SQL Server 实例。 - 在“对象资源管理器”窗口中,展开“SQL Server代理”节点,右键单击“邮件”,选择“管理辅助任务”。 - 在“SQL Server代理任务向导”中,选择“添加辅助任务”。 - 在“辅助任务”页面上,选择“发送电子邮件”辅助任务,并点击“下一步”。 - 在“发送电子邮件”页面上,输入任务名称和描述,并点击“完成”。 2. 配置 Database Mail: - 在 SSMS 中,展开“管理”节点,右键单击“Database Mail”,选择“配置 Database Mail”。 - 在“Database Mail 配置向导”中,选择“启用 Database Mail”并点击“下一步”。 - 在“配置新配置文件”页面上,输入配置文件名称和描述,并点击“添加”。 - 在“编辑配置文件”页面上,输入 SMTP 服务器的名称、端口和发件人地址。点击“确定”。 - 在“配置新配置文件”页面上,选择刚刚创建的配置文件,并点击“下一步”。 - 在“配置新配置文件”页面上,选择“公共配置文件”,并点击“下一步”。 - 在“配置 Database Mail”页面上,点击“下一步”,然后点击“完成”。 3. 创建一个作业: - 在 SSMS 中,展开“SQL Server 代理”节点,展开“作业”节点,右键单击“新建作业”。 - 在“新建作业”对话框中,输入作业名称和描述。 - 在“步骤”页面上,点击“新建”。 - 在“新建步骤”对话框中,输入步骤名称、选择数据库和输入 T-SQL 脚本来执行发送邮件的操作。例如: ```sql EXEC msdb.dbo.sp_send_dbmail @profile_name = 'YourDatabaseMailProfile', @recipients = '[email protected]', @subject = 'Test Email', @body = 'This is a test email from SQL Server.' ``` 替换 `YourDatabaseMailProfile` 为你在第二步中创建的配置文件名称。 - 点击“确定”。 - 在“调度”页面上,选择你想要执行作业的时间表。如果需要立即执行,可以点击“立即”。 - 点击“确定”。 现在,当作业按计划运行时,它将发送一封电子邮件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值