protected void Page_Load(object sender, EventArgs e)
        {
            string dataSource = "Data Source=localhost";
            string security = "user id=sa;password=;";
            string initialCatalog = "initial catalog=pubs;";
            string cnnString = dataSource + security + initialCatalog;            SqlConnection connection = new SqlConnection(cnnString);            //数据库连接
            string strSql = "select * from [authors]";
            SqlCommand cmd = new SqlCommand(strSql, connection);            DataSet dataSet = new DataSet();            try
            {
                connection.Open();
          
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmd;
                sda.Fill(dataSet);//dataSet为空,为什么会这样呢?
            }
            catch (SqlException ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                connection.Close();
            }            grid1.DataSource=dataSet.Tables[0];
            grid1.DataBind();
        }
    }

解决方案 »

  1.   


    SqlDataAdapter sda = new SqlDataAdapter(cmd);
     sda.Fill(dataSet); //断点调试下 就知道哪一步有问题了
      

  2.   

    有这个异常,可是怎么不能与数据库连接呢,密码是对的:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
      

  3.   

    string dataSource = "Data Source=localhost;"; 
      

  4.   

    是本机上吧,把Data Source=localhost 换成Data Source=(local),更稳定,要不用IP也可以。
      

  5.   

    string dataSource = "Data Source=localhost";  
    改成
    string dataSource = "Data Source=localhost;"; 少了个分号 连接字符串建议放到webconfig中
      

  6.   

    就如Jinglecat所说的,谢谢各位,是我太不认真了,我也是刚刚进入.NET的,以后多多指教.