日期:2014-05-17  浏览次数:20617 次

vs 2010链接数据库失败

我在vs2010 用asp.net(C#)链接数据库时,出现如上图所示错误,链接数据库的代码如下:
SqlConnection sqlConn = new SqlConnection("server=(localhost);uid=tao;pwd=tao;database=Exam;");
        DataSet ds = new DataSet();
        SqlDataAdapter myComm = new SqlDataAdapter("select ProName from Province", sqlConn);
        myComm.Fill(ds);
        sqlConn.Close();
为什么 myComm.Fill(ds); 这一行会报错?
我用其他的项目试过,数据库连接没有问题,why??
vs2010 visual?studio?2010 Sql?Server?2005 数据库 数据库连接出错

------解决方案--------------------
1.根据数据库版本,看数据库是否已经设置运行sa和windows同时连接
2.验证你的帐号密码是否正确直接登录数据库试试
3.数据库实例名是否正确?
------解决方案--------------------
是不是连接错数据库了??

或者是数据库服务没有开启,检查下

还有那个(localhost)换成.试试

SqlConnection sqlConn = new SqlConnection("Data Source=.;uid=tao;pwd=tao;database=Exam;");
------解决方案--------------------
The Fill method retrieves the data from the data source using a SELECT statement.The IDbConnection object associated with the select command must be valid, but it does not need to be open.If the IDbConnection is closed before Fill is called, it is opened to retrieve data and then closed.If the connection is open before Fill is called, it remains open.

来自msdn.
------解决方案--------------------
连接字符串里本机地址可以用
.
(local)
localhost
不可以用(localhost)