用VS2005开发ASPNET时在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错),以前出现这样的问题的error:40,是很好解决的,现在这在网上找了一些资料,说进行程序 —>Microsoft SQL Server 2005 —> 配置工具—> SQLServer 外围区域配置 —>服务与连接的外围配置器—>SQL Server Browser —>服务—>启动,就可以解决了,但是我的还是不行,也是允许 sql 安全性 \服务器身份验证 选择 sql与windows集成身份验证模式和 sql server configuration manager\网络配置\协议 里面允许tcp/ip连接,但是目前还是不行,但是我如果用VS中的SqlDataSource这个控件来配置的话又能正确的显示,是什么原因呢,难道我的代码写错了?下面是我的代码,请高手帮我找一下错:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=MYCOMPUTER\\SQLEXPRESS; Initial Catalog=newswebsite ;User ID=designer;password=123");
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbCatalog", con);
SqlDataReader rda = cmd.ExecuteReader();
this.GridView1.DataSource = rda;
this.GridView1.DataBind();
con.Close(); }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=MYCOMPUTER\\SQLEXPRESS; Initial Catalog=newswebsite ;User ID=designer;password=123");
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbCatalog", con);
SqlDataReader rda = cmd.ExecuteReader();
this.GridView1.DataSource = rda;
this.GridView1.DataBind();
con.Close(); }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{ }
}
如何打开sql server 2005 的1433端口配置工具->Sql Server Configuration Manager->MSSQLSERVER的协议看看TCP/IP协议是否启动,如果启动,右键菜单点"属性" ,在分页菜单中选"IP地址",把"IP1"和"IP2"中"TCP端口"为1433,"已启用"改为"是"配置工具->Sql Server Configuration Manager->SQL Native Client 配置->客户端协议->TCP/IP选择TCP/IP右键菜单中"属性",确认"默认端口"是1433,"已启用"为"是"
SQL 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 Server(从 Microsoft SQL Server 7.0 开始)提供此服务。