读书人

DataAdapter填充DataSet有关问题

发布时间: 2012-01-19 20:57:58 作者: rapoo

DataAdapter填充DataSet问题
当用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
------解决方案--------------------


在Server Explorer查看你的数据库连接状态
[解决办法]
要远程连接要打开SQL2005的Tcp/Ip协议
[解决办法]
con.open();
[解决办法]
SQL2005 启动服务
[解决办法]
数据库连接失败.con.open()检查一下你写的数据库连接的代码.......
[解决办法]
1.检查数据库是否正常可用,别的程序是否能够正常连接数据库,如不能:
SQL Server 外围应用配置器-> 远程连接,打开Tcp/Ip 默认的是本地连接,要选中本地连接和远程连接
或者
重装,呵呵
2.如果数据库没问题,检查程序:
a.con.open()和con.close()
b.检查数据库连接字符串,这个字符串可能有问题
[解决办法]
User ID=sa;password=;
既然设置了用户和密码
看看你SQLSERVER的登陆是不是Windows集成登陆
如是,改成另一种,指定用户和密码!
[解决办法]
数据库连接串的问题吧

读书人网 >asp.net

热点推荐