当用DataAdapter填充DataSet时,da.FILL(ds)时总报错,是怎么回事,用SQL SERVER2000和2005都出同样的问题,运行程序如下:
System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class filldemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection( "Initial Catalog=pubs;Data Source=(local);User ID=sa;password=; ");
SqlCommand cmd = new SqlCommand( "select * from employee ",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "employee ");
GridView1.DataSource = ds;
GridView1.DataMember = "employee ";
GridView1.DataBind();
}
}
出错信息为:An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
------解决方案--------------------------------------------------------
链接数据库失败。
------解决方案--------------------------------------------------------
con.open();
------解决方案--------------------------------------------------------
看数据库服务是不是关闭!???
------解决方案--------------------------------------------------------
不用con.open也行。
------解决方案--------------------------------------------------------
这个错误应该是2005的吧,在config manager里面看看
------解决方案--------------------------------------------------------
SQL Server 外围应用配置器-> 远程连接,打开Tcp/Ip 默认的是本地连接,要选中本地连接和远程连接
------解决方案--------------------------------------------------------
应是SQL服务没有开启,或连接字符串不正确。
------解决方案--------------------------------------------------------
错误提示说可能服务器不允许远程连接,就是说你的程序不管有没有问题,反正现在是连不上数据库服务器。
------解决方案--------------------------------------------------------
amandag(高歌) 正解
在看看数据库的连接有没有问题,看看web.config
------解决方案--------------------------------------------------------